Results 1 to 4 of 4

Thread: Why does my array of objects end up with an array of null pointers?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,190

    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.

  2. #2
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,826

    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

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,840

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,826

    Re: Why does my array of objects end up with an array of null pointers?

    Quote Originally Posted by jmcilhinney View Post
    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
  •  



Click Here to Expand Forum to Full Width