java - Why can't I generate this GUI? -


this question has answer here:

so i'm trying create gui ball controlled buttons run through maze. begin want create frame few directional buttons , whenever try run in eclipse comes error not specific @ all. code below. error points "buttontl, buttontr, buttonbl , buttonbr" sections of code. thank can provide.

the error is:
exception in thread "main" java.lang.nullpointerexception @ mazeassignment.one.creategui(one.java:55) @ mazeassignment.one.main(one.java:23)

import java.awt.*; import java.awt.event.*; import javax.imageio.imageio; import javax.swing.*;  public class 1 extends jframe implements actionlistener//, changelistener  { private jbutton buttonright, buttonleft, buttonup, buttondown, buttonplay, buttonexit, buttonreset, buttongrid, buttontl, buttontr, buttonbl, buttonbr, buttonm, buttonoption1, buttonoption3, buttoncompass; private jpanel panelcentre, panelright, panelbottom, buttonpanel, compasspanel, optionspanel, debugpanel; private jslider sliderspeed; private jlabel appspeed; private jbutton [] jbarray = new jbutton[630]; private int blocation = 290; public int balllocation = 287; private icon bimage, iconball, tile, iconwall, iconcompassnorth;  public static void main (string[] args) {     1 frame = new one();     frame.setsize(875, 600);     frame.creategui();     frame.setvisible(true); }  private void creategui() {        setdefaultcloseoperation(exit_on_close);     container window = getcontentpane();     window.setlayout(new borderlayout() );      panelcentre = new jpanel();     panelcentre.setpreferredsize(new dimension(625, 450));     panelcentre.setbackground(color.blue);     window.add(panelcentre);     panelcentre.setlayout(new gridlayout(21, 30));      panelright = new jpanel();     panelright.setpreferredsize(new dimension(200, 450));     panelright.setbackground(color.gray);     window.add(panelright, borderlayout.east);      panelbottom = new jpanel();     panelbottom.setpreferredsize(new dimension(875, 100));     panelbottom.setbackground(color.green);     window.add(panelbottom, borderlayout.south);      debugpanel = new jpanel();     debugpanel.setpreferredsize(new dimension(200, 100));     debugpanel.setbackground(color.white);     panelright.add(debugpanel);      buttontl = new jbutton("");     buttonpanel.add(buttontl);     buttontl.setenabled(false);      buttonup = new jbutton("^");     buttonpanel.add(buttonup);     buttonup.addactionlistener(this);      buttontr = new jbutton("");     buttonpanel.add(buttontr);     buttontr.setenabled(false);      buttonleft = new jbutton("<");     buttonpanel.add(buttonleft);     buttonleft.addactionlistener(this);      buttonm = new jbutton("");     buttonpanel.add(buttonm);     buttonm.setenabled(false);      buttonright = new jbutton(">");     buttonpanel.add(buttonright);     buttonright.addactionlistener(this);      buttonbl = new jbutton("");     buttonpanel.add(buttonbl);     buttonbl.setenabled(false);      buttondown = new jbutton("v");     buttonpanel.add(buttondown);     buttondown.addactionlistener(this);      for(int i=0; i<630; i++)     {         jbarray[i] = new jbutton();         buttongrid = new jbutton(""+i);         panelcentre.add(jbarray[i]);         jbarray[i].setborderpainted(false);     } }  public void actionperformed(actionevent event) {     object source = event.getsource();      jbarray[blocation].seticon(iconball);     jbarray[blocation].seticon(tile);      if (source == buttonright)     {         jbarray[blocation+1].seticon(iconball);         jbarray[blocation-1].seticon(tile);         blocation=blocation+1;     }     if (source == buttonleft)     {         jbarray[blocation-1].seticon(iconball);         jbarray[blocation+1].seticon(tile);         blocation=blocation-1;     }     if (source == buttonup)     {         jbarray[blocation-30].seticon(iconball);         jbarray[blocation+30].seticon(tile);         blocation=blocation-30;     }     if (source == buttondown)     {         jbarray[blocation+30].seticon(iconball);         jbarray[blocation-30].seticon(tile);         blocation=blocation+30;     } }  } 

you forgot initialize buttonpanel. add before start use it:

buttonpanel= new jpanel(); 

then works fine.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -