package application;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
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.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
public class PrinterController implements Initializable {
@FXML private Button ButtonHome;
@FXML private TableView < Printer > table;
@FXML private TableColumn < Printer, String > Make;
@FXML private TableColumn < Printer, Integer > Model;
@FXML private TableColumn < Printer, Integer > SerialNO;
@FXML private TableColumn < Printer, Integer > IP;
@FXML private TableColumn < Printer, Integer > MAC;
@FXML private TableColumn < Printer, String > Cartridge;
@FXML private TableColumn < Printer, String > Date;
@FXML private TableColumn < Printer, String > Other;
@FXML private TableColumn < Printer, String > Remarks;
@FXML private TextField makeInput;
@FXML private TextField modelInput;
@FXML private TextField serialInput;
@FXML private TextField ipInput;
@FXML private TextField macInput;
@FXML private TextField cartridgeInput;
@FXML private TextField dateInput;
@FXML private TextField otherInput;
@FXML private TextField remarksInput;
@FXML private Button done;
@FXML private Button buttonadd1;
//Executing Home Button
public ObservableList < Printer > list = FXCollections.observableArrayList(
new Printer("HP", 230, 001, 192168, 888, "HP", "24", "-", "New")
);
public void AddforPrinter(ActionEvent event) throws Exception {
Stage primaryStage = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("/application/PrinterAdd.fxml"));
Scene scene = new Scene(root, 400, 400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
//primaryStage.
}
// public void AddforPrinter(ActionEvent event) throws Exception
// {
// ((Node)(event.getSource())).getScene().getWindow().hide();
// Stage primaryStage=new Stage();
// Parent root = FXMLLoader.load(getClass().getResource("/application/PrinterAdd.fxml"));
// Scene scene = new Scene(root,500,500);
// scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
// primaryStage.setScene(scene);
// primaryStage.show();
//
// }
public void Home(ActionEvent event) throws Exception {
((Node)(event.getSource())).getScene().getWindow().hide();
Stage primaryStage = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("/application/Main.fxml"));
Scene scene = new Scene(root, 448, 388);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
}
//Executing Add Button
@Override
public void initialize(URL location, ResourceBundle resources) {
Make.setCellValueFactory(new PropertyValueFactory < Printer, String > ("Make"));
Model.setCellValueFactory(new PropertyValueFactory < Printer, Integer > ("Model"));
SerialNO.setCellValueFactory(new PropertyValueFactory < Printer, Integer > ("SerialNO"));
IP.setCellValueFactory(new PropertyValueFactory < Printer, Integer > ("IP"));
MAC.setCellValueFactory(new PropertyValueFactory < Printer, Integer > ("MAC"));
Cartridge.setCellValueFactory(new PropertyValueFactory < Printer, String > ("Cartridge"));
Date.setCellValueFactory(new PropertyValueFactory < Printer, String > ("Date"));
Other.setCellValueFactory(new PropertyValueFactory < Printer, String > ("Other"));
Remarks.setCellValueFactory(new PropertyValueFactory < Printer, String > ("Remarks"));
table.setItems(list);
}
public void onDone(ActionEvent event) {
// This code is for supplying the data
Printer entry = new Printer(makeInput.getText(), Integer.parseInt(modelInput.getText()), Integer.parseInt(serialInput.getText()),
Integer.parseInt(ipInput.getText()), Integer.parseInt(macInput.getText()), cartridgeInput.getText(), dateInput.getText(),
otherInput.getText(), remarksInput.getText());
// This code is for fetching the data
list.add(entry);
// This code is for clearing the text fields
}
}
因此,每次单击“添加”按钮时,都会抛出一个空的ppinter异常。它之前也给了我类似的错误,但我能够通过在父root中的fxml文件之前给出包名来修复它。
package application;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
public class Printer {
private final SimpleStringProperty make;
private final SimpleIntegerProperty model;
private final SimpleIntegerProperty serialNO;
private final SimpleIntegerProperty IP;
private final SimpleIntegerProperty MAC;
private final SimpleStringProperty Cartridge;
private final SimpleStringProperty date;
private final SimpleStringProperty other;
private final SimpleStringProperty remarks;
public Printer(String make, Integer model, Integer serialNO, Integer IP, Integer MAC, String cartridge, String date,
String other, String remarks) {
super();
this.make = new SimpleStringProperty(make);
this.model = new SimpleIntegerProperty(model);
this.serialNO = new SimpleIntegerProperty(serialNO);
this.IP = new SimpleIntegerProperty(IP);
this.MAC = new SimpleIntegerProperty(MAC);
this.Cartridge = new SimpleStringProperty(cartridge);
this.date = new SimpleStringProperty(date);
this.other = new SimpleStringProperty(other);
this.remarks = new SimpleStringProperty(remarks);
}
public String getMake() {
return make.get();
}
public Integer getModel() {
return model.get();
}
public Integer getSerialNO() {
return serialNO.get();
}
public Integer getIP() {
return IP.get();
}
public Integer getMAC() {
return MAC.get();
}
public String getCartridge() {
return Cartridge.get();
}
public String getDate() {
return date.get();
}
public String getOther() {
return other.get();
}
public String getRemarks() {
return remarks.get();
}
}
每当我在我的打印场景上点击添加它就会给我错误,错误与运行时和零点异常有关。我不知道如何解决这个问题。我已经尝试了很多并且每次都失败了
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: 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.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: 381)
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$354(GlassViewEventHandler.java: 417)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java: 389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java: 416)
at com.sun.glass.ui.View.handleMouseEvent(View.java: 555)
at com.sun.glass.ui.View.notifyMouse(View.java: 937)
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)
...45 more
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java: 3207)
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 application.PrinterController.AddforPrinter(PrinterController.java: 58)
...55 more
答案 0 :(得分:0)
删除Initializable
接口实现并从中更改方法签名:
class YourClass implements Initializable {
@Override
public void initialize(URL location, ResourceBundle resources) {
//your code
table.setItems(list);
}
}
到这个
class YourClass {
@Override
public void initialize() {
//your code
table.setItems(list);
}
}