0

I'm trying to make a multiplayer chess game using javaFX when player1 tries to send a game request in server this exception will be appeared

Exception in thread "Thread-1" java.lang.ExceptionInInitializerError 
    at java.io.ObjectStreamClass.computeDefaultSUID(ObjectStreamClass.java:1887)
    at java.io.ObjectStreamClass.access$100(ObjectStreamClass.java:79) 
    at java.io.ObjectStreamClass$1.run(ObjectStreamClass.java:263) 
    at java.io.ObjectStreamClass$1.run(ObjectStreamClass.java:261) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.io.ObjectStreamClass.getSerialVersionUID(ObjectStreamClass.java:260) 
    at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:682) 
    at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1880) 
    at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1746) 
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2037) 
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1568) 
    at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2282) 
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2206) 
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2064) 
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1568) 
    at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2282) 
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2206) 
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2064) 
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1568) 
    at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2282) 
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2206) 
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2064) 
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1568) 
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:428) 
    at ServerSide.ClientHandler.newGameRequest(ClientHandler.java:142) 
    at ServerSide.ClientHandler.run(ClientHandler.java:62) 
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalStateException: Toolkit not initialized
    at com.sun.javafx.application.PlatformImpl.runLater (PlatformImpl.java:273)
    at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:268)
    at com.sun.javafx.application.PlatformImpl.setPlatformUserAgentStylesheet(PlatformImpl.java:550)
    at com.sun.javafx.application.PlatformImpl.setDefaultPlatformUserAgentStylesheet(PlatformImpl.java:512)
    at javafx.scene.control.Control.<clinit>(Control.java:87)
... 34 more

this is the code and exception is at game = (Game) ois.readObject();

SimpleUser requestedUser = (SimpleUser) ois.readObject();
game = (Game) ois.readObject();
for (ClientHandler clientHandler : Server.getActiveClients()) {
    if (clientHandler.user.getSimpleUser().equals(requestedUser)) {
        pairedClientHandler = clientHandler;
        pairedClientHandler.getIncomingGameRequests().add(game);
        break;
    }
}

and this is the Game class

package ClientSide.Game;

import General.User.Audience;
import General.User.Player;
import General.User.SimpleUser;

import java.io.Serializable;;
import java.util.ArrayList;

public class Game implements Serializable {
    private Board board;
    private Player player1;
    private Player player2;
    private SimpleUser winner;
    private SimpleUser loser;
    private boolean isDraw;

    ArrayList<Audience> audiences;
    private ArrayList<Move> moves;
    private boolean isRated;
    private boolean isPlayer2Accepted;
    private int time;

    public Board getBoard() {
        return board;
    }

    public Player getPlayer1() {
        return player1;
    }

    public void setPlayer1(Player player1) {
        this.player1 = player1;
    }

    public void setPlayer2(Player player2) {
        this.player2 = player2;
    }

    public Player getPlayer2() {
        return player2;
    }

    public boolean isPlayer2Accepted() {
        return isPlayer2Accepted;
    }

    public void setPlayer2Accepted(boolean player2Accepted) {
        isPlayer2Accepted = player2Accepted;
    }

    public ArrayList<Move> getMoves() {
        return moves;
    }

    public Game() {

    }

    public Game(Player player1, Player player2, boolean isRated) {
        this.player1 = player1;
        this.player2 = player2;
        this.isRated = isRated;
    }  

    {
        board = new Board(this);
        moves = new ArrayList<>();
    }

    public SimpleUser getWinner() {
        return winner;
    }

    public void setWinner(SimpleUser winner) {
        this.winner = winner;
    }

    public SimpleUser getLoser() {
        return loser;
    }

    public void setLoser(SimpleUser loser) {
        this.loser = loser;
    }

    public boolean isDraw() {
        return isDraw;
    }

    public void setDraw(boolean draw) {
        isDraw = draw;
    } 

    public int getTime() {
        return time;
    }

    public boolean isRated() {
        return isRated;
    }
}
Reza Ferdosi
  • 43
  • 1
  • 6
  • 1
    http://idownvotedbecau.se/noexceptiondetails/ like the full stacktrace incl. *cause* of the `ExceptionInInitializerError` – Andreas Jul 11 '18 at 23:22
  • @Andreas sorry it just added – Reza Ferdosi Jul 11 '18 at 23:26
  • Is that the _whole_ [stack trace](https://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors)? I would expect at least one `Caused by:` to be present with an `ExceptionInInitializerError`. Also, you should format stack traces as _code_ and not as a _block quote_; the former is much easier to read. – Slaw Jul 12 '18 at 05:23
  • You called this before your JavaFX application starts? – Jai Jul 12 '18 at 11:14
  • @Slaw sorry I had problems with adding stack trace so a part of it was forgotten and also thanks for your edit – Reza Ferdosi Jul 12 '18 at 11:14
  • @Jai oh no, application is started and after sign in or sign up and requesting a game it will be appeared, can I give the link of the project in GitHub? – Reza Ferdosi Jul 12 '18 at 11:18
  • Why are you sending a `Control` object (see `javafx.scene.control.Control.` in the stacktrace) to the *server* (see `ServerSide.*` in the stacktrace). The server is not running JavaFX, is it? – Andreas Jul 12 '18 at 15:56
  • @Andreas No, it is not – Reza Ferdosi Jul 12 '18 at 16:36
  • Well, then, as you can see, you can't deserialize a `Control` object on the server, so you need to change your logic to not serialize them on the client, e.g. by marking the field [`transient`](https://stackoverflow.com/questions/910374/why-does-java-have-transient-fields). – Andreas Jul 12 '18 at 18:31
  • @Andreas It fixed, thanks – Reza Ferdosi Jul 12 '18 at 21:23

1 Answers1

0

your serializable classes must specify a UUID and this UUId must be the same between threads / network obect streams, try to specify one for each Serializable in your code and give it another try:

private static final long serialVersionUID = SOME_LONG_HERE;
  • I added UUID to all of my serializable classes but it still not works – Reza Ferdosi Jul 11 '18 at 23:50
  • 1
    @RezaFerdosy My guess is that your serialized `Game` doesn't have a `serialVersionUID`, despite you have added that in your class now. In short, you cannot use the old serialized data, you can only use the new ones after this change. – Jai Jul 12 '18 at 01:18
  • When the UUID is missing Java employs reflection to generate a default UUID. By taking a closer look, it appears that line 1887 is a call to a native method (hasStaticInitializer) - is the server that produces the messages running in another OS ?. I would also advise, even if it appears to be an overhead, to serialize to something more structured, say JSON when exchanging messages. – Ioannis Baourdos Jul 14 '18 at 18:19