如何解决Java运行时错误以及加载异常错误
您好,当我尝试运行在oracle场景构建器中创建的fxml文件时遇到此错误,该fxml文件使用每个按钮的正确名称以及正确标记的控制器类进行了标记,我的控制代码问题中的错误看起来像这样
package prog24178;
import javafx.collections.FXCollections;
import java.util.ArrayList;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.control.Alert.AlertType;
import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;
import javafx.scene.control.ListView;
public class DataBaseController implements Config{
@FXML TextField txt1,txt2,txt3,txt4;
@FXML Button b1,b2,b3,b4,b5;
@FXML ListView<String> list;
@FXML private ArrayList<String> stringItems = new ArrayList<String>();
@FXML private ArrayList<Item> items = new ArrayList<Item>();
@FXML
public void add(ActionEvent e) {
int id = Integer.parseInt(txt1.getText());
String item = txt2.getText();
int quantity = Integer.parseInt(txt3.getText());
double price = Double.parseDouble(txt4.getText());
items.add(new Item(id,item,quantity,price));
list.getItems().clear();
stringItems.clear();
for(Item item1 : items) stringItems.add(item1.toString());
ObservableList<String> items = FXCollections.observableArrayList(stringItems);
list.setItems(items);
}
@FXML
public void load(ActionEvent e) {
items.clear();
stringItems.clear();
list.getItems().clear();
File f = new File(FILE);
try {
if(!f.exists()) f.createNewFile();
Scanner lines = new Scanner(f);
if(!lines.hasNextLine()) {
Alert alert = new Alert(AlertType.INFORMATION,"data file is empty\nenter car and click the save button");
alert.showAndWait();
}
else {
while(lines.hasNextLine()) {
Scanner line = new Scanner(lines.nextLine());
line.useDelimiter(":\\s*");
items.add(new Item(line.nextInt(),line.next(),line.nextInt(),line.nextDouble()));
line.close();
}
}
lines.close();
for(Item item1 : items)
list.getItems().add(item1.toString());
//stringCars.add(car.toString());
//ObservableList<String> items = FXCollections.observableArrayList(stringCars);
//list.setItems(items);
} catch (Exception e1) {
e1.printStackTrace();
}
}
@FXML
public void save(ActionEvent e) {
File f = new File(FILE);
try {
if(!f.exists()) f.createNewFile();
PrintWriter pw = new PrintWriter(f);
for(Item c : items)
pw.printf("%s:%s:%s:%d:%b\n",c.getID(),c.getItem(),c.getQuantity(),c.getPrice());
pw.close();
} catch (Exception e1) {
e1.printStackTrace();
}
Alert alert = new Alert(AlertType.INFORMATION,"data is saved in a file\nyou can close the \napplication now");
alert.showAndWait();
}
@FXML
public void modify(ActionEvent e) {
}
@FXML
public void remove(ActionEvent e) {
}
}
RunApp类
package prog24178;
import java.net.URL;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class RunApp extends javafx.application.Application implements Config{
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage ps) throws Exception {
URL res = this.getClass().getResource("control.fxml");
Parent scene = FXMLLoader.load(res);
Scene ourscene = new Scene(scene);
ps.setScene(ourscene);
ps.setTitle("Items Database");
ps.show();
}
}
已实现的配置界面
以及完整的错误消息
Exception in Application start method
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 com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
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.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: javafx.fxml.LoadException: Error resolving onAction='add',either the event handler is not in the Namespace or there is an error in the script.
/C:/Users/Canada%20Computers/workspace/Assign6/bin/prog24178/control.fxml:44
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
at javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:610)
at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:770)
at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532)
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 prog24178.RunApp.start(RunApp.java:17)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:186)
... 1 more
Exception running application prog24178.RunApp
FXML代码
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="384.0" prefWidth="664.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="prog24178.RunApp">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="130.0" minWidth="10.0" prefWidth="51.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="276.0" minWidth="10.0" prefWidth="34.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="253.0" minWidth="0.0" prefWidth="39.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="307.0" minWidth="10.0" prefWidth="203.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="446.0" minWidth="10.0" prefWidth="66.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="446.0" minWidth="10.0" prefWidth="75.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="446.0" minWidth="10.0" prefWidth="51.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="446.0" minWidth="10.0" prefWidth="148.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints />
<RowConstraints maxHeight="48.0" minHeight="10.0" prefHeight="48.0" />
<RowConstraints maxHeight="48.0" minHeight="0.0" prefHeight="40.0" />
<RowConstraints maxHeight="44.0" minHeight="8.0" prefHeight="32.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="274.0" minHeight="10.0" prefHeight="32.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="274.0" minHeight="10.0" prefHeight="201.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="123.0" minHeight="10.0" prefHeight="31.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label alignment="CENTER_RIGHT" text="ID:" GridPane.halignment="CENTER" GridPane.rowIndex="2" />
<TextField fx:id="txt1" prefHeight="25.0" prefWidth="46.0" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<Label text="Item:" GridPane.columnIndex="2" GridPane.halignment="CENTER" GridPane.rowIndex="2" />
<TextField fx:id="txt2" GridPane.columnIndex="3" GridPane.rowIndex="2" />
<Label prefHeight="17.0" prefWidth="57.0" text="Quanitity:" GridPane.columnIndex="4" GridPane.halignment="CENTER" GridPane.rowIndex="2" />
<TextField fx:id="txt3" GridPane.columnIndex="5" GridPane.rowIndex="2" />
<Label text="Price:" GridPane.columnIndex="6" GridPane.halignment="CENTER" GridPane.rowIndex="2" />
<TextField fx:id="txt4" GridPane.columnIndex="7" GridPane.rowIndex="2">
<GridPane.margin>
<Insets />
</GridPane.margin>
</TextField>
<HBox prefHeight="29.0" prefWidth="516.0" spacing="10.0" GridPane.columnIndex="3" GridPane.columnSpan="2147483647" GridPane.rowIndex="3">
<children>
<Button fx:id="b1" mnemonicParsing="false" onAction="add" text="Add Item" />
<Button fx:id="b2" mnemonicParsing="false" onAction="#modify" text="Modify Item" />
<Button fx:id="b3" mnemonicParsing="false" onAction="#remove" prefHeight="25.0" prefWidth="107.0" text="Remove Item" />
</children>
<GridPane.margin>
<Insets top="10.0" />
</GridPane.margin>
</HBox>
<HBox prefHeight="100.0" prefWidth="200.0" GridPane.columnSpan="2147483647" GridPane.rowIndex="5">
<children>
<ListView fx:id="list" prefHeight="201.0" prefWidth="627.0">
<padding>
<Insets left="20.0" right="10.0" />
</padding>
</ListView>
</children>
</HBox>
<Label text="Items:" GridPane.halignment="CENTER" GridPane.rowIndex="4" />
<HBox prefHeight="100.0" prefWidth="200.0" spacing="10.0" GridPane.columnIndex="3" GridPane.columnSpan="2147483647" GridPane.rowIndex="6">
<children>
<Button fx:id="b4" mnemonicParsing="false" onAction="#load" text="Load Data From File" />
<Button fx:id="b5" mnemonicParsing="false" onAction="#save" text="Save Data To File" />
</children>
</HBox>
<HBox prefHeight="100.0" prefWidth="200.0" GridPane.columnIndex="3" GridPane.columnSpan="2147483647" GridPane.rowIndex="1">
<children>
<Label alignment="CENTER" prefHeight="48.0" prefWidth="318.0" text="Database of Items">
<font>
<Font size="26.0" />
</font>
</Label>
</children>
</HBox>
</children>
<opaqueInsets>
<Insets />
</opaqueInsets>
</GridPane>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。