user interface - Java - Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException error -
i creating simple game in java. here code. gui class:
public class gui extends jframe implements actionlistener{ jbutton upbutton, downbutton, leftbutton, rightbutton; game game; player player; board b; maze maze; player pl; public gui () { b= new board(); game game = new game(b); setsize(800,600); container cp = getcontentpane(); upbutton = new jbutton("up"); downbutton = new jbutton("down"); leftbutton = new jbutton("left"); rightbutton= new jbutton("right"); upbutton.addactionlistener(this); downbutton.addactionlistener(this); leftbutton.addactionlistener(this); rightbutton.addactionlistener(this); jpanel p = new jpanel(); boxlayout boxlayout1 = new boxlayout(p, boxlayout.y_axis); p.setborder(borderfactory.createemptyborder(250, 0, 250, 0)); p.setlayout(boxlayout1); jpanel panel_1 = new jpanel(); p.add(panel_1, borderlayout.north); jpanel panel_2 = new jpanel(); p.add(panel_2); jpanel panel_3 = new jpanel(); p.add(panel_3, borderlayout.after_last_line); panel_1.add(upbutton); panel_2.add(leftbutton); panel_2.add(rightbutton); panel_3.add(downbutton); cp.setlayout(new borderlayout()); cp.add(p, borderlayout.east); game= new game(b); cp.add(game, borderlayout.center); } public void actionperformed(actionevent e) { if(upbutton == e.getsource()){ game.moveplayerup(); system.out.println("up"); } if(downbutton == e.getsource()){ game.moveplayerdown(); system.out.println("down"); repaint(); } if(leftbutton == e.getsource()){ game.moveplayerleft(); repaint(); system.out.println("left"); } if(rightbutton == e.getsource()){ game.moveplayerright(); repaint(); system.out.println("right"); } game.repaint(); } public static void main(string[] args) { gui frame = new gui(); frame.setvisible(true); }
}
this game class:
public class game extends canvas {
int m; int n; // margin board b; double tokensize, markingsize; int[][] marking; int pickuptype; int pickupx, pickupy; dimension lastsize; image picture; maze maze; player player; boolean ingame = false; boolean dying = false; boolean finished = false; thread gamethread = null; int pposition [][]; public int positiona; public int positionb; public final font smallfont = new font("helvetica", font.bold, 14); public game (board db) { pposition = new int[4][4]; maze = new maze(); b=db; m=20; setbackground(color.lightgray); lastsize=getsize(); picture=createimage(lastsize.width, lastsize.height); createplayerboard(); setinitialplayerposition(); } public void paint (graphics g){ update(g); } public synchronized void update(graphics g1) { dimension d=getsize(); if (!d.equals(lastsize)) { picture=createimage(d.width, d.height); } graphics g=picture.getgraphics(); g.setcolor(getbackground()); g.fillrect(0, 0, d.width, d.height); g.setcolor(getforeground()); g.drawrect(0, 0, d.width-1, d.height-1); int h=getheight(); int w=getwidth(); int squaresize = math.max(0,(math.min(h,w)-2*m))/8; squaresize = squaresize+40; graphics2d g2 = (graphics2d) g; image img1 = toolkit.getdefaulttoolkit().getimage("3down.jpg"); image img2 = toolkit.getdefaulttoolkit().getimage("3left.jpg"); image img3 = toolkit.getdefaulttoolkit().getimage("3right.jpg"); image img4 = toolkit.getdefaulttoolkit().getimage("3up.jpg"); image img5 = toolkit.getdefaulttoolkit().getimage("all4.jpg"); image img6 = toolkit.getdefaulttoolkit().getimage("down_left.jpg"); image img7 = toolkit.getdefaulttoolkit().getimage("down_right.jpg"); image img8 = toolkit.getdefaulttoolkit().getimage("up_left.jpg"); image img9 = toolkit.getdefaulttoolkit().getimage("up_right.jpg"); image img10 = toolkit.getdefaulttoolkit().getimage("cyclops.gif"); //this code displays board (int i=0; i<5; i++) { (int j=0; j<5; j++) { if(i==0&&j==0){ g2.drawimage(img7,m + * squaresize, m + j * squaresize, squaresize, squaresize, this); g2.finalize();} else if(i==1&&j==0){ g2.drawimage(img1,m + * squaresize, m + j * squaresize, squaresize, squaresize, this); g2.finalize();} else if (i==2&&j==0){ g2.drawimage(img1,m + * squaresize, m + j * squaresize, squaresize, squaresize, this); g2.finalize();} else if (i==3&&j==0){ g2.drawimage(img1,m + * squaresize, m + j * squaresize, squaresize, squaresize, this); g2.finalize();} else if (i==4 &&j==0){ g2.drawimage(img6,m + * squaresize, m + j * squaresize, squaresize, squaresize, this); g2.finalize();} } //this code display cyclop loadplayerposition(); int x=positiona; int y=positionb; g2.drawimage(img10,m + x * squaresize, m + y * squaresize, squaresize, squaresize, this); g2.finalize(); g1.drawimage(picture,0,0,null); } //this method creates player board , fills places in array 0 public void createplayerboard(){ (int = 0; < pposition.length; i++){ (int j = 0; j < pposition.length; j++){ pposition[i][j] = 0; } } } //this method sets initial player position in array public void setinitialplayerposition(){ pposition[0][0] = 1; } //this method changes interger positiona , positib be, integers represent current player position public void loadplayerposition(){ (int = 0; < pposition.length; i++){ (int j = 0; j < pposition.length; j++){ if(pposition [i][j] == 1){ positiona=i; positionb=j; } } } } //these methods change number in player array 0 1, deppending on button player clicked public void moveplayerup(){ (int = 0; < pposition.length; i++){ (int j = 0; j < pposition.length; j++){ if(pposition [i][j] == 1){ pposition [i][j] = 0; pposition [i][j+1] = 1; } } } } public void moveplayerdown(){ (int = 0; < pposition.length; i++){ (int j = 0; j < pposition.length; j++){ if(pposition [i][j] == 1){ pposition [i][j] = 0; pposition [i][j-1] = 1; } } } } public void moveplayerleft(){ (int = 0; < pposition.length; i++){ (int j = 0; j < pposition.length; j++){ if(pposition [i][j] == 1){ pposition [i][j] = 0; pposition [i-1][j] = 1; } } } } public void moveplayerright(){ (int = 0; < pposition.length; i++){ (int j = 0; j < pposition.length; j++){ if(pposition [i][j] == 1){ pposition [i][j] = 0; pposition [i+1][j] = 1; } } } }
}
board class:
public class board {
int[][] b; maze maze; boolean alive = true; public int playerscore = 0; maze m; public board() { }
}
the program creates windows buttons, displays board , displays player icon. want is: when player clicks on right button, icon moves right on array. when click on button, error:
exception in thread "awt-eventqueue-0" java.lang.nullpointerexception
i try search error , says happens when program points object null.however, think object not null. can give me advice please? thank much
Comments
Post a Comment