I’ve recently started adding networking into my game, and this is the first time i’ve had experience with anything like this.
I’m using this Slick API to handle my graphics (among some other things) and using ObjectInputSteams and ObjectOutputStreams to do the networking.
I decided to test the connection by sending an image through the server, and was met with a NotSerializableException for the image:
java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: org.newdawn.slick.Image
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1332)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:350)
at inGame.ConnectClient.alwaysGet(ConnectClient.java:50)
at inGame.ConnectClient.setUp(ConnectClient.java:26)
at inGame.ConnectClient.run(ConnectClient.java:34)
at java.lang.Thread.run(Thread.java:680)
Caused by: java.io.NotSerializableException: org.newdawn.slick.Image
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1164)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330)
at inGame.ConnectServer.write(ConnectServer.java:40)
at scenes.HostMenu.update(HostMenu.java:47)
at org.newdawn.slick.state.StateBasedGame.update(StateBasedGame.java:268)
at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:657)
at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:408)
at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:318)
at scenes.Head.main(Head.java:47)
How would i make the object serializable? Or am i going in the complete wrong direction with all of this?
Any input is appreciated!
Thanks!
One easy way to make third-party class Serializable is to make your own class that extends Image class and implements java.io.Serializable interface (remember to prepare all versions of constructor and delegate them to the base class’ constructor).
However Slick’s Image class may be not-Serializable for a good reason. If it contains some low-level, operating system related stuff, it may be impossible to just serialize it, send it to the server and open there. Maybe some simplier solutions like just sending image name to the server and loading it from a file on server would be more reliable?