java.lang.NullPointerException在使用ObjectOutputStream时发生

huangapple 未分类评论66阅读模式
标题翻译

java lang NullPointerException when working with ObjectOutputStream

问题

我正在尝试将一些对象写入文件并读取。我的代码连接到一个在SceneBuilder中制作的应用程序,每当我按下按钮使writeLogicToFile运行时,就会出现NullPointerException。我已经检查了文件路径,确保它是正确的。

以下是我的方法:

public void writeLogicToFile(String fileName, GameLogic gameLogic) {
    try {
        ObjectOutputStream w = new ObjectOutputStream(new FileOutputStream(fileName));
        w.writeObject(gameLogic);
        w.close();
    } catch (FileNotFoundException e) {
        e.getStackTrace();
    } catch (IOException e) {
        e.getStackTrace();
    }
}

这是它的使用方式:

file.writeLogicToFile("/Users/eliasjoa/ntnu/objektprog/tdt4100-v2020-master/git/tdt4100-v2020-students/ovinger/src/app/LogicStorage", logic);

关于这个逻辑,是否有什么问题?我对尝试保存对象并从文件中检索它们还不熟悉。

以下是我尝试检索它的方式(尽管错误发生在写入部分,所以我从未到达读取部分):

public GameBoard readBoardFromFile(String fileName) {
    try {
        ObjectInputStream r= new ObjectInputStream(new FileInputStream(fileName));
        GameBoard board = (GameBoard) r.readObject();
        r.close();
        return board;
    } 
    catch (IOException | ClassNotFoundException e) {
        return null;
    }
}

并尝试这样保存它到一个对象:

this.board = file.readBoardFromFile("/Users/eliasjoa/ntnu/objektprog/tdt4100-v2020-master/git/tdt4100-v2020-students/ovinger/src/app/BoardStorage");

这是堆栈跟踪:

Mar 16, 2020 11:03:55 AM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 11.0.1 by JavaFX runtime of version 11-ea
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
...
Caused by: java.lang.reflect.InvocationTargetException
...
Caused by: java.lang.NullPointerException
	at ovinger/app.GameController.WritePressed(GameController.java:54)
	... 59 more
英文翻译

I am trying to write and read a couple objects to a file. My code is connected to an app made in scenebuilder and whenever I press the button making the writeLogicToFile run I get NullPointerException. I have checked the file path making sure it is correct.

Here is my method:

public void writeLogicToFile(String fileName, GameLogic gameLogic) {
		try {
			ObjectOutputStream w = new ObjectOutputStream(new FileOutputStream(fileName));
			w.writeObject(gameLogic);
			w.close();
		} catch (FileNotFoundException e) {
			e.getStackTrace();
		} catch (IOException e) {
			e.getStackTrace();
		}
	}

And this is how it gets used:

file.writeLogicToFile("/Users/eliasjoa/ntnu/objektprog/tdt4100-v2020-master/git/tdt4100-v2020-students/ovinger/src/app/LogicStorage", logic);

Is there anything wrong about my logic about this? I´m new to trying to save objects and retrieve them from files.

This is how I try to retrieve it (although the error happens in the writing so I have never reached the read part):

public GameBoard readBoardFromFile(String fileName) {
		try {
			ObjectInputStream r= new ObjectInputStream(new FileInputStream(fileName));
			GameBoard board = (GameBoard) r.readObject();
			r.close();
			return board;
		} 
		catch (IOException | ClassNotFoundException e) {
			return null;
		}
	}

And trying to save it to an object like this:

this.board=file.readBoardFromFile("/Users/eliasjoa/ntnu/objektprog/tdt4100-v2020-master/git/tdt4100-v2020-students/ovinger/src/app/BoardStorage");

Here is stacktrace:

Mar 16, 2020 11:03:55 AM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 11.0.1 by JavaFX runtime of version 11-ea
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
	at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1787)
	at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1670)
	at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
	at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
	at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
	at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
	at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
	at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
	at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
	at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
	at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
	at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
	at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
	at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
	at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
	at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8879)
	at javafx.controls/javafx.scene.control.Button.fire(Button.java:200)
	at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:206)
	at com.sun.javafx.scene.control.behavior.ButtonBehavior$$Lambda$269.0000000000000000.handle(Unknown Source)
	at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
	at com.sun.javafx.scene.control.inputmap.InputMap$$Lambda$224.0000000000000000.handle(Unknown Source)
	at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
	at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
	at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
	at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
	at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
	at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
	at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
	at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
	at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
	at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
	at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
	at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
	at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
	at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
	at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3851)
	at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1200(Scene.java:3579)
	at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849)
	at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2588)
	at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:397)
	at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:704)
	at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:434)
	at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda$510.0000000000000000.get(Unknown Source)
	at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:390)
	at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:433)
	at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
	at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
	at javafx.graphics/com.sun.glass.ui.mac.MacView.notifyMouse(MacView.java:127)
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:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:567)
	at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
	at jdk.internal.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:567)
	at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
	at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
	at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1784)
	... 48 more
Caused by: java.lang.NullPointerException
	at ovinger/app.GameController.WritePressed(GameController.java:54)
	... 59 more

huangapple
  • 本文由 发表于 2020年3月16日 17:32:35
  • 转载请务必保留本文链接:https://java.coder-hub.com/60703405.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定