No-Argument constructors?

Hi,



I’ve been fiddling with spidermonkey recently, and for the most part it was working fine, except when I did something (I think from looking at rollbacks its was changing the message definition to non static), and now it’s spitting the error: “Registration error: no-argument constructor not found on:class client.net.net_messages$movepacket” and “Registration error: no-argument constructor not found on:class client.net.net_messages$chatMessage” at me.



It doesn’t seem to go away if I change them back to static, and I’m not entirely sure how to get rid of it or what I broke… I tried moving the to different folders, renaming them, making new classes, but I think I broke something.



Here is the message definitions:

Code:
public class net_messages { /* chatMessage: defines the chatMessage message (packet-style used by the spidermonkey api) used for sending text based messages backwards and forwards */ @Serializable() public class chatMessage extends AbstractMessage { public String text; public chatMessage() { } public chatMessage(String text) { this.text = text; } }
@Serializable()
public class movepacket extends AbstractMessage
{
    public Vector3f move;
    public Vector3f lagmove;
    public boolean dolag;
    public int idd;
    public movepacket()
    {
    }
    public movepacket(Vector3f inmove, int ping, boolean dolag, int id)
    {
        lagmove = move.interpolate(move, ping);
        this.idd = id;
        if(!dolag)
        {
            this.move = inmove;
        }
        else
        {
            this.move = lagmove;
        }
    }
}

}


and here is where i'm registering it:
Code:
public static void registerMessages() { Serializer.registerClass(chatMessage.class); Serializer.registerClass(movepacket.class); }

Not sure what I broke this time, help would be appreciated :D

Not sure, since i played with spider only a few days, but it could be neccesary to include a constructor with all values in your class.

1 Like

Just tried that, didn’t work. (Tried with both just the value itself and this.value)

Well, i usually do no calculations in my Messages and write a new file for every message.

Here is how it looks:

[java]package CommunicationClient2Server.Messages.PlanetInfo;



import com.jme3.network.AbstractMessage;

import com.jme3.network.serializing.Serializable;



/**

*

  • @author Michael

    */

    @Serializable

    public class req_PlanetInfo extends AbstractMessage{

    private int universeId;

    private int planetId;

    public req_PlanetInfo(){}

    public req_PlanetInfo(int uid,int pid){universeId=uid;planetId=pid;}

    }[/java]

    The only other difference i see is that my @Serializable tag has no ()
1 Like

Just tried removing the () from Serializable and having only one message in the file, still no dice. Seriously, what the hell did I do to make it go funky like this?



As far as I can see, there is a no argument constructor right there in it’s face.

@koaandl said:
Hi,

I've been fiddling with spidermonkey recently, and for the most part it was working fine, except when I did something (I think from looking at rollbacks its was changing the message definition to non static), and now it's spitting the error: "Registration error: no-argument constructor not found on:class client.net.net_messages$movepacket" and "Registration error: no-argument constructor not found on:class client.net.net_messages$chatMessage" at me.

It doesn't seem to go away if I change them back to static, and I'm not entirely sure how to get rid of it or what I broke.. I tried moving the to different folders, renaming them, making new classes, but I think I broke something.

Here is the message definitions:
Code:
public class net_messages { /* chatMessage: defines the chatMessage message (packet-style used by the spidermonkey api) used for sending text based messages backwards and forwards */ @Serializable() public class chatMessage extends AbstractMessage { public String text; public chatMessage() { } public chatMessage(String text) { this.text = text; } }
@Serializable()
public class movepacket extends AbstractMessage
{
    public Vector3f move;
    public Vector3f lagmove;
    public boolean dolag;
    public int idd;
    public movepacket()
    {
    }
    public movepacket(Vector3f inmove, int ping, boolean dolag, int id)
    {
        lagmove = move.interpolate(move, ping);
        this.idd = id;
        if(!dolag)
        {
            this.move = inmove;
        }
        else
        {
            this.move = lagmove;
        }
    }
}

}


and here is where i'm registering it:
Code:
public static void registerMessages() { Serializer.registerClass(chatMessage.class); Serializer.registerClass(movepacket.class); }

Not sure what I broke this time, help would be appreciated :D


Non-static inner classes cannot be instantiated without a reference to the outer instance. Thus, SpiderMonkey cannot instantiate an instance of the message just from its class.

If you made them static and it still didn't work then you need to rebuild or something and/or post the modified code. The code posted definitely won't work for the reason I stated.
1 Like

I read that in your last post about this, and I wondered about that. It previously worked when it was static, and now it isn’t reguardless of static state. I’ve obviously done something wrong when I was fiddling with my net code, so i’ll have a look at it in a bit and see if I can figure where I went wrong…

If you have something with static inner classes that doesn’t work then post the code.



But make sure you do a clean build before you test because incremental compiles are notorious for screwing things up when you least need it. :wink:

1 Like

You always need a no-argument constructor for messages, else they cannot be created on the client side.

1 Like
@normen said:
You always need a no-argument constructor for messages, else they cannot be created on the client side.


