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

  • SEARCH
  • Home
  • 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 3320526
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:57:14+00:00 2026-05-17T22:57:14+00:00

public class Main { public static void main(String args[]) { List list = new

  • 0
public class Main {
public static void main(String args[]) {

List list = new List(0);

int[] intArr = null;  
list.fillWithRandom(intArr); // null pointer
list.print(intArr);
   }
 }



  import java.util.*;
  class List {


 private static final int NUMINTS = 10;



 private void list(int numInts) {
 List list = new List(10); 
 int[] intArr = new int[10];
 } 

public List(int i) {
// TODO Auto-generated constructor stub
 }


//fill array with random numbers
 public void fillWithRandom(int intArr[]) {
 Random r;
 r = new Random();

 int i;

 for(i=0; i < NUMINTS ; i++)

 intArr[i] = r.nextInt();     // null pointer
} 

 //display numbers
public void print(int intArr[]) {
int i;
for(i=0 ; i < NUMINTS; i++)
System.out.println(intArr[i]);


    }
  }

My message in the compiler says:

Exception in thread “main” java.lang.NullPointerException
at List.fillWithRandom(List.java:28)
at Main.main(Main.java:9)

  • 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-05-17T22:57:14+00:00Added an answer on May 17, 2026 at 10:57 pm

    You set your int array to null, then pass it to fillWithRandom. Then, without actually allocating any space for that array, you attempt to populate it.

    You need to allocate memory before you can use it.


    Here’s a nice simple one to start with:

    test.java:

        public class test {
            public static void main(String args[]) {
                MyList list = new MyList(10);
                list.fillWithRandom();
                list.print();
            }
        }
    

    MyList.java:

        import java.util.Random;
    
        public class MyList {
            private int[] list = null;
    
            public MyList(int numInts) {
                list = new int[numInts];
            }
    
            public void fillWithRandom() {
                Random r = new Random();
                for (int i=0; i < list.length; i++)
                    list[i] = r.nextInt();
            }
    
            public void print() {
                for (int i=0 ; i < list.length; i++)
                    System.out.println(list[i]);
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

static void Main(string[] args) { List<int> listArray = new List<int>(); listArray.Add(100); foreach (int item
public class Test { public static void main(String[] args) { } } class Outer
public class doublePrecision { public static void main(String[] args) { double total = 0;
public class WrapperTest { public static void main(String[] args) { Integer i = 100;
The following code public class GenericsTest2 { public static void main(String[] args) throws Exception
The following code does not compile: public class GenericsTest { public static void main(String[]
I have written this piece of code public class Test{ public static void main(String[]
I have the following code: public class Test { public static void Main() {
public class MyClass { public int Age; public int ID; } public void MyMethod()
public class CovariantTest { public A getObj() { return new A(); } public static

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.