提问人:yangttze 提问时间:6/6/2023 更新时间:6/6/2023 访问量:56
JavaFX 调用异常和 NullPointer 异常
javafx invocation exception and nullpointer exception
问:
我是 Javafx 的新手,在做一个小项目时遇到了一个问题。这个程序应该是一个小型调度程序,其中用户应该能够在 scene2 和 scene4 中创建事件,并在 scene3.fxml 中查看它们。在创建事件并且用户导航到 scene3.fxml 后,它应该将标签更改为事件列表(我尝试了 listView,但它有其他错误)。出于某种原因,每当我尝试访问“scene3.fxml”时,我都会收到下面的错误堆栈,即使我看到场景,它也没有按预期工作。我已确保所有fxids都是正确的,并且所有其他fxml文件都正常工作。 包应用; 场景控制器.java:
package application;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.time.LocalDate;
import java.util.ResourceBundle;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.scene.image.*;
import java.util.ArrayList;
public class SceneController {
private Stage stage;
private Scene scene;
private Parent root;
//Scene2
@FXML
private DatePicker myDatePicker;
@FXML
private Label dateLabel;
@FXML
private Label errorDate;
//Scene3
@FXML
private Label listLabel;
@FXML
private Label ScheduleLabel;
@FXML
private Button returnFrom3;
//Scene4
@FXML
private TextArea description;
@FXML
private TextField name;
@FXML
private Button eventSubmitter;
@FXML
private Label errorLabel;
public static ArrayList<String> events = new ArrayList<String>();
public static ArrayList<String> descriptions = new ArrayList<String>();
public static ArrayList<LocalDate> dates = new ArrayList<LocalDate>();
private LocalDate myDate;
ImageView myImageView;
String currentEvent;
Image myImage = new Image(getClass().getResourceAsStream("SchedulerLogo.PNG"));
/*
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
// TODO Auto-generated method stub
/*
listLabel.getItems().addAll(events);
listLabel.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> arg0, String arg1, String arg2) {
// TODO Auto-generated method stub
currentEvent = listLabel.getSelectionModel().getSelectedItem();
}
});
}
*/
public String getDate() {
try {
myDate = myDatePicker.getValue();
System.out.println(myDate.toString());
dates.add(myDate);
return myDate.toString();
}catch(NullPointerException e) {
System.out.println("NullPointerException e");
return "";
}catch(Exception e) {
System.out.println("exception e");
LocalDate myDate = myDatePicker.getValue();
dates.add(myDate);
System.out.println(myDate.toString());
return myDate.toString();
}
}
public void switchToScene1(ActionEvent event) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("fxml.fxml"));
stage = (Stage)((Node)event.getSource()).getScene().getWindow();
scene = new Scene(root);
stage.setScene(scene);
stage.show();
//myImageView.setImage(myImage);
}
public void switchToScene2(ActionEvent event) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("scene2.fxml"));
stage = (Stage)((Node)event.getSource()).getScene().getWindow();
scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public void loadEvents() {
for(int i =0; i<events.size(); i++) {
currentEvent = currentEvent + events.get(i);
}
listLabel.setText(currentEvent);
}
public void switchToScene3(ActionEvent event) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("scene3.fxml"));
stage = (Stage)((Node)event.getSource()).getScene().getWindow();
scene = new Scene(root);
stage.setScene(scene);
stage.show();
loadEvents();
}
public void submitDate(ActionEvent event) throws IOException {
if (myDate != null) {
System.out.println(myDate + "numba 2 ");
//dates.add();
Parent root = FXMLLoader.load(getClass().getResource("scene4.fxml"));
stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
scene = new Scene(root);
stage.setScene(scene);
stage.show();
} else {
errorDate.setVisible(true);
}
printArrays();
}
public void submitEvent(ActionEvent event) throws IOException{
String eventName = name.getText();
String eventDescription = description.getText();
if(eventName.length()>0) {
events.add(eventName);
descriptions.add(eventDescription);
errorLabel.setVisible(true);
errorLabel.setText("Your event has been submitted. You may now navigate back to the main menu.");
}else {
errorLabel.setVisible(true);
}
printArrays();
}
public void printArrays() {
System.out.println(events);
System.out.println(descriptions);
System.out.println(dates);
}
}
Main.java:
package application;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.image.*;
import javafx.scene.input.KeyCombination;
import javafx.scene.text.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javafx.application.Application;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.control.DatePicker;
import java.io.IOException;
import java.net.URL;
import java.time.LocalDate;
import java.util.ResourceBundle;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
import javafx.scene.image.*;
import java.util.ArrayList;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
try {
LocalDate e = LocalDate.now();
SceneController.events.add(" ");
SceneController.descriptions.add(" ");
SceneController.dates.add(e);
Parent root = FXMLLoader.load(getClass().getResource("fxml.fxml"));
Scene scene = new Scene(root);
String css = this.getClass().getResource("application.css").toExternalForm();
scene.getStylesheets().add(css);
Image icon = new Image("SchedulerLogo.PNG");
stage.getIcons().add(icon);
stage.setTitle("Scheduler");
stage.setWidth(1000);
stage.setHeight(700);
stage.setFullScreen(false);
stage.setFullScreenExitHint("press q to escape?");
stage.setFullScreenExitKeyCombination(KeyCombination.valueOf("q"));
stage.setScene(scene);
stage.show();
}catch(Exception e) {
e.printStackTrace();
}
}
}
scene3.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="1255.0" prefWidth="1299.0" style="-fx-background-color: pink;" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.SceneController">
<children>
<Label fx:id="ScheduleLabel" alignment="CENTER" layoutX="-4.0" layoutY="73.0" prefHeight="17.0" prefWidth="1309.0" style="-fx-background-color: pink;" text="Upcoming Events:">
<font>
<Font size="75.0" />
</font>
</Label>
<Button fx:id="returnFrom3" layoutX="625.0" layoutY="1136.0" mnemonicParsing="false" onAction="#switchToScene1" prefWidth="94.0" text="Main Menu" />
<Label fx:id="listLabel" alignment="TOP_LEFT" layoutY="301.0" prefHeight="821.0" prefWidth="1299.0" text="Label" />
</children>
</AnchorPane>
错误堆栈跟踪:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at [email protected]/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1858)
at [email protected]/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1726)
at [email protected]/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at [email protected]/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:232)
at [email protected]/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:189)
at [email protected]/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.ba[email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at [email protected]/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at [email protected]/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at [email protected]/javafx.event.Event.fireEvent(Event.java:198)
at [email protected]/javafx.scene.Node.fireEvent(Node.java:8944)
at [email protected]/javafx.scene.control.Button.fire(Button.java:203)
at [email protected]/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:207)
at [email protected]/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
at [email protected]/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
at [email protected]/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at [email protected]/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:232)
at [email protected]/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:189)
at [email protected]/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at [email protected]/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at [email protected]/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at [email protected]/javafx.event.Event.fireEvent(Event.java:198)
at [email protected]/javafx.scene.Scene$MouseHandler.process(Scene.java:3980)
at [email protected]/javafx.scene.Scene.processMouseEvent(Scene.java:1890)
at [email protected]/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2704)
at [email protected]/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:411)
at [email protected]/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:301)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at [email protected]/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:450)
at [email protected]/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424)
at [email protected]/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:449)
at [email protected]/com.sun.glass.ui.View.handleMouseEvent(View.java:551)
at [email protected]/com.sun.glass.ui.View.notifyMouse(View.java:937)
at [email protected]/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at [email protected]/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:185)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:72)
at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at [email protected]/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:270)
at [email protected]/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:84)
at [email protected]/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1853)
... 46 more
Caused by: java.lang.NullPointerException: Cannot invoke "javafx.scene.control.Label.setText(String)" because "this.listLabel" is null
at hellofx/application.SceneController.loadEvents(SceneController.java:157)
at hellofx/application.SceneController.switchToScene3(SceneController.java:166)
... 57 more
我尝试重新创建fxml文件,我认为所有库和构建路径都井井有条,我不知道发生了什么。请帮忙。
答: 暂无答案
评论