为什么javafx中的Custom Combobox在关闭时会出现Nullpointerexception

时间:2018-04-22 20:31:48

标签: java javafx nullpointerexception custom-controls

我制作了一个自定义的组合框弹出窗口,我打算用于更大的项目,问题是,组合框的工作原理与它应该如此,但是当我关闭它,或触摸内部节点时,我得到一个NullPointerException。这是代码。

Main.java

package custompopup;

import java.io.IOException;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.DatePicker;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class Main extends Application {
     CustomComboBox cCBox;
     CustomComboBoxSkin cCBoxSkin;
     DecadeChooserfxController dc;

     public FXMLLoader fxmll;
     public static void main(String[]args) {
     launch(args);
}

   @Override
   public void start(Stage stage) throws Exception {

      AnchorPane pane = new AnchorPane();

      pane.getChildren().add(cCBox);

      stage.setScene(new Scene(pane));
      stage.setTitle("Custom Control");
      stage.setWidth(900);
      stage.setHeight(900);
      stage.show();

      cCBox.setOnMouseClicked(e->{ cCBoxSkin.showOrHide();

   });
} 


 public void init() {

    cCBox = new CustomComboBox();
    cCBoxSkin = (CustomComboBoxSkin) cCBox.getComboBoxSkin();
    cCBox.setMinSize(100,10);
    cCBox.setLayoutX(140);
    cCBox.setLayoutY(80);

  }
} 

CustomComboboxSkin.java

package custompopup;

import java.io.IOException;

import javafx.fxml.FXMLLoader;
import javafx.scene.Node;

import javafx.scene.control.ComboBoxBase;

import javafx.scene.control.TextField;
import javafx.scene.control.skin.ComboBoxPopupControl;
import javafx.scene.layout.Pane;

import javafx.util.StringConverter;

public class CustomComboBoxSkin<Year> extends ComboBoxPopupControl<Year> {
     public CustomComboBox<Year> customComboBox;
     public Pane pane;
     public DecadeChooserfxController dcc;
     public FXMLLoader fxmll;

     public CustomComboBoxSkin(ComboBoxBase<Year> cCB) {
         super(cCB);
         customComboBox = (CustomComboBox<Year>) cCB;
         getContentController();
     }

     @Override
     protected StringConverter<Year> getConverter() {
        // TODO Auto-generated method stub
         return null;
     }

     @Override
     protected TextField getEditor() {
       // TODO Auto-generated method stub
       return null;
     }

     @Override
     protected Node getPopupContent() {
        setContent();
        return pane; 
     }

     public void setContent() {
         pane = new Pane();
         pane.setMinSize(50.5, 50.5);
         pane.setStyle("-fx-background-color: slateblue");

     }


     @Override
     public Node getDisplayNode() {
        // TODO Auto-generated method stub
        return null;
     }


     public void showOrHide() {
        if (this.customComboBox.isShowing()){
            this.customComboBox.hide();
           }else {
            this.customComboBox.show();
        }
    }
}

CustomCombobox.java

package custompopup;

import java.io.IOException;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Skin;

public class CustomComboBox<Year> extends ComboBox<Year> {
    public  CustomComboBoxSkin<Year> cCBoxSkin;

    public CustomComboBox() {
        cCBoxSkin = (CustomComboBoxSkin) this.createDefaultSkin();
    }

    @Override
    protected Skin<?> createDefaultSkin(){
        cCBoxSkin = new CustomComboBoxSkin<Year>(this);
        return cCBoxSkin;
    }


    public Skin getComboBoxSkin() {
       return this.cCBoxSkin;
    }   
}

为简单起见,我将窗格内容替换为仅填充了颜色的窗格,但结果仍然是相同的。关闭组合框后,我得到一个NPE。

Exception in the console 
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at javafx.controls/javafx.scene.control.skin.ComboBoxPopupControl.lambda$createPopup$5(Unknown Source)
at javafx.graphics/javafx.stage.PopupWindow.doAutoHide(Unknown Source)
at javafx.graphics/javafx.stage.PopupWindow$PopupEventRedirector.handleAutoHidingEvents(Unknown Source)
at javafx.graphics/javafx.stage.PopupWindow$PopupEventRedirector.handleRedirectedEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventRedirector.dispatchCapturingEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchCapturingEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventRedirector.redirectEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventRedirector.dispatchCapturingEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchCapturingEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.base/javafx.event.Event.fireEvent(Unknown Source)
at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Unknown Source)
at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1300(Unknown Source)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Unknown Source)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(Unknown Source)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
at javafx.graphics/com.sun.glass.ui.View.notifyMouse(Unknown Source)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(Unknown Source)
at java.base/java.lang.Thread.run(Unknown Source)

0 个答案:

没有答案