0

I am programming a game in Java (tetris-like) but I got this error when running it :

    java.rmi.UnmarshalException: error unmarshalling return; nested exception is: 
java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: java.awt.image.BufferedImage
at sun.rmi.server.UnicastRef.invoke(Unknown Source)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(Unknown Source)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(Unknown Source)
at com.sun.proxy.$Proxy0.PoolPieces(Unknown Source)
at Fenetre.<init>(Fenetre.java:196)
at Score.lancerNewFenetre(Score.java:103)

So this error happens at the beginning, when I try to retrieve an Array of "Piece" from the server (Class "Partie" (Game)), to then set the pieces and be able to play. The code that makes the error occur is :

    try 
    {
        this.unpool = this.ninja.PoolPieces(this.id);
        System.out.println(unpool);
        this.setBoutons(this.unpool);
        System.out.println("boutons set");
    } 
    catch (RemoteException e) 
    {
        e.printStackTrace();
    }

It breaks at the first line, where "unpool" is a Piece[], "ninja" is my interface that I set at the beginning of the code, and "PoolPieces" is my method to get a pool of 3 pieces (Piece[]). Here is the code of the method "PoolPieces" :

public Piece[] PoolPieces(Integer id) 
{
    Integer rang = this.listejoueurs[id].getRang(id);
    Integer retour = rang + 1;
    this.listejoueurs[id].setRang(retour);
    this.leretourpieces = this.piecespartie[rang];
    return this.leretourpieces;
}

I have created a class "Joueur" (Player), and every player has a "rang" (rank). So, the avaible pieces are the same for all the players, and they retrieve the pool of pieces according to their progression in the game. The "listejoueurs" is a Joueur[], it is an array containing the different players for the game. The "id" is the identifier to link a client with a player : the first player added is added at place 0 in the array, and the id = 0 is sent to the client. The next player will have id = 1, and so on. "leretourpiece" is my Piece[] that I want to return. It is taken from a Piece[][], created randomly at the beginning of the game. It is an array, of array of 3 Piece. "leretourpiece" gets the pool of piece according to the progression of the player (as explain above).

After looking for that error on internet, I implemented Serializable in barely every class, but nothing changed. The strange thing is also that something the error doesn't show up, and succeeds in getting the pool, maybe once over 10 intents or so, but then, when putting the pieces on the grid I have sme problems, misplacements, etc. (that may be an error in the code that puts the piece on the grid, I am not sure).

So I am quite stuck with that problem and I can't find where it comes from. Thank you very much in advance for the time taken to read this, and eventually help me :)

  • Okay, I did the change you suggested but the same error still comes out :/ –  Jun 06 '16 at 13:42

2 Answers2

0

Since you dont have related code block its hard to give clear answer. In some part of your code you are using BufferedImage as instance variable and try to serialize that. Simply find the related code block and carry into method body as an image provider. (i.e. getMyImage())

If you can not find a solution, add related code block (where you use BufferedImage) into your question so I can update my answer

**EDIT**** Can you please try this:

Remove ImageIcon references on Class Fenetre, below part:

private ImageIcon image1;
private ImageIcon image2;
private ImageIcon image3;   

And replace this part:

 this.image1 = new ImageIcon(this.getClass().getResource(this.nompiece1 + ".png"));
        this.image2 = new ImageIcon(this.getClass().getResource(this.nompiece2 + ".png"));
        this.image3 = new ImageIcon(this.getClass().getResource(this.nompiece3 + ".png"));

with:

ImageIcon image1 = new ImageIcon(this.getClass().getResource(this.nompiece1 + ".png"));
        ImageIcon image2 = new ImageIcon(this.getClass().getResource(this.nompiece2 + ".png"));
        ImageIcon image3 = new ImageIcon(this.getClass().getResource(this.nompiece3 + ".png"));
HRgiger
  • 2,750
  • 26
  • 37
-1

The object you are trying to manipulate via RMI holds a reference to a BufferedImage and, as the error message implies, java.awt.image.BufferedImage does not implement java.io.Serializable.

As is (unfortunately) often the case with Java, it is better to stay away from the provided Serializable mechanism an provide yours.

(For the record, Java designers actually regret having implemented it in the first place)

Downvoters: since you downvote faster than you check, let me save you some time: watch this video of Brian Goetz (Java Language Architect) and at 20:59 have a good look at the slide that says "regretting serialization" and listen until to about 22:15.

For the rest of us, there are librairies out there, such as Kryo which does it better, faster, safer.

Bruno Grieder
  • 28,128
  • 8
  • 69
  • 101