You created the array but didn't put anything in it, so you have an array that contains 5 elements, all of which are null. You could add
boll[0] = new ResultList();
before the line where you set boll[0].name.
Answer from Nathan Hughes on Stack OverflowYou created the array but didn't put anything in it, so you have an array that contains 5 elements, all of which are null. You could add
boll[0] = new ResultList();
before the line where you set boll[0].name.
ResultList[] boll = new ResultList[5];
creates an array of size=5, but does not create the array elements.
You have to instantiate each element.
for(int i=0; i< boll.length;i++)
boll[i] = new ResultList();
[Java] Null Pointer Exception with respect to an array of objects
java - Array null pointer exception error - Stack Overflow
java - Why am I getting NullPointerExceptions after assigning references in arrays in a loop? - Stack Overflow
Java NullPointerException why is the array null? - Stack Overflow
Videos
Hey r/learnprogramming, I'm doing this project for school and I can't seem to figure out why I'm receiving this null pointer exception. I believe the object is null on initialization, but I'm trying to use a setter method from inside the class to scan in a line of text one at a time from a file.
Luckily it's not due for about a week (thankful that I started this early) so I'm not in too much of a panic, but I'd like to at least get past this error so I can start writing some of my methods.
The error I receive is
Exception in thread "main" java.lang.NullPointerException at Micsa.main(n00737298.java:21)
Here is my code
I'd appreciate any help or even just a pointer (lol) in the right direction. I feel like it's right in front of me but maybe I've just been staring at my monitor too long.
Thank you!
As your code is currently written,
CopySystem.out.println(studentArray[0].getFirstName());
Will throw an NPE since you're never initializing the array.
Copyprivate static students[] studentArray;
Just declares it, doesn't initialize it.
Maybe you should refactor your code as follows
Copyprivate static ArrayList<students> studentArray = new ArrayList<students>();
// Inside the constructor or some method called before you access studentArray:
studentArray.add(stu1);
studentArray.add(stu2);
//...
// or, more concisely, since you're not using the temporary `students` variables:
studentArray.add(new students(65435, "Bob", "Ted"));
studentArray.add(new students(45546, "Guy", "Sid"));
//...
If you're committed to using an array and not a List,
Copyprivate static students[] studentArray;
needs to be initialized with something like
Copyprivate static students[] studentArray = new students[10];
and then you need to assign its elements
CopystudentArray[0] = stu1;
or
CopystudentArray[0] = new students(65435, "Bob", "Ted");
You should always create a object before trying to access it. You have declared a reference pointing to a array :
Copyprivate static students[] studentArray;
but never assigned any object to the reference. Hence you get a NullPointerException.
Use this :
Copyprivate static students[] studentArray = new students[size];
Above code will create a new object (array of students), pointed by a reference variable (studentArray). OR
You can use List or Set which are more efficient then array.
Because you never initialized the array with any values, so it just contains a bunch of nulls. You need to give it values before you can call .toString() on them.
For example:
Integer[] array = new Integer[5] { 1, 2, 3, 4, 5 };
Your initial loop (x = -1} is not changing the array, but rather the transient object x in your loop. It would be like doing this:
for (int i = 0; i < array.length; i++) {
Integer j = array[i];
j = -1;
}
Instead, do this if you want to use a loop to initialize:
for (int i = 0; i < array.length; i++) {
array[i] = -1;
}
Integer[] array=new Integer[5];
for(Integer x: array){x=-1;}
The code above doesn't update the array. If you want to update it, you'll need to assign values to the array elements, like this.
for( int i = 0; i < array.length; i++ )
array[ i ] =-1;
The reason the first snippet doesn't do anything useful is that Java is pass by value only. When you create a local variable x and assign an array element to it as its value (as you do in your code), it will contain a reference to the value (the Integer object), but not to the location it's in the array. When you change the value of x, you simply make x point to a different object.