I am writing a game and am now in the last phrase. I am going to write the load class but when I press the load button, I get an exception, which is caused by a null pointer.
However, I allocated every object and arrays with new
. Moreover, I have points that are non-static
but are volatile
.
This is my code:
class LOADSTATE
{
private char[] mapx;
int x=0, y=0;
public LOADSTATE()throws FileNotFoundException, IOException
{
String line;
for (int i=0;i<10;i++)
{
GamePlay.tm[i].start();
}
}
GamePlay.t.start();
int flag=0;
int m=0;
BufferedReader fr=new BufferedReader(new FileReader("save.txt"));
while((line=fr.readLine())!=null){
if(flag==0){
GamePlay.me.name=line;
}
if (flag==1){
GamePlay.tt.time=Integer.parseInt(line);
}
else if(flag==2){
GamePlay.lvl=Integer.parseInt(line);
}
else if(flag==3){
GamePlay.win=Boolean.parseBoolean(line);
}
else if(flag==4){
GamePlay.mainlvl=Boolean.parseBoolean(line);
}
else if(flag==5){
GamePlay.lvl1=Boolean.parseBoolean(line);
}
else if(flag==6){
GamePlay.lvl2=Boolean.parseBoolean(line);
}
else if(flag==7){
GamePlay.lvl3=Boolean.parseBoolean(line);
}
else if(flag>7&&flag<37){
if(flag%3==2){
x=Integer.parseInt(line);
}
if(flag%3==0){
y=Integer.parseInt(line);
GamePlay.monster[m].oldPos.setLocation(x, y);
}
if(flag%3==1){
GamePlay.monster[m].existance=Boolean.parseBoolean(line);
m++;
}
}
else if(flag==37){
x=Integer.parseInt(line);
}
else if(flag==38){
y=Integer.parseInt(line);
GamePlay.me.oldPos.setLocation(x, y);
}
else if(flag==39){
GamePlay.me.killed=Integer.parseInt(line);
}
else if(flag==40){
GamePlay.me.health=Integer.parseInt(line);
}
else if(flag>40){
mapx=line.toCharArray();
for(int i=0;i<map.length;i++){
map[flag-41][i]=Integer.parseInt(""+mapx[i]);
}
}
flag++;
}
fr.close();
}
}
class GamePlay extends JPanel implements ActionListener{
static boolean lvl1=true;
static boolean lvl2=false;
static boolean lvl3=false;
static boolean mainlvl=false;
static Me me=new Me();
static Portal portal=new Portal();
static Shot shot=new Shot();
static Thread s;
static KeyBoard keyBoard=new KeyBoard();
static Monster monster[]=new Monster[10];
static boolean win;
static Thread tm[]=new Thread[10];
static int score=1000;
static timeThread tt=new timeThread();
static Thread t=new Thread(tt);
static int time=0;
JButton btn=new JButton("start");
JButton btn1=new JButton("stop");
JButton btn2=new JButton("fixer");
JLabel lbl=new JLabel("time :");
JLabel lbl1=new JLabel("score :");
JLabel lbl2=new JLabel("health :");
JLabel lbl3=new JLabel("assasins :");
/**/}
I call game play before load state.