Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8730565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:03:02+00:00 2026-06-13T09:03:02+00:00

I have found many related post here but couldn’t get my answer.Why this run-time

  • 0

I have found many related post here but couldn’t get my answer.Why this run-time error ?

static List<Integer>[] adj = (List<Integer>[]) new ArrayList[1000];

    public static void main(String[] args) {
        int edge, u, v, source;
        Scanner input = new Scanner(System.in);
        edge = input.nextInt();
        for (int i = 0; i < edge; i++) {
            u = input.nextInt();
            v = input.nextInt();
            adj[v].add(u); // Null pointer Exception
            adj[u].add(v); // Null pointer Exception
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:03:03+00:00Added an answer on June 13, 2026 at 9:03 am

    First you need to initialize each elements of your array. Because until you do this, your reference in the array are not pointing to any object.

    So, before that for-loop, you can add this one, to initialize your List inside the Array: –

    for (List<Integer> elem: adj) {
        elem = new ArrayList<Integer>();
    }
    

    Also, it would be better if you have List of List rather than an array of List. So, you can declare your list as: –

    static List<List<Integer>> adj = new ArrayList<List<Integer>>();
    

    An advantage of using an ArrayList is that, you don’t have to limit your size at the beginning. So, you can add any number of elements. But, if it is a requirement to create a fixed size list, you can pass the size parameter in your ArrayList constructor.

    And then you need to change your element adding code from: –

    adj[v].add(u);
    

    to: –

    adj.get(v).add(u);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i found many related questions here. I have a simple java program . it
I have hunted around for answer to this one, and though have found related
This has to be a common question and I found many related here in
I have found many great answers to this from a year ago (when it
I have found many people with simliar issues but no soultions...basically I have two
I have been going through the web about this exception. I found many articles
I have a problem that I have found many similar problems related to multi
I think there are many questions related to ios storage guideline. I have found
I have found many similar posts and even tried to find out how to
I am relatively new to assembly language. I have found so many tutorials that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.