swing - Java - Separate Listener Class throws NullPointerException -


apologize long post, wanted detailed possible there's better chance people understand i'm trying convey:

ok, overview of problem i'm trying make program simulates cash register. (this "for-fun" project). way set is:

cashregister: main class starts program, serves main window everything.

public class cashregister extends jframe {     ... } 

other classes: serve jpanels provide different components of main cashregister window. example:

public class northpanel extends jpanel {     ... } 

then in cashregister class:

add(new northpanel(), borderlayout.north); 

etc. basically, have jtextfield in northpanel class called pricefield holds value of price user enters. have separate class (keypad) extends jpanel , serves number keypad in center of main window. in cashregister:

add(new northpanel(), borderlayout.north); add(new keypad()); // (default borderlayout center) 

one of problems have run created 1 class, eventhandler, serve listener class entire program because there components in each jpanel class (northpanel , keypad, instance) need communicate each other; user presses keypad button, price field in northpanel needs know key in keypad pressed.

i don't know if problem comes fact these components coming different classes , therefore referenced differently, or what. classes in same directory, i'm using netbeans , it's part of same package.

in eventhandler class, created several different constructors able pass in components need communicate each other different classes. example:

public class eventhandler implements actionlistener {     private jtextfield pricefield;     private jbutton[][] keypad;      public eventhandler(jtextfield pricefield) {         this.pricefield = pricefield;     }      public eventhandler(jbutton[][] keypad) {         this.keypad = keypad;     } } 

in northpanel class, instantiate pricefield first, configure (set font, etc.) , say:

eventhandler e = new eventhandler(pricefield); 

in attempt pass pricefield through listener class.

then in keypad class, say:

eventhandler e = new eventhandler(keypad); for(int = 0; < 4; i++) {     for(int j = 0; j < 3; j++) {         keypad[i][j].addactionlistener(e);     } } 

then in eventhandler class, having passed through variables:

public void actionperformed(actionevent e) {     for(int = 0; < 4; i++) {         for(int j = 0; j < 3; j++) {             if(keypad[i][j] == e.getsource()) {                 // call custom method append numbers text field             }         }     } } 

it @ point nullpointerexception. don't know why, guess it's because listener objects coming different classes , using different constructors because have pass in different objects each class need communicate each other. i'm not 100% sure though. there way around or doing wrong?

you should let cashregister handle user input, main frame of application (it extends jframe). sub-panels, make method, such akeywaspressed( int keycode ), called frame, within body of keypressed method override.

also, set jframe focusable, , preferably make other panels unfocusable setfocusable(false);

here sample code of cashregister:

public class cashregister extends jframe implements keylistener {     private northpanel northpanel;     private keypad keypad;      public cashregister ()     {         this.setfocusable(true);         this.addkeylistener(this);         northpanel = new northpanel();         keypad = new keypad();          /* code ... */     }      @override     public void keytyped(keyevent ke)      {         /*do nothing*/     }      @override     public void keypressed(keyevent ke)      {         northpanel.akeywaspressed( ke.getkeycode() );         keypad.akeywaspressed( ke.getkeycode() );     }      @override     public void keyreleased(keyevent ke)      {         /*do nothing*/     }  } 


note: distinguish key pressed, inside akeywaspressed method panels, can do:

private void akeywaspressed(int code) {     if(code == keyevent.vk_a)     {         /*if letter 'a', this...*/     } } 

again, there many ways of doing it, letting frame handle user input is, in opinion, best pratice in cases, when dealing multiple panels.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -