alextc
alextc

Reputation: 3515

DWR reporting converter error when passing null from Javascript to Java class

In my Java EE web application, I use DWR (Direct Web Remoting) for Ajax.

The Java class has a number of int members which can be converted to a JavaScript object by DWR. For example:

// Java
public class MyClass {

    private int id;
    private String comments;
}

// the converted JavaScript class
var myObject = {
    id: 100,
    comments: "hello world"
}

However, when I tried to send the js object with id equal to null to the app server, DWR reported the following exception:

Erroring: batchId[7] message[org.directwebremoting.extend.MarshallException: Error marshalling int: Format error converting null. See the logs for more details.]

Can anyone help with any ideas?

Upvotes: 2

Views: 4757

Answers (1)

Juan Calero
Juan Calero

Reputation: 4224

In Java, int is a value type, not a reference type. Therefore, it can never get assigned the value null. Javascript, on the other side, uses dynamic typing. Every variable can change its type, so you can assign null to everything. DWR deals with that generating an error.

You have several options:

If you need to work with the fact that this value can be null in the server-side (which looks like it's the case), you could use a reference type, like Integer, instead of int. Otherwise, simply be sure to always assign a value to the Javascript idmember

Another option: maybe DWR allows to assign a default value if the value from the javascript object is missing.

Upvotes: 4

Related Questions