Kryonet error

I’m trying to pass an ArrayList from my gameserver to all my clients. However, i’m getting an error i can’t fix.
Here’s the error:


Exception in thread "Client" com.esotericsoftware.kryonet.KryoNetException: Error during deserialization.
	at com.esotericsoftware.kryonet.TcpConnection.readObject(TcpConnection.java:141)
	at com.esotericsoftware.kryonet.Client.update(Client.java:239)
	at com.esotericsoftware.kryonet.Client.run(Client.java:317)
	at java.lang.Thread.run(Thread.java:680)
Caused by: com.esotericsoftware.kryo.KryoException: Class cannot be created (missing no-arg constructor): NFT.Player
Serialization trace:
playerList (NFT.Network$PlayerList)
	at com.esotericsoftware.kryo.Kryo.newInstantiator(Kryo.java:1048)
	at com.esotericsoftware.kryo.Kryo.newInstance(Kryo.java:1060)
	at com.esotericsoftware.kryo.serializers.FieldSerializer.create(FieldSerializer.java:228)
	at com.esotericsoftware.kryo.serializers.FieldSerializer.read(FieldSerializer.java:217)
	at com.esotericsoftware.kryo.Kryo.readClassAndObject(Kryo.java:735)
	at com.esotericsoftware.kryo.serializers.CollectionSerializer.read(CollectionSerializer.java:109)
	at com.esotericsoftware.kryo.serializers.CollectionSerializer.read(CollectionSerializer.java:18)
	at com.esotericsoftware.kryo.Kryo.readObject(Kryo.java:654)
	at com.esotericsoftware.kryo.serializers.FieldSerializer$ObjectField.read(FieldSerializer.java:605)
	at com.esotericsoftware.kryo.serializers.FieldSerializer.read(FieldSerializer.java:221)
	at com.esotericsoftware.kryo.Kryo.readClassAndObject(Kryo.java:735)
	at com.esotericsoftware.kryonet.KryoSerialization.read(KryoSerialization.java:57)
	at com.esotericsoftware.kryonet.TcpConnection.readObject(TcpConnection.java:139)
	... 3 more

The server sending the object:


PlayerList playerlist = new PlayerList();
playerlist.playerList = otherplayers;
server.sendToAllTCP(playerlist);

and the client recieving it:


if (object instanceof PlayerList) {
    otherplayers = ((PlayerList)object).playerList;
}

the packet must have no constructor OR require no arguments (parameters) im assuming for the Player packet.

you can tell by this:


Caused by: com.esotericsoftware.kryo.KryoException: Class cannot be created (missing no-arg constructor): NFT.Player

//this is the give away 
missing no-arg constructor

just make the variable public

e.g. to create the packet


Player packet = new Player();
packet.player = PlayerEntity;

EDIT: fixed the first line
EDIT: fixed more

Oh, i see, thank you! :slight_smile:

However, it’s not a single player i’m passing, it’s an arraylist of players. How do i make this static?

well, in my game Im having trouble sending an arraylist of entities over, I think you may need to send them seperately, because the packet size is to big according to kryonet (may work for your code).

inside your packet class you want to make the Player variable / array public


//inside the packet code
public ArrayList<Players> players = new ArrayList<Players>();


//creating the packet
Player packet = new Player();
packet.players = thePlayerArrayWeAreSending;

EDIT: fixing

Thank you mister, that worked like a charm!

However, another problem came up. Now when i am trying to loop through it, i’m getting a nullpointer :frowning:


java.lang.NullPointerException
	at NFT.NFTClient.render(NFTClient.java:115)
	at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:703)
	at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:456)
	at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:361)
	at NFT.NFTClient.main(NFTClient.java:134)
Mon Dec 03 10:06:48 CET 2012 ERROR:Game.render() failure - check the game code.
org.newdawn.slick.SlickException: Game.render() failure - check the game code.
	at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:706)
	at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:456)
	at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:361)
	at NFT.NFTClient.main(NFTClient.java:134)

Render method:


    public void render(GameContainer gc, Graphics g) throws SlickException {
        g.drawString("" + System.currentTimeMillis(), 0, 0);
        
        for (int i = 0; i < otherplayers.size(); i++) {
            Player currentPlayer = otherplayers.get(i);
            g.drawString(currentPlayer.getName(), 10, (i * 10) + 10);
        }
        
    }

It works fine without to loop, and i can’t quite figure out what is wrong with the loop

are you sure your are setting the packets array to otherplayers?

some code like this to where your server receives the packet.


Players otherPlayerPacket = (Players)packet;
otherplayers = otherPlayerPacket.players;

Absolutely :slight_smile:


                if (object instanceof PlayerList) {
                    otherplayers = ((PlayerList)object).playerList;
                }


Im guessing their is something wrong with arraylist.

your array may have a blank gap, or actually no players within

you could do this to check:


		if(otherplayers.get(i) != null){
			Player currentPlayer = otherplayers.get(i);
			if(currentPlayer.getName() != null){
				g.drawString(currentPlayer.getName(), 10, (i * 10) + 10);
			}else{
				System.out.println("player name is null for index: "+i);
			}
		}else{
			System.out.println("player is null for index: "+i);
		}

EDIT: had a else in the wrong place

It’s not empty, i suspect it has something to do with it being static

wait, you dont even need to make it static. just it inside the packet to


Public ArrayList<Players> otherplayers ....

I dont know why I thought to set it to static.

I guess the last few weeks of not programming + fairly new to networking dont mix Oo.

EDIT: going back over my posts to remove that static modifier.

EDIT 2:
The rest of the code will work as normal, you just need to remove the static modifier and it should work.