Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (408)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Kryonet error  (Read 654 times)
0 Members and 1 Guest are viewing this topic.
Offline Reck

Senior Newbie





« Posted 2012-12-03 09:37:05 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
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:
1  
2  
3  
PlayerList playerlist = new PlayerList();
playerlist.playerList = otherplayers;
server.sendToAllTCP(playerlist);


and the client recieving it:
1  
2  
3  
if (object instanceof PlayerList) {
    otherplayers = ((PlayerList)object).playerList;
}
Offline Phased
« Reply #1 - Posted 2012-12-03 09:44:02 »

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

you can tell by this:
1  
2  
3  
4  
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
1  
2  
Player packet = new Player();
packet.player = PlayerEntity;


EDIT: fixed the first line
EDIT: fixed more
Offline Reck

Senior Newbie





« Reply #2 - Posted 2012-12-03 09:48:13 »

the packet must have no constructor im assuming for the Player packet.

you can tell by this:
1  
2  
3  
4  
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 static

e.g. to create the packet
1  
2  
Player packet = new Player();
packet.player = PlayerEntity;



Oh, i see, thank you! :-)

However, it's not a single player i'm passing, it's an arraylist of players. How do i make this static?
Games published by our own members! Check 'em out!
Play the free demo of Revenge of the Titans!
Offline Phased
« Reply #3 - Posted 2012-12-03 09:51:20 »

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
1  
2  
3  
4  
5  
6  
7  
//inside the packet code
public ArrayList<Players> players = new ArrayList<Players>();


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


EDIT: fixing
Offline Reck

Senior Newbie





« Reply #4 - Posted 2012-12-03 10:06:27 »

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 static
1  
2  
3  
4  
5  
6  
7  
//inside the packet code
public static ArrayList<Players> players = new ArrayList<Players>();


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

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 :-(

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
    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
Offline Phased
« Reply #5 - Posted 2012-12-03 10:10:49 »

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

some code like this to where your server receives the packet.
1  
2  
Players otherPlayerPacket = (Players)packet;
otherplayers = otherPlayerPacket.players;
Offline Reck

Senior Newbie





« Reply #6 - Posted 2012-12-03 10:11:49 »

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

some code like this to where your server receives the packet.
1  
2  
Players otherPlayerPacket = (Players)packet;
otherplayers = otherPlayerPacket.players;


Absolutely :-)

1  
2  
3  
                if (object instanceof PlayerList) {
                    otherplayers = ((PlayerList)object).playerList;
                }
Offline Phased
« Reply #7 - Posted 2012-12-03 10:17:47 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
      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
Offline Reck

Senior Newbie





« Reply #8 - Posted 2012-12-03 10:34:42 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
      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
Offline Phased
« Reply #9 - Posted 2012-12-03 10:39:22 »

wait, you dont even need to make it static. just it inside the packet to
1  
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.
Pages: [1]
  ignore  |  Print  
 
 

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (118 views)
2013-05-17 21:29:12

alaslipknot (125 views)
2013-05-16 21:24:48

gouessej (154 views)
2013-05-16 00:53:38

gouessej (147 views)
2013-05-16 00:17:58

theagentd (161 views)
2013-05-15 15:01:13

theagentd (146 views)
2013-05-15 15:00:54

StreetDoggy (190 views)
2013-05-14 15:56:26

kutucuk (214 views)
2013-05-12 17:10:36

kutucuk (213 views)
2013-05-12 15:36:09

UnluckyDevil (217 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.355 seconds with 21 queries.