添加到组合框

时间:2015-11-03 01:58:26

标签: object javafx combobox nullpointerexception abstract

我创建了一个javafx gui。我还有一个自定义类,每次按下添加对象按钮时,我都会使用它来创建该类的对象。 当我创建类的对象并将对象打印到控制台时,调用toString()方法。但是当我添加一个组合框并选择它时,java会抛出一个NullPointerException。我使用了setOnAction方法来组合框,但它甚至没有查看handle方法。任何人都可以看到堆栈跟踪中发生了什么?

火星级是一个抽象类。火星人是红色或绿色。

protected ComboBox<Martian> currentMartians = new ComboBox<>(); 

public class MartianSelectorHandler implements EventHandler<ActionEvent>{

    @Override
    public void handle(ActionEvent event) {
        // TODO Auto-generated method stub
        System.out.println("Inside combobox handler");

        Martian m = (Martian) currentMartians.getValue();
        txtaResults.setText(m.toString());

    }

public Pane buildRightPane(){
    VBox leftPane = new VBox();
    leftPane.setPadding(new Insets(15,5,5,0));
    leftPane.setAlignment(Pos.TOP_LEFT);
    Label label = new Label("Curent Martians");
    currentMartians.setPromptText("Martians");
    leftPane.getChildren().addAll(label,currentMartians);

    MartianSelectorHandler comboBox = new MartianSelectorHandler();
    currentMartians.setOnAction(comboBox);
    return leftPane;
}    

堆栈跟踪

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at martian_stuff.Martian.equals(Martian.java:20)
at javafx.scene.control.MultipleSelectionModelBase.select(MultipleSelectionModelBase.java:385)
at javafx.scene.control.MultipleSelectionModelBase.clearAndSelect(MultipleSelectionModelBase.java:348)
at javafx.scene.control.ListView$ListViewBitSetSelectionModel.clearAndSelect(ListView.java:1400)
at com.sun.javafx.scene.control.behavior.CellBehaviorBase.simpleSelect(CellBehaviorBase.java:260)
at com.sun.javafx.scene.control.behavior.CellBehaviorBase.doSelect(CellBehaviorBase.java:224)
at com.sun.javafx.scene.control.behavior.CellBehaviorBase.mousePressed(CellBehaviorBase.java:150)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:95)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3758)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3486)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2495)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:350)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$350(GlassViewEventHandler.java:385)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda$315/1947016627.get(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:404)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:384)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:927)

Martian类是一个jar文件。但是equals方法看起来像是

public boolean equals(Object o){
    //boolean returnValue = ((getId() == ((Martian) o).getId())); ? true : false);
    return ((getId() == ((Martian) o).getId()));
}

这些方法添加到组合框

    public Pane buildRow2(){
    GridPane row2 = new GridPane();
    row2.setHgap(10);
    row2.setVgap(10);
    row2.setPadding(new Insets(25, 25, 25, 25));

    Button addGM = new Button("Add GM");
    addGM.setOnAction(e->{
        System.out.println("Adding Green Martian");
        int newId = Integer.parseInt(txtfId1.getText());
        int newVolume = Integer.parseInt(txtfVolume.getText());
        GreenMartian gm = new GreenMartian(newId);
        gm.setVolume(newVolume);

        if (mm.addMartian(gm) == true){
            mm.addMartian(gm);
            currentMartians.getItems().add(gm);
            txtaResults.appendText("Green Martian ID: " + newId + " added.\n");
        }
        else{System.out.println("Martian not added");}

    });

    Button addRM = new Button("Add RM");
    addRM.setOnAction(e->{
        System.out.println("Adding Red Martian");
        int newId = Integer.parseInt(txtfId1.getText());
        int newVolume = Integer.parseInt(txtfVolume.getText());
        RedMartian rm = new RedMartian(newId);
        rm.setVolume(newVolume);
        if (mm.addMartian(rm) == true){
            mm.addMartian(rm);
            currentMartians.getItems().add(rm);
            txtaResults.appendText("Red Martian ID: " + newId + " added.\n");
        }
        else{
            txtaResults.appendText("**Martian not added. Duplicate ID.**");
            System.out.println("Cant add");
        }
    });

    Button groupSpeak = new Button("Group Speak");
    groupSpeak.setOnAction(e->{
        String speak = mm.groupSpeak();
        txtaResults.setText(speak+"\n");
    });

    Button groupTele = new Button("Group Teleport");
    Label stringDest = new Label("Enter Destination: ");
    txtfDest = new TextField();
    stringDest.setVisible(false);
    txtfDest.setVisible(false);
    Button okay1 = new Button("Okay!");
    okay1.setOnAction(e->{
        if(txtfDest != null)
            txtaResults.setText(mm.groupTeleport(txtfDest.getText())+"\n");
        stringDest.setVisible(false);
        txtfDest.setVisible(false);
        okay1.setVisible(false);

    });
    okay1.setVisible(false);

    groupTele.setOnAction(e->{
        stringDest.setVisible(true);
        txtfDest.setVisible(true);
        okay1.setVisible(true);


    });
    row2.add(addGM, 0, 0);
    row2.add(addRM,1,0);
    row2.add(groupSpeak,0,1);
    row2.add(groupTele, 1,1);
    row2.add(stringDest,2,0);
    row2.add(txtfDest, 2,1);
    row2.add(okay1, 2, 2);

    return row2;

}

0 个答案:

没有答案