java为什么javafx中的自定义组合框在关闭时会收到Nullpointerexception
我制作了一个自定义的组合框弹出窗口,我计划用于一个更大的项目。问题是,组合框的工作原理与预期的一样,但当我关闭它或触摸其中的节点时,我会得到一个NullPointerException。这是密码
梅因。爪哇
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);
}
}
定制Boxskin。爪哇
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();
}
}
}
自定义组合框。爪哇
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) 个答案