JavaFX eventhandler nullpointerException

时间:2015-04-24 19:50:38

标签: java user-interface javafx nullpointerexception eventhandler

我有一个Gui,它有几个不同的类,它们使用相同的事件处理程序。我无法找到解决方案的问题是,当事件处理程序正在寻找正确的方法调用并偶然发现尚未初始化的按钮时,如何避免获得nullpointerexception。 我有几个按钮,只有在用户进入createAccountGui阶段时才会初始化。

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);
    } 

除非初始化CreateAccGui,否则每当涉及到cAG.getRegistrate()时,我都会得到nullpointerexception。有没有人有一种聪明的方法来处理这类问题,而不必将其分成几个不同的处理程序。

@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

2 个答案:

答案 0 :(得分:0)

对使用cAG

的代码添加空检查
     /* right here */
else if(  cAG != null  && e.getSource() == cAG.getRegistrate()) {

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

&&符号short circuits如果为假,如果cAG.getRegistrate()为空则永远不会到达cAG

BTW JOptionPane是swing的一部分,java 8 update 40推出了新的JavaFX对话框API

答案 1 :(得分:0)

在事件处理程序的开头获取事件的来源并将其设置为变量:

@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

然后,您可以将逻辑放入每个按钮的switch语句中。这样你就不必经历所有那些寻找源的if语句。我认为您不能直接在switch语句中使用Button,但您可以使用按钮中的文本。