我尝试了一切来使这段代码正常工作,我希望有人能救我......
我只是想初始化我现在正在评论中的Objects 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)
答案 0 :(得分:1)
看起来你想要创建一个Objects
个对象的数组,对吗?
在定义其内容之前,您省略了构造新数组的代码:
Street2 = new Objects[] { new Objects(1891, 1011, 1), new Objects (1847, 1025, 101) };