They have no-argument constructors. The issue in the posted code is that the inner classes are not static.
1 Like
@pspeed said:
They have no-argument constructors. The issue in the posted code is that the inner classes are not static.

Right, I ignored the lower-case constructors..
1 Like

Okay, adding static works and changing some stuff to older revisions, but an older bug popped up that I remembered I was trying to fix.



Here is the new net message code:

Code:
public class net_messages { /* chatMessage: defines the chatMessage message (packet-style used by the spidermonkey api) used for sending text based messages backwards and forwards */ @Serializable public static class chatMessage extends AbstractMessage { public String text; public chatMessage() { } public chatMessage(String text) { this.text = text; } } @Serializable public static class movepacket extends AbstractMessage { public Vector3f move; public Vector3f lagmove; public boolean dolag; public int idd; public movepacket() {
    }
    public movepacket(Vector3f inmove, int ping, boolean dolag, int id)
    {
        lagmove = move.interpolate(move, ping);
        this.idd = id;
        if(!dolag)
        {
            this.move = inmove;
        }
        else
        {
            this.move = lagmove;
        }
    }
}

}


This is the part where it is sending chat:
Code:
public void sendChat(String message) { Message mess = new chatMessage(message); mess.setReliable(true); if(child != null) { child.send(mess); } if(host != null) { host.broadcast(mess); } }

And here is the receiving part:
Code:
public class ClientListener implements MessageListener<Client> { public void messageReceived(Client source, Message message) { if (message instanceof chatMessage) { final chatMessage chatm = (chatMessage) message; lund.print_console_info("C#" + source.getId() + ": " + chatm.toString()); } if (message instanceof movepacket) { final movepacket mo = (movepacket) message; lund.findSpatial(Integer.toString(mo.idd)).setLocalTranslation(mo.move); } } }
public class ServerListener implements MessageListener&lt;HostedConnection&gt;
{
    public void messageReceived(HostedConnection source, Message message)
    {
        if (message instanceof chatMessage)
        {
            chatMessage chatm = (chatMessage) message;
            lund.print_console_info(&quot;C#&quot; + source.getId() + &quot;: &quot; + chatm.text);
        }
    }
}</div>

and the function (lund.print_console_info):
Code:
public void print_console_info(String text) { Main.api.gi.niftyConsoleC.output("INFO: " + text); }

but upon sending a chat message to a connected client, the message comes in as "INFO: C#0: [Ljava.lang.String;@1fd360b". I see the start of the message is correct (The info and the client ID) but the rest of it is strange. I'm sure i've done something wrong somewhere. Is it related to me using statics in the wrong place again?

The problem is much further up in your code I can’t see… for example, put a log in this method:

public void sendChat(String message)



To log the message that you are trying to send. My guess is that it looks like: [Ljava.lang.String;@1fd360b



…which to me says that somewhere before the sentChat() you tried to convert a String[] to a String without realizing it.

1 Like

How would I log it? I tried doing this in the sendChat:

Code:
public void sendChat(String message) { Message mess = new chatMessage(message); mess.setReliable(true); System.out.println(mess.toString()); if(child != null) { child.send(mess); } if(host != null) { host.broadcast(mess); } }

Which from what I can see is the same thing the console does on the receiving end, and It prints out "client.net_messages$chatMessage@132b67c".

I’m not sure how I can be any clearer so I will write the actual code for you:

@koaandl said:
How would I log it? I tried doing this in the sendChat:
Code:
public void sendChat(String message) { System.out.println( "The message:" + message ); Message mess = new chatMessage(message); mess.setReliable(true); System.out.println(mess.toString()); if(child != null) { child.send(mess); } if(host != null) { host.broadcast(mess); } }


The problem is not in spider monkey or JME at all but in your own code. At least I'm 99% sure that the above println I added will not show you good things.
1 Like

Ohhhhhhhhhhhh. Excuse me while I go bang my head on a wall in a corner for not seeing that. I’m so daft sometimes. Sorry pspeed :(.

Ahh, you people are so patient with me and my silly escapades. The console prints out “[Ljava.lang.String;@1c634b9”, exactly the same as what the client receives. Somewhere there is something going wrong. I’ll go through my code and look for the source of it.



Thank you for all your help guys :slight_smile:



Edit: @pspeed was absolutely correct, I was passing a string array to the sendchat function instead of a string. Woops. I’m so bad at this :(.

“[Ljava.lang.String;@1c634b9



…is what a String[] would look like if you printed it.



ie:

String[] myStringArray = new String[2];



System.out.println(“Test:” + myStringArray );



…would look just like what you see.



Somewhere you are getting a string array when you think you are getting a string.

1 Like

As edited in my last post, you’re 100% right. Gosh I suck at coding. Thanks for the patience guys :slight_smile:

You should try and follow the standard Java coding conventions as well. It makes your code more readable for others and when you are used to reading it it makes other people’s code easier for you to read.



Not to mention for 99% of potential java employers it will be mandatory.



http://www.oracle.com/technetwork/java/codeconv-138413.html



If nothing else read section 9 on naming conventions.