0

我尽一切努力让这段代码正常工作,我希望有人能救我..

我只想初始化我现在在评论中的对象 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)
4

1 回答 1

1

看起来你想创建一个Objects对象数组,对吧?

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

Street2 = new Objects[] { new Objects(1891, 1011, 1), new Objects (1847, 1025, 101) };
于 2012-06-25T20:09:00.663 回答