0

I tried everything to get this code working, and I hope someone will save me..

I just want to initialize my Objects Street1 and Street2 which are in commentary right now.

The only way i found to make my code work is to initialize them like this :

Street1 = new Objects[0];
Street2 = new Objects[0];

But i can't fill my Objects ! I worked fine for days, but when i decided to move into a non-static class, it just gave my that error.. What's wrong please ?

public class Maps {


int CurrentMapID ;

int CheckedObjetQuantity=0;

Map[] MapList = new Map[5];

Objects[] Street1; 
Objects[] Street2;

Map CurrentMap;


public Maps(){

    CurrentMapID = 1;


    //Street1 = {new Objects(987,1020,1,2),new Objects(985,1036,3,75/*,true,3*/),new Objects(1259,1028,1,3),new Objects(3391,1036,1,1),new Objects(213,1013,102,1)};  // les Objets de la map Street1      (x,y,IDobjet,quantité)
    //Street2 = {new Objects(1891,1011,1), new Objects (1847,1025,101)};

    Street1 = new Objects[0];
    Street2 = new Objects[0];


    MapList[1]= new Map (1,4,2,Street1,(Main.InstallPath+"maps/street.png"),(Main.InstallPath+"maps/fstreet.png"),(Main.InstallPath+"maps/streetcollision.png"),50,800,3600,800);
    MapList[2]= new Map (2,1,3,Street2,(Main.InstallPath+"maps/street2.png"),(Main.InstallPath+"maps/fstreet2.png"),(Main.InstallPath+"maps/street2collision.png"),50,800,3600,800);
    MapList[3]= new Map (3,2,4,Street2,(Main.InstallPath+"maps/street2.png"),(Main.InstallPath+"maps/fstreet2.png"),(Main.InstallPath+"maps/street2collision.png"),50,800,3600,800);
    MapList[4]= new Map (4,3,1,Street2,(Main.InstallPath+"maps/street2.png"),(Main.InstallPath+"maps/fstreet2.png"),(Main.InstallPath+"maps/street2collision.png"),50,800,3600,800);


    CurrentMap = MapList[CurrentMapID];

}

So, this code works, the following won't work and i don't know why:

public class Maps {


int CurrentMapID ;

int CheckedObjetQuantity=0;

Map[] MapList = new Map[5];

Objects[] Street1 = {new Objects(987,1020,1,2),new Objects(985,1036,3,75/*,true,3*/),new Objects(1259,1028,1,3),new Objects(3391,1036,1,1),new Objects(213,1013,102,1)};  // les Objets de la map Street1      (x,y,IDobjet,quantité)
Objects[] Street2 = {new Objects(1891,1011,1), new Objects (1847,1025,101)};

Map CurrentMap;


public Maps(){

    CurrentMapID = 1;


    //Street1 = new Objects[0];
    //Street2 = new Objects[0];


    MapList[1]= new Map (1,4,2,Street1,(Main.InstallPath+"maps/street.png"),(Main.InstallPath+"maps/fstreet.png"),(Main.InstallPath+"maps/streetcollision.png"),50,800,3600,800);
    MapList[2]= new Map (2,1,3,Street2,(Main.InstallPath+"maps/street2.png"),(Main.InstallPath+"maps/fstreet2.png"),(Main.InstallPath+"maps/street2collision.png"),50,800,3600,800);
    MapList[3]= new Map (3,2,4,Street2,(Main.InstallPath+"maps/street2.png"),(Main.InstallPath+"maps/fstreet2.png"),(Main.InstallPath+"maps/street2collision.png"),50,800,3600,800);
    MapList[4]= new Map (4,3,1,Street2,(Main.InstallPath+"maps/street2.png"),(Main.InstallPath+"maps/fstreet2.png"),(Main.InstallPath+"maps/street2collision.png"),50,800,3600,800);


    CurrentMap = MapList[CurrentMapID];

}

It return this error :

Exception in thread "main" java.lang.ExceptionInInitializerError
        at Maps.<init>(Maps.java:11)
        at Main.main(Main.java:73)
Caused by: java.lang.NullPointerException
        at Box.<init>(Box.java:65)
        at Objects.<clinit>(Objects.java:16)
3
  • 1
    Are you certain you mean to give the Object arrays a size of 0? Commented Jun 25, 2012 at 20:04
  • Please show the complete exception, including the stack trace. Commented Jun 25, 2012 at 20:05
  • 1
    Might want to consider making your variable names unique and less likely to clash with Java keywords. Commented Jun 25, 2012 at 20:05

1 Answer 1

1

It looks like you want to create an array of Objects objects, right?

You left out the code to construct the new array before defining its contents:

Street2 = new Objects[] { new Objects(1891, 1011, 1), new Objects (1847, 1025, 101) };
Sign up to request clarification or add additional context in comments.

2 Comments

I tried to place this code (with Street1 too) in my constructor and it didn't work either.. Same when placing it above my constructor
Gosh, I found where the bug was ! Thank you very much ! Your code works !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.