Jacob
Jacob

Reputation: 14731

Correct way to initialize wrapper Integer

I have a drop down field in application which displays numbers.

When user doesn't select any value from drop down, I would want to insert as null to database.

How can I initialize an Integer wrapper class to null?

I have tried as

 Integer  days = new Integer(null);
        if (request.getParameter("days").equals("")) {
        } else {
            days  = 
                    Integer.parseInt(request.getParameter("days"));
        }

However I am getting the following error, so what is the correct method in declaring Integer variable?

NumberFormatException at test.doPost(Controller.java:23);

How to initialize an Integer variable so that if no values are selected by user then null should get inserted.

Upvotes: 1

Views: 2169

Answers (6)

Konstantin Yovkov
Konstantin Yovkov

Reputation: 62864

How about :

Integer days = null;
String param = request.getParameter("days");
if (param != null && !"".equals(param)) {
   days  = Integer.parseInt(request.getParameter("days"));
}

Upvotes: 1

anubhava
anubhava

Reputation: 785246

You should initialize with null:

    Integer  days = null;
    if (request.getParameter("days") != null && !request.getParameter("days").isEmpty()) {
        days = Integer.parseInt(request.getParameter("days"));
    }

UPDATE: Better to validate its an integer first:

    Integer  days = null;
    if(request.getParameter("days")!=null && request.getParameter("days").matches("^\\d+$"))
    {
        days = Integer.parseInt(request.getParameter("days"));
    }

UPDATE 2: To be able to insert null in DB:

if (project.getDays() != null)
   callablestatement.setInt(2, project.getDays());
else 
   callablestatement.setNull(2, java.sql.Types.INTEGER);

Upvotes: 7

user180100
user180100

Reputation:

With a try/catch:

Integer days = null;
try {
    days = Integer.parseInt(request.getParameter("days"));
} catch (final NumberFormatException ex) {
    // ignore
}

or with guava Ints

Integer days = null;
final String param = request.getParameter("days");
if (param != null) {
    days = Ints.tryParse(param);
}

This way, you avoid the NumberFormatException if "days" is not parsable.

Upvotes: 1

Itay Karo
Itay Karo

Reputation: 18286

You cannot trust the value of the days parameter in the request.
What if some client will pass an invalid value? (a non-number string).
For this reason you should catch NumberFormatException when you try to parse the parameter value.

Integer days = null;
String parameterValue = request.getParameter("days");
if (parameterValue != null && !parameterValue.isEmpty()) {
  try {
    days = Integer.parseInt(parameterValue);
  } catch (NumberFormatException e) {
    // log or something
  }
}

Upvotes: 1

Juned Ahsan
Juned Ahsan

Reputation: 68715

Apart from the declaration on which others have commented.

You are getting NumberFormatException because request.getParameter("days") is returning a non integer value text or null. parseInt method throws NumberFormatException when it gets a string input which is not a valid integer value.

Upvotes: 1

blackpanther
blackpanther

Reputation: 11486

What about setting the days object to null and then checking to see if the days object is null or is empty:

Integer days = null;
if (request.getParameter("days") != null && !request.getParameter("days").isEmpty()) {
    // rest of code.

Upvotes: 1

Related Questions