【问题标题】:deserializing objects run issue反序列化对象运行问题
【发布时间】:2020-10-04 04:39:44
【问题描述】:

运行代码后出现以下错误:

关于导致该问题的原因以及如何解决它的任何想法?

这里是Restore()

public void Restore() {
        try
        {
            ObjectInputStream is = new ObjectInputStream(new FileInputStream("dump.out"));
            GreenhouseControls greenhouseControls = (GreenhouseControls)  is.readObject(); // Line 343
            is.close();

            Fixable fixable = getFixable(greenhouseControls.getError());
            if(fixable != null)
                fixable.fix();
            else
                System.out.println("Nothing to fix");
        }

        catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

如果需要完整代码: https://repl.it/repls/MajesticDarkredCrash#Main.java

example3.txt:

Event=ThermostatNight,time=0
Event=LightOn,time=2000
Event=WaterOff,time=10000
Event=ThermostatDay,time=12000
Event=Bell,time=9000,rings=5
Event=WaterOn,time=6000
Event=LightOff,time=4000
Event=Terminate,time=20000
Event=FansOn,time=7000
Event=WindowMalfunction,time=15000
Event=FansOff,time=8000

我使用 java GreenhouseCOntrols -f emample3.txt 然后 java GreenhouseControls -d dump.out 来运行程序。

【问题讨论】:

  • 欢迎来到 Stack Overflow。请通过tour 了解 Stack Overflow 的工作原理,并阅读How to Ask 了解如何提高问题的质量。然后edit你的问题包含你拥有的源代码(特别是GreenhouseControls类)作为minimal reproducible example,可以被其他人编译和测试。
  • 您链接到的代码显然不是您正在运行的代码,因为您的代码中没有任何地方可以将输出序列化为文件。此外,您的评论 line 343 出现在第 325 行。
  • @LukeWoodward 它应该是相同的代码,当我通过 repli 时,我刚刚删除了一些 cmets。但是你说有什么东西可以序列化到一个文件中是什么意思?
  • 当我说没有任何东西可以将输出序列化到文件时,我的意思是没有任何东西可以生成dump.out 文件。您正在加载的dump.out 文件来自哪里?

标签: java serializable


【解决方案1】:

无论您尝试在该行中将什么对象类型转换为 GreenhouseControls

GreenhouseControls greenhouseControls = (GreenhouseControls) is.readObject();

不工作。因此,您似乎正在阅读与 GreenhouseControls 不匹配的对象

能否也提供文件内容?

【讨论】:

  • 我认为这不是问题所在。当它写入对象而不是读取它时会引发错误。此外,强制转换不应抛出 NotSerializableException
  • 我在问题和使用的运行命令中添加了examples3.txt,这些应该向您显示完整的输出以及dump.out 我似乎无法阅读它在这里发布抱歉
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-10
  • 1970-01-01
  • 2022-01-25
  • 2011-07-23
  • 1970-01-01
相关资源
最近更新 更多