Bogdan Bogdan - 1 year ago 132
HTML Question

Check if a Integer is empty

I want to check if a

value is empty.
The value for
is completed in a form. Here is my code.

Here the value is introduced:

<input name="varsta" placeHolder="Varsta:" type="text" data-constraints='@NotEmpty @Required @AlphaSpecial'> <br/><br/>

Now I want to check if there are data introduced or not.

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

PrintWriter out = response.getWriter();

Integer varsta = Integer.parseInt(request.getParameter("varsta"));
request.getSession().setAttribute("varsta", varsta);

try {
DBConnection connection = new DBConnection();
Connection con = connection.Connect();

PreparedStatement ps = con.prepareStatement(
"insert into user(Nume,Prenume,E_mail,Parola,Varsta,Sex,Greutate,Inaltime,Nivel_activitate,Calcul_calorii)" +
"values ('"+nume+"','"+prenume+"','"+email1+"','"+parola+"','"+varsta+"','"+sex+"','"+greutate+"','"+inaltime+"','"+activitate+"','"+calorii+"')"

if (varsta == null && "".equals(varsta)) {
String message = "Va rugam completati cu atentie TOATE campurile!";
request.setAttribute("message", message);
request.getRequestDispatcher("/inregistrare.jsp").forward(request, response);
} else {
int i = ps.executeUpdate();

if (i > 0) {
request.getRequestDispatcher("/preferinte.jsp").forward(request, response);
} catch(Exception se) {

is not working.

Could anyone help me?

Answer Source

Class Integer is just an wrapper on top of primitive int type. So it can either be null or store a valid integer value. There is no obvious "empty" definition for it.

If you just compare Integer against empty String, you''ll get false as a result. Always. See Integer.equals(Object o) implementation:

public boolean equals(Object obj) {
    if (obj instanceof Integer) {
        return value == ((Integer)obj).intValue();
    return false;

First of all, you can get a NumberFormatException during parsing integer in the line:

Integer varsta = Integer.parseInt(request.getParameter("varsta"));

And you are getting it, since For input string: "" looks like a NumberFormatExpection message.

In your example you should either check whether the "varsta" attribute value is a number (assume it's a string) and then parse it, or parse it as is and catch NumberFormatException that Integer.parseInt() throws on incorrect argument.

First solution:

Integer varsta = null;
String varstaStr = request.getParameter("varsta"); // read string 'varsta' field
if (varstaStr != null && varstaStr.matches("\\d+")) { // null-check and regex check to make sure the string contains only digits
     varsta = Integer.parseInt(varstaStr);

Second solution:

Integer varsta = null;
String varstaStr = request.getParameter("varsta"); // read string 'varsta' field
try {
    varsta = Integer.parseInt(varsta);
} catch (NumberFormatException e) {
    // handle error

After this, you have one more problem in the line:

if(varsta == null && "".equals(varsta)){

The varsta reference has type Integer here, so "".equals(varsta) will always return false:

(varsta == null && "".equals(varsta)) = [assume varsta is null] =
((null) == null && "".equals(null)) = (true && false) = false


if(varsta == null && "".equals(varsta)){


if(varsta == null){

This should help you.

P.S. If you use Java of version 7 or higher, consider use try-with-resources to manage Connection and PreparedStatement.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download