简体   繁体   English

JavaFX事件处理程序nullpointerException

[英]JavaFX eventhandler nullpointerException

I have a Gui with several different classes which uses the same eventhandler. 我有一个带有几个不同类的Gui,它们使用相同的事件处理程序。 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. 我似乎无法找到解决方案的问题是,当事件处理程序正在寻找正确的方法调用并偶然发现尚未初始化的按钮时,如何避免获得nullpointerexception。 I have a couple of buttons that will only be initialized when the user goes into a createAccountGui stage. 我有几个仅在用户进入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);
    } 

Unless CreateAccGui has been initialized, whenever it comes to the cAG.getRegistrate() I get a nullpointerexception. 除非CreateAccGui已初始化,否则无论何时涉及cAG.getRegistrate(),我都会得到一个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

Add a null check to the code that uses cAG 向使用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 &&符号短路如果是假的,绝不会达到cAG.getRegistrate()如果cAG为null

BTW JOptionPane is part of swing, and java 8 update 40 came out with a new JavaFX dialog API BTW JOptionPane是摆动的一部分,并且Java 8 update 40带有新的JavaFX对话框API

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. 然后,您可以将逻辑放入每个按钮的switch语句中。 That way you don't have to go through all those if statements looking for the source. 这样,您不必遍历所有if语句寻找源代码。 I don't think you can use Button directly in a switch statement, but you could use the text in the button. 我认为您不能在switch语句中直接使用Button,但是可以在button中使用文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM