Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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 1696 times)
0 Members and 1 Guest are viewing this topic.
Offline Reck

Senior Newbie





« Posted 2012-12-03 08: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 08: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 08: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!
Legends of Yore - The Casual Retro Roguelike
Offline Phased
« Reply #3 - Posted 2012-12-03 08: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 09: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 09: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 09: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 09: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 09: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 09: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  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

Dwinin (28 views)
2014-09-12 09:08:26

Norakomi (57 views)
2014-09-10 13:57:51

TehJavaDev (72 views)
2014-09-10 06:39:09

Tekkerue (37 views)
2014-09-09 02:24:56

mitcheeb (57 views)
2014-09-08 06:06:29

BurntPizza (43 views)
2014-09-07 01:13:42

Longarmx (27 views)
2014-09-07 01:12:14

Longarmx (34 views)
2014-09-07 01:11:22

Longarmx (34 views)
2014-09-07 01:10:19

mitcheeb (40 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!