How to write nullable int in java?

Dozer picture Dozer · Jan 14, 2013 · Viewed 41.4k times · Source

I want to convert a web form to a model in Java.

In C# I can write this:

<input name="id" value="" type="text"/>


public class Test
{
    public int? Id{get;set;}
}

The id can be null.

But in Java when using struts2 it throws an exception:

Method "setId" failed

So how to write this case in Java?

Answer

Ivaylo Strandjev picture Ivaylo Strandjev · Jan 14, 2013

Instead of using int you can use Integer (Integer javadoc) because it is a nullable Java class.