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?
Java primitive types (such as int , double , or float ) cannot have null values, which you must consider in choosing your result expression and host expression types.
You can declare nullable types using Nullable<t> where T is a type. Nullable<int> i = null; A nullable type can represent the correct range of values for its underlying value type, plus an additional null value. For example, Nullable<int> can be assigned any value from -2147483648 to 2147483647, or a null value.
Instead of using int
you can use Integer
(Integer javadoc) because it is a nullable Java class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With