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 Overflow
🌐
TutorialsPoint
tutorialspoint.com › when-does-a-java-array-throws-a-nullpointerexception
When does a Java Array Throws a NullPointerException?
While working with arrays a NullPointerException occurs − · If you try to access the elements of an array which is not initialized yet (which is null). public class Demo { public static void main(String args[]) { int myArray[] = null; System.out.println(myArray[5]); } } Exception in thread ...
Discussions

[Java] Null Pointer Exception with respect to an array of objects
Vehicle[] stuff = new Vehicle[200]; This line creates an array that can hold references to 200 vehicle objects. However, each index in the array is initially empty; there are no Vehicle objects in the array. Before you can call stuff[i].whatever(), you need to set an Vehicle object at that position in the array. You should have something like: stuff[i] = new Vehicle(); More on reddit.com
🌐 r/learnprogramming
7
1
October 25, 2014
java - Array null pointer exception error - Stack Overflow
I am continuing with my project for school and have seemed to encounter another error. So what is happening is basically I am receiving a null pointer exception even though the code looks fine. I More on stackoverflow.com
🌐 stackoverflow.com
java - Why am I getting NullPointerExceptions after assigning references in arrays in a loop? - Stack Overflow
0 Why am I getting a NullPointerException with arrays in Java? More on stackoverflow.com
🌐 stackoverflow.com
Java NullPointerException why is the array null? - Stack Overflow
The heuristic for NullPointerExceptions is almost always the same: You should critically read your exception's stacktrace to find the line of code at fault, the line that throws the exception, and then inspect that line carefully, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me. – Hovercraft Full Of Eels Commented Apr 5, 2016 at 15:35 · The array ... More on stackoverflow.com
🌐 stackoverflow.com
May 24, 2017
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › NullPointerException.html
NullPointerException (Java Platform SE 8 )
April 21, 2026 - Accessing or modifying the slots of null as if it were an array. Throwing null as if it were a Throwable value. Applications should throw instances of this class to indicate other illegal uses of the null object. NullPointerException objects may be constructed by the virtual machine as if ...
🌐
Reddit
reddit.com › r/learnprogramming › [java] null pointer exception with respect to an array of objects
r/learnprogramming on Reddit: [Java] Null Pointer Exception with respect to an array of objects
October 25, 2014 -

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!

🌐
GeeksforGeeks
geeksforgeeks.org › java › null-pointer-exception-in-java
Null Pointer Exception in Java - GeeksforGeeks
April 24, 2026 - A NullPointerException occurs due to the following reasons: Invoking a method from a null object. Accessing or modifying a null object’s field. Taking the length of null, as if it were an array...
🌐
Sentry
sentry.io › sentry answers › java › avoiding `nullpointerexception` in java
Avoiding `NullPointerException` in Java | Sentry
July 12, 2022 - Let’s take a look at a concrete example of where a NullPointerException might be thrown. ... class NullPointerExample { private static void printFirstElement(String[] array) { System.out.println(array[0]); } public static void main(String[] args) { String[] myStringArray = null; printFirstElement(myStringArray); } } Executing this code will result in the following stack trace: Exception in thread "main" java.lang.NullPointerException at NullPointerExample.printFirstElement(NullPointerExample.java:6) at NullPointerExample.main(NullPointerExample.java:11)
Top answer
1 of 2
11

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");
2 of 2
4

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.

Find elsewhere
🌐
Quora
quora.com › How-can-you-get-a-null-pointer-exception-in-case-of-arrays
How to get a null pointer exception in case of arrays - Quora
Answer (1 of 2): Here are two scenarios a [code ]NullPointerException[/code] (NPE) can occur while using arrays, in Java. 1. Array definition Take the case of an array variable is defined and is not instantiated (or is not pointing to a array object). This is applicable to both arrays of type p...
🌐
Software Testing Help
softwaretestinghelp.com › home › java › what is nullpointerexception in java & how to avoid it
What Is NullPointerException In Java & How To Avoid It
April 1, 2025 - In the above program, we declare an array and assign null to it i.e. no data. When we use the length property on this null array, NullPointerException is thrown. ... Similar to length, even if we try to access a value in a null array using an index, it is the cause of java.lang.NullPointerException.
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › lang › NullPointerException.html
NullPointerException (Java Platform SE 7 )
Accessing or modifying the slots of null as if it were an array. Throwing null as if it were a Throwable value. Applications should throw instances of this class to indicate other illegal uses of the null object. NullPointerException objects may be constructed by the virtual machine as if ...
🌐
Syntx Scenarios
syntaxscenarios.com › home › java › how to fix nullpointerexception in java (5 causes & fixes)
How to Fix NullPointerException in Java (5 Causes & Fixes)
March 14, 2026 - If you try to access an element in an array that hasn’t been initialized (i.e., it’s null), Java will throw a NullPointerException. This happens because you’re trying to work with an array that doesn’t exist in memory yet.
🌐
Scaler
scaler.com › home › topics › null pointer exception in java
Null Pointer Exception in Java - Scaler Topics
March 20, 2024 - If we perform the in-built operations on the null array, it throws a null pointer exception, as in the following code.
🌐
Wikibooks
en.wikibooks.org › wiki › Java_Programming › Preventing_NullPointerException
Preventing NullPointerException - Wikibooks, open books for an open world
NullPointerException is thrown when an application attempts to use an object reference, having the null value. These include: Calling an instance method on the object referred by a null reference. Accessing or modifying an instance field of the object referred by a null reference. If the reference type is an array type, taking the length of a null reference...
🌐
Coderanch
coderanch.com › t › 657049 › java › Null-Pointer-Exception-Iterating-Array
Null Pointer Exception when Iterating through 2d Array (Java in General forum at Coderanch)
October 24, 2015 - Go through your code and see where you have declared an array and not initialised it, remembering that all the elements in your array must be initialised, too. Then you can see where you didn't get rid of the nulls.
🌐
G. Hunter Anderson
ghunteranderson.com › posts › java › helpful-null-pointers
Helpful NullPointerExceptions · G. Hunter Anderson
November 5, 2019 - Similarly, we cannot store a value into the index of a null array. myArray[i] = 10; /* Exception in thread "main" java.lang.NullPointerException: Cannot store to int array because "myArray" is null */
🌐
Coderanch
coderanch.com › t › 676312 › java › NullPointerException-adding-array-list
NullPointerException adding to array list [Solved] (Beginning Java forum at Coderanch)
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums · this forum made possible by our volunteer staff, including ... ... I'm trying to finish a program in which i add values to an array list from user input and organize them via comparable, but i keep getting a null pointer exception.
🌐
Medium
medium.com › @timothyjosephcw › what-is-a-nullpointerexception-npe-in-java-how-to-fix-it-244ad1ed77f7
What is a NullPointerException (NPE) in Java? How to Fix It? | by timothy joseph | Medium
August 21, 2024 - A NullPointerException (NPE) is a runtime exception in Java that occurs when your program attempts to use an object reference that has not been initialized (i.e., it points to null).
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › lang › NullPointerException.html
NullPointerException (Java SE 17 & JDK 17)
April 21, 2026 - Accessing or modifying the slots of null as if it were an array. Throwing null as if it were a Throwable value. Applications should throw instances of this class to indicate other illegal uses of the null object. NullPointerException objects may be constructed by the virtual machine as if suppression were disabled and/or the stack trace was not writable.