gpt4 book ai didi

java - 在 Controller JavaFX 的初始化方法中获取 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 09:44:34 26 4
gpt4 key购买 nike

以下是我在 Controller 类的初始化方法中遇到的 NullPointerException:

`Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
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.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:8411)
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.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:352)
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$354(GlassViewEventHandler.java:388)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
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$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
... 48 more
Caused by: javafx.fxml.LoadException:
file:/C:/Users/W7/Documents/NetBeansProjects/PivotNSE/dist/run2050028213/PivotNSE.jar!/pivotnse/popupFXML.fxml

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at pivotnse.PivotFXController.handleLoadButton(PivotFXController.java:173)
... 58 more
Caused by: java.lang.NullPointerException
at pivotnse.PivotFXController.initialize(PivotFXController.java:294)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
... 66 more`

我想做的是,当单击 myLoadButton 时,会弹出一个新窗口并在新窗口的标签中显示当前正在获取的所有符号“已下载”! (有点像对用户的“实时更新”)。只是为了让用户知道发生了什么。以下是代码:

 public class PivotFXController implements Initializable {

final ObservableList<GetPivots> data = FXCollections.observableArrayList();

@FXML
private TableView<GetPivots> pivotTable;

@FXML
private TableColumn<GetPivots, String> symbolColumn;
@FXML
private TableColumn<GetPivots, String> pivotColumn;
@FXML
private TableColumn<GetPivots, String> r1Column;
@FXML
private TableColumn<GetPivots, String> r2Column;

@FXML
private TableColumn<GetPivots, String> r3Column;

@FXML
private TableColumn<GetPivots, String> s1Column;
@FXML
private TableColumn<GetPivots, String> s2Column;
@FXML
private TableColumn<GetPivots, String> s3Column;
@FXML
private Label statusLabel;
@FXML
private Button myLoadButton;
@FXML
private Button doneButton;

@FXML
private TextField symbolField;

private String SYMBOLS_FILE = "SYMBOLS.txt";

private static List<String> symbolList = new ArrayList<String>();


private boolean handleSymbol(String symbol) {
System.out.println(symbol + " is the input");
// statusLabel.setText("Downloading... " + symbol);
FetchDataNSE fdn = new FetchDataNSE();

if (fdn.getDataNSE(symbol).size() > 0) {
CalculatePivot cp = new CalculatePivot(fdn.getDataNSE(symbol));
GetPivots gp = new GetPivots(cp);
data.add(gp);
// statusLabel.setText(symbol + " " + "Downloaded!");
return true;
}
return false;
}



@FXML
private void handleLoadButton(ActionEvent event) throws IOException {
Stage stage;
Parent root;
if (event.getSource() == myLoadButton) {
//get reference to the button's stage
stage = (Stage) myLoadButton.getScene().getWindow();
//load up OTHER FXML document
root = FXMLLoader.load(getClass().getResource("popupFXML.fxml"));// <-- ****this line is causing problem**** I am getting NULLPOINTEREXCEPTION from 1st line in `initialize` method WHY?
stage.setScene(new Scene(root));
stage.setTitle("DOWNLOADING STATUS");
stage.initModality(Modality.APPLICATION_MODAL);
stage.showAndWait();
loadSymbols();

} else {
stage = (Stage) doneButton.getScene().getWindow();
root = FXMLLoader.load(getClass().getResource("pivotFXGUI.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}


}

private void loadSymbols() {
data.clear();
symbolList.clear();

BufferedReader br = null;

try {

String sCurrentLine;

br = new BufferedReader(new FileReader(SYMBOLS_FILE));

while ((sCurrentLine = br.readLine()) != null) {
symbolList.add(sCurrentLine.trim());
}

} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}

for (String sym : symbolList) {
statusLabel.setText("Downloading... " + sym);
handleSymbol(sym);
}

statusLabel.setText("LOADING SYMBOLS DONE!");
}




@Override
public void initialize(URL url, ResourceBundle rb) {
// 1st line below gives NullPointerException! why does that happen?
symbolColumn.setCellValueFactory(new PropertyValueFactory<>("symbolProperty"));
pivotColumn.setCellValueFactory(new PropertyValueFactory<>("pivotProperty"));
r1Column.setCellValueFactory(new PropertyValueFactory<>("r1Property"));
r2Column.setCellValueFactory(new PropertyValueFactory<>("r2Property"));
r3Column.setCellValueFactory(new PropertyValueFactory<>("r3Property"));
s1Column.setCellValueFactory(new PropertyValueFactory<>("s1Property"));
s2Column.setCellValueFactory(new PropertyValueFactory<>("s2Property"));
s3Column.setCellValueFactory(new PropertyValueFactory<>("s3Property"));
pivotTable.setItems(data);

// myLoadButton.fire();
// pivotTable.getColumns().addAll(symbolColumn,s3Column, s2Column,s1Column, pivotColumn, r1Column, r2Column, r3Column );

}


}
`

为什么“initialize”方法中的第一行抛出 NullPointerException!?我在 handleLoadButton(..) 方法和 initialize(..) 方法中错误做什么?当我从 initialize 方法中删除所有内容时,当我单击 myLoadButton 时,程序会打开新窗口,但是当调用 loadSymbols() 方法时,会发生 NullPointerException...

还有:::在pivotnse.PivotFXController.handleLoadButton(PivotFXController.java:173)
... 58 更多
引起原因:java.lang.NullPointerException
在pivotnse.PivotFXController.initialize(PivotFXController.java:294)
第 173 行如下:::root = FXMLLoader.load(getClass().getResource("popupFXML.fxml"));第 294 行是 initialize() 方法中的第一行。

最佳答案

可能“symbolColumn”尚未初始化。也许您忘记或拼错了 fxml 文件中的 fx:id。

关于java - 在 Controller JavaFX 的初始化方法中获取 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38172353/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com