简体   繁体   English

获取java.io.NotSerializableException错误

[英]Getting java.io.NotSerializableException error

I want to save an object of 我想保存一个对象

 javafx.scene.media.MediaPlayer 

class using ObjectOutputStream, but it only works with serializable classes and it looks like MediaPlayer class is not serializable. 使用ObjectOutputStream的类,但它仅适用于可序列化的类,并且看起来MediaPlayer类不可序列化。

This is my code: 这是我的代码:

 ObjectOutputStream objectOutputStream = new ObjectOutputStream(
                new BufferedOutputStream(new FileOutputStream(file)));

        MediaPlayer[] player = songList.toArray(new MediaPlayer[songList.size()]);

        objectOutputStream.writeObject(player); // error occurs here

        objectOutputStream.close();

And I get this error: 我得到这个错误:

java.io.NotSerializableException: javafx.scene.media.MediaPlayer
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeArray(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at mp3.MusicPlayer.saveSongs(MusicPlayer.java:72)
at mp3.MainFrame$1.windowClosing(MainFrame.java:55)

How to solve this problem ? 如何解决这个问题呢 ?

The best question here is why would you want to save this in the first place. 这里最好的问题是,为什么要首先保存此内容。 Taking into consideration that MediaPlayer is a JavaFx component I do not see the reason for wanting to serialize and save this. 考虑到MediaPlayer是JavaFx组件,我看不到要序列化并保存它的原因。

Assuming that what you want to achieve is to save the songList you're passing in I would just go ahead and do exactly that. 假设您要实现的是保存您要传递的songList,那么我将继续执行此操作。

There is no point in serializing and saving objects like a MediaPlayer , just save the list of songs and/or whatever settings. 序列化和保存MediaPlayer类的对象没有任何意义,只需保存歌曲列表和/或任何设置即可。 Then in case you can to reconstruct this somewhere else, just deserialize the saved data and reconstruct a new MediaPlayer instance with them. 然后,如果您可以在其他地方重建它,只需反序列化保存的数据并使用它们重建一个新的MediaPlayer实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM