我尽一切努力使此代码正常工作,希望有人能救我。

我只想初始化当前在评论中的对象Street1和Street2。

我发现使我的代码正常工作的唯一方法是像这样初始化它们:

Street1 =新对象[0];
Street2 =新对象[0];

但是我不能填补我的对象!我工作了好几天,但是当我决定进入一个非静态课程时,这只是给了我那个错误。.请问怎么了?

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];

}


因此,此代码有效,以下代码不起作用,我也不知道为什么:

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];

}


它返回此错误:

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)

最佳答案

看来您想创建Objects对象的数组,对吗?

在定义其内容之前,您省去了构建新数组的代码:

Street2 = new Objects[] { new Objects(1891, 1011, 1), new Objects (1847, 1025, 101) };

07-27 14:01