java - This line is giving a nullpointer and I have no idea why -


i'm trying add buttons these panels, can check if they're clicked. i'm still new java , how taught how it.

right i'm making big panel , adding 48 new panels onto , adding buttons on each of panels, can make action event. if there way me check if clicked panel that, have no idea how.

i'm getting nullpointerexception on line "panel[x].add(click[x]);"

package catchthemouse;  import java.awt.*; import java.awt.event.*; import javax.swing.*;  public class catchthemouse extends jframe implements actionlistener, mouselistener{     final int rows = 8;     final int cols = 6;     final int gap = 2;     final int max_panels = rows * cols;     int clicks;     int hits;     int percentage = 0;     int width;     int height;     int panelx;     int panely;     int whichpanel = (int)(math.random() * 47 + 1);      jbutton[] click = new jbutton[max_panels];     jlabel grats = new jlabel("");     jlabel spot = new jlabel("x");     jpanel[] panel = new jpanel[max_panels];     jpanel pane = new jpanel(new gridlayout(rows, cols, gap, gap));     font xfont = new font("ariel", font.bold, 20);     font font = new font("ariel", font.plain, 12);      public catchthemouse() {         super("catch mouse");         setdefaultcloseoperation(jframe.exit_on_close);         setsize(300,300);         add(spot);         spot.setfont(xfont);         add(grats);         grats.setfont(font);         add(pane);         for(int x = 0; x < max_panels; ++x) {             panel[x] = new jpanel();             pane.add(panel[x]);             panel[x].setbackground(color.red);             panel[x].add(click[x]);             click[x].addactionlistener(this);             click[x].setvisible(false);         }         pane.setbackground(color.black);         panel[whichpanel].add(spot);     }      public void mouseclicked(mouseevent e) {         clicks = e.getclickcount();     }      public void mouseentered(mouseevent e) {      }      public void mouseexited(mouseevent e) {     }      public void mousepressed(mouseevent e) {     }      public void mousereleased(mouseevent e) {     }      public void actionperformed(actionevent e) {         object src = e.getsource();         if(src == click[whichpanel]) {             hits++;             grats.settext("you have made " + integer.tostring(hits) + " hits");         }     }      public static void main(string[] args) {         catchthemouse frame = new catchthemouse();         frame.setvisible(true);     } } 

a guess, line:

panel[x].add(click[x]); 

you're trying add jbutton's have not yet been constructed. construct them first before adding!

click[x] = new jbutton("something"); panel[x].add(click[x]); 

in future though, when asking here, please include relevant information, including , line throws exceptions you're stuck on.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -