My ListView抛出空指针异常,但控制器连接到FXML。我看了,但看起来它连接到fxml和控制器。它确实调用了该方法,但是当它设置listview时会抛出错误。我已经尝试了很多不同的方法,但总是回到这里,我不知道是否有更好的方法来处理这个或我可以用来保存列表的另一个容器?</ p>
package Controler;
import javafx.beans.property.ListProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.collections.FXCollections;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import wiziwig.interfaces.Language;
import javafx.fxml.FXML;
import java.util.*;
public class WiziwigContentLoader {
Language language;
WizigHelper helper;
public WiziwigContentLoader() {
}
@FXML
Button Create;
@FXML
ListView comp;
@FXML
ListView added;
protected ListProperty<String> listProperty = new SimpleListProperty<>();
public Language getLanguage() {
return language;
}
public void setLanguage(Language language) {
this.language = language;
}
public void getList() {
List<String> strings = new ArrayList<>();
strings.add("hi");
strings.add("ME");
listProperty.set(FXCollections.observableArrayList(strings));
comp.setItems(listProperty);
}
public void setAddedComponentListView() {
added.setItems(FXCollections.observableArrayList(language.usedComponents));
}
public void create(){
language.runFile(language.createFile());
}
public void setComponent(){
comp.setOnMouseClicked((MouseEvent event)->
helper.switchToProp(event,"Proproties.fxml",getLanguage()));
}
}
<?import javafx.scene.AmbientLight?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<AnchorPane prefHeight="400.0" prefWidth="600.0"
xmlns="http://javafx.com/javafx/8.0.121"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="Controler.WiziwigContentLoader">
<children>
<BorderPane layoutX="2.0" prefHeight="400.0" prefWidth="600.0">
<left>
</left>
<top>
<AmbientLight color="CHARTREUSE" lightOn="true"
BorderPane.alignment="CENTER" />
</top>
<right>
<ListView fx:id="added" prefHeight="200.0" prefWidth="200.0"
BorderPane.alignment="CENTER" />
</right>
<bottom>
<HBox alignment="BOTTOM_RIGHT" prefHeight="43.0" prefWidth="600.0"
BorderPane.alignment="CENTER">
<children>
<Button fx:id="Create" mnemonicParsing="false"
onMouseClicked="#create" text="Create" />
</children>
</HBox>
</bottom>
<left>
<ListView fx:id="comp" prefHeight="200.0" prefWidth="200.0"
BorderPane.alignment="CENTER" />
</left>
</BorderPane>
</children>
</AnchorPane>
StackTrace:
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at Controler.WiziwigContentLoader.setBox(WiziwigContentLoader.java:56)
at Controler.WizigHelper.switchToWIZI(WizigHelper.java:36)
at Controler.StartWindowController.lambda$initialize$0(StartWindowController.java:36)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
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:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8413)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
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:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:432)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at
com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
处理完成,退出代码为0