1

I have a Gui with several different classes which uses the same eventhandler. A problem I can't seem to find a solution to is how to avoid getting a nullpointerexception when the eventhandler is looking for the right method call and stumbles upon a button that is yet to be initialized. I have a couple of buttons that will only be initialized when the user goes into a createAccountGui stage.

public class GuiHandler implements EventHandler<ActionEvent> {
    // this class take care off the EventHandler (buttons, all the buttons method).
    private UserGui uG;
    private AdvSearch aS;
    private LogIn logI;
    private CreateAccGui cAG;
    private OrganizerGui oG;
    private AdminGui aG;
    private PersonRegister pR;

    public GuiHandler() {
        uG = new UserGui(this);
    } 

Unless CreateAccGui has been initialized, whenever it comes to the cAG.getRegistrate() I get a nullpointerexception. Does anyone have a smart way of handling this sort of problem without having to split it into several different handlers.

@Override
    public void handle(ActionEvent e) {
        try {
            //where all the button goes 2 when clicked on and perform the method they are supposed 2 do
            // UserGui class buttons
            if(e.getSource() == uG.getLogInB()) {
                logI = new LogIn(this);
            } else if (e.getSource() == uG.getAdvSearch()) {
                aS = new AdvSearch(this);
            } else if (e.getSource() == uG.getSearch()) {

            }
            // the buttons in LogIn class
            else if(e.getSource() == logI.getSignIn()) {
                signIn();
            } else if(e.getSource() == logI.getCreateAcc()) {
                logI.logInStage.close();
                cAG = new CreateAccGui(this);
            }
            //the buttons in CreateAccGui class
            else if(e.getSource() == cAG.getRegistrate()) {
                System.out.println("it stop here on registrate");
                createAccount();
            } else if(e.getSource() == cAG.getCancelReg()) {
                cAG.getCreateStage().close();
                logI = new LogIn(this);
            }
            //the buttons in AdminGui for setting text from the admin field 2 the userGui
            else if(e.getSource() == aG.getAdmLogout()) {
                aG.stage.close();
            } else if(e.getSource() == aG.getHomeAreaButton()) {
                uG.getHomeNews().setText(aG.getHomeArea().getText());
            } else if(e.getSource() == aG.getAboutButton()) {
                uG.getAboutArea().setText(aG.getAboutArea().getText());
            } else if(e.getSource() == aG.getRentButton()) {
                uG.getRentArea().setText(aG.getRentArea().getText());
            }
        } catch(NumberFormatException nfe) {
            JOptionPane.showMessageDialog(null, "Feil format på noen felter, gjerne endre på dem", "Nummer Format", JOptionPane.ERROR_MESSAGE);
        }
    } // End of Handler method
}// End of GuiHandler class
Huy Trần
  • 13
  • 6

2 Answers2

0

Add a null check to the code that uses cAG

     /* right here */
else if(  cAG != null  && e.getSource() == cAG.getRegistrate()) {

System.out.println("it stop here on registrate");
    createAccount();
}

The && symbol short circuits if it is false and will never reach the cAG.getRegistrate() if cAG is null

BTW JOptionPane is part of swing, and java 8 update 40 came out with a new JavaFX dialog API

Community
  • 1
  • 1
J Atkin
  • 3,080
  • 2
  • 19
  • 33
0

At the beginning of your event handler get the source of the event and set it to a variable:

@Override
public void handle(ActionEvent e) {
  Button sourceBtn = (Button) e.getSource();
    try {
      //where all the button goes 2 when clicked on and perform the method they are supposed 2 do
      // UserGui class buttons
       switch(sourceBtn.getText()){
       case "LogIn":
          logI = new LogIn(this);
          break;
        case "Advanced Search":
          aS = new AdvSearch(this);
          break;
        case "Search":
          break;
        case "Sign In":
          signIn();
        break;
        case "Create Acc":
          logI.logInStage.close();
          cAG = new CreateAccGui(this);
        break;
        case "Registrate":
          System.out.println("it stop here on registrate");
          createAccount();
          break;
        case "Cancel Reg":
          cAG.getCreateStage().close();
          logI = new LogIn(this);
        break;
        case "AdmLogout":
          aG.stage.close();
          break;
        case "HomeAreaButton":
          uG.getHomeNews().setText(aG.getHomeArea().getText());
        break;
        case "About":
          uG.getAboutArea().setText(aG.getAboutArea().getText());
        break;
        case "Rent":
          uG.getRentArea().setText(aG.getRentArea().getText());
          break;
      }
    } catch(NumberFormatException nfe) {
               JOptionPane.showMessageDialog(null, "Feil format på noen felter, gjerne endre på dem", "Nummer Format", JOptionPane.ERROR_MESSAGE);
            }
        } // End of Handler method
    }// End of GuiHandler class

Then you can put your logic into a switch statement per button. That way you don't have to go through all those if statements looking for the source. I don't think you can use Button directly in a switch statement, but you could use the text in the button.

JeramyRR
  • 4,273
  • 3
  • 20
  • 20
  • Thank u, it work, but i think it may overlap with some other buttons with stitch-case if some of the buttons have the same name, as we compare the getText() of the button. Im still a novice in Java so if im wrong correct me ^^, – Huy Trần Apr 24 '15 at 21:41