开发者

Java - ImageIcon Serialization Trouble (Last Part of Game)

I've been working on my game pretty much non stop since 11am this morning. And I'm just about done.

But I get an error whenever I try to serialize and deserialize my data...

There's is a "game running" class that extends JFrame. And that holds other classes that are serializable. Now, when I try to serialize those objects to a file (or read them back into the program) I get an error.

Here's the gist of it:

java.io.IOException: failed to load image contents
at javax.swing.ImageIcon.writeObject(ImageIcon.java:418)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:945)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1461)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1392)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1150)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1509)
at java.io.ObjectOutputS开发者_运维百科tream.defaultWriteObject(ObjectOutputStream.java:416)
at javax.swing.JLabel.writeObject(JLabel.java:890)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:945)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1461)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1392)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1150)
at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1338)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1146)

** Is it possible to serialize ImageIcons? (My teacher told me yes, which is why I used them.**

If it matters, this is how I declared/initialized the imageicons in my serializable class:

private final Image heroIcona = new ImageIcon("heroIcon.png").getImage();


You need to use the actual ImageIcon and not convert it into an Image (since ImageIcon is the one that implements Serializable).

  private ImageIcon heroIcona = new ImageIcon("heroIcon.png");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