-
Jul 9th, 2024, 11:28 PM
#1
Thread Starter
Frenzied Member
Why does my array of objects end up with an array of null pointers?
When I do this:
Code:
MyClass[] ArrayOfMyClass = new MyClass[100];
instead of giving me an array of initialized objects of type MyClass (as it was supposed to do, as I did use the "new" keyword), it instead has given me an array of null pointers. Any time I attempt to do anything with any entry in this array, I get a null pointer exception. Why is that?
Last edited by Ben321; Jul 9th, 2024 at 11:42 PM.
-
Jul 10th, 2024, 12:42 AM
#2
Re: Why does my array of objects end up with an array of null pointers?
https://www.geeksforgeeks.org/how-to...jects-in-java/
And once an array of objects is instantiated like this, then the individual elements of the array of objects needs to be created using the new keyword.
Code:
MyClass[] ArrayOfMyClass = new MyClass[100];
ArrayOfMyClass[0] = new MyClass(123456)
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Jul 10th, 2024, 01:19 AM
#3
Re: Why does my array of objects end up with an array of null pointers?
Your question is like asking "when I made this new egg carton, why didn't it contain a dozen eggs". You're just creating the container to store the objects. You still have to create the objects and put them in the container.
You can think of an array as basically being a way to group multiple variables where each element is a variable. If a variable of a type would initially be null then so will an element of an array of that type. Note that, in .NET, a variable of a data type that is a class will be null by default while a variable of a data type that is a structure will contain an instance of that structure with all its fields having default values. Not sure whether Java has similar concepts but your situation is like classes in .NET.
-
Jul 10th, 2024, 01:41 AM
#4
Re: Why does my array of objects end up with an array of null pointers?
Originally Posted by jmcilhinney
Your question is like asking "when I made this new egg carton, why didn't it contain a dozen eggs". You're just creating the container to store the objects. You still have to create the objects and put them in the container.
You can think of an array as basically being a way to group multiple variables where each element is a variable. If a variable of a type would initially be null then so will an element of an array of that type. Note that, in .NET, a variable of a data type that is a class will be null by default while a variable of a data type that is a structure will contain an instance of that structure with all its fields having default values. Not sure whether Java has similar concepts but your situation is like classes in .NET.
From my link above:
The array of Objects the name itself suggests that it stores an array of objects. Unlike the traditional array stores values like String, integer, Boolean, etc an Array of Objects stores objects that mean objects are stored as elements of an array. Note that when we say Array of Objects it is not the object itself that is stored in the array but the reference of the object.
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|