Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] Array of a Class

Started by AdamGL Jan 1, 2007 at 5:22 PM 3 replies 650+ views
Original Post
AdamGL
AdamGL
This gives me a "NullPointerException":

public class Hoop
{
    public static void main(String [] args)
    {
        CollisionObject [] bullet = new CollisionObject [50];
        for(int n = 0; n < 50; n++)
        {
            bullet[n].isActive = false;
        }
    }
}


public class CollisionObject
{
    public boolean isActive;
    public int XPosition;
    public int YPosition;
    public int width;
    public int height;
}

Why? I don't really understand. I am pretty sure my syntax is correct. Any suggestions?
bi-FreeSoftware
rip-off
rip-off
You have allocated an array of 50 CollisionObject references, but you haven't made them reference anything.

You need to allocate a new CollisionObject at each iteration, like so:
for(int n = 0; n < 50; n++){    bullet[n] = new CollisionObject();     bullet[n].isActive = false;}


Currently you are attempting to get a fields value from bullet[n], which is null.
Nypyren
Nypyren
(Edit) beaten to the punch
kratz
kratz
I think you need to loop through and make each spot in the array a new CollisionObject. Like so...

for(int n = 0; n < 50; n++)
{
bullets[n] = new CollisionObject();
bullet[n].isActive = false;
}
---------------------------Noobies have feelings too!!----------------------------
AdamGL
AdamGL
Oh! Wow, thanks. Looks like I misunderstood the documentation. Thanks a bunch fellas!!
bi-FreeSoftware

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.