我一直在编写一个FXML
GUI应用程序,它从几个textFields读取内容并将其写入文件。但我坚持这个烦人的错误:
当我尝试将值保存到文件时,它会抛出InvocationTargetException
。
经过一些研究后,我发现错误位于<code>silaTf.getText()</code>
,其中silaTf
是textField
。
代码:
package drdtools;
import java.net.URL;
//... other imports
public class FXMLDocumentController implements Initializable {
@FXML
//NASTAVENI
//bonusy nebo postihy
private TextField silaTf;
private TextField obratTf;
private TextField odolTf;
private TextField intelTf;
private TextField charisTf;
//zbraně
//-zblizka
/*
uc=utocne cislo
utc=utocnost
oc=obranne cislo
zb=zbran na blozku
zs=zbran strelecka
*/
private TextField uczbTf;
private TextField utczbTf;
private TextField oczbTf;
//-strelecke
private TextField uczsTf;
private TextField utczsTf;
//zbroj
private TextField kvalitaTf;
//ostatni
private TextField maxzivTf; //max. zivotu
private TextField nosnostTf;
private TextField zivotuTf;
//POZNAMKY
private HTMLEditor poznamkyHTML;
@FXML
public void handleNacistAction() {
System.out.println("kliknuto na nacist");
System.out.println("nacitani hodnot...");
}
@FXML
public void handleUlozitAction() {
System.out.println("kliknuto na ulozit");
System.out.println("ukladani...");
System.out.println("ukladani nastaveni");
System.out.println("Otevirani souboru ke cteni");
String jmenoSouboru = "nastaveni.txt";
String jmenoPoznamky = "poznamky.html";
try {
try (BufferedWriter bafr = new BufferedWriter(new FileWriter(jmenoSouboru,true))) {
System.out.println("Soubor otevren.");
System.out.println("Zapisuji do souboru " + jmenoSouboru);
System.out.println("zapisuji bonusy a postihy...");
bafr.write(silaTf.getText());
bafr.newLine();
bafr.write(obratTf.getText()); //here is an error
bafr.newLine();
//... writing another values to file
System.out.println("uzavirani souboru...");
bafr.flush();
bafr.close();
System.out.println("zapis nastaveni probehl uspesne.");
}
} catch (IOException e) {
System.out.println("Stala se chyba:" + e);
}
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
输出:
Kliknuto na Nacist
nacitani hodnot...
kliknuto na ulozit
ukladani...
ukladani nastaveni
Exception in thread "JavaFX Application Thread"
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
Otevirani souboru ke cteni
Soubor otevren.
Zapisuji do souboru nastaveni.txt
zapisuji bonusy a postihy...
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)
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:1771)
... 48 more
Caused by: java.lang.NullPointerException
at drdtools.FXMLDocumentController.handleUlozitAction(FXMLDocumentController.java:80)
... 58 more
对不起语法错误和非英语println
,但英语不是我的主要语言而且我制作非英语应用程序。
对不起。
谢谢你的回答
再见。