Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.NumberFormatException: null i

Here i come up with problem while performing some operation like update,delete and insert but its return some null value with exception kindly some one could look at the code if its wrong:

Error:

HTTP Status 500 - null

type Exception report

message null

description The server encountered an internal error that prevented it from fulfilling this request.

exception

java.lang.NumberFormatException: null
    java.lang.Integer.parseInt(Integer.java:454)
    java.lang.Integer.parseInt(Integer.java:527)
    Controller.ControllerTest.doGet(ControllerTest.java:48)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.37 logs.

Apache Tomcat/6.0.37

user.jsp:

<form method="POST" action='ControllerTest' name="frmAddUser">

  <jsp:useBean id="users" class="java.util.ArrayList" scope="request" />
        <% for(int i = 0; i < users.size(); i+=1) 
        { 
            UseBean user = (UseBean)users.get(i);
        %>

        id:<input type="text" name="ID" value="<%=user.getID() %>"><br/>
        Name:<input type="text" name="Name" value="<%= user.getName() %>"><br/>
        Password:<input type="text" name="password" value="<%= user.getPassword() %>"><br/>
        phoneno:<input type="text" name="Phoneo" value="<%= user.getPhoneo() %>"><br/>
        Emailid:<input type="text" name="Emailid" value="<%= user.getEmailID() %>">  <br/> 

        <%} %>
         <input type="submit" value="Submit" />
    </form>

listuser.jsp

  <body>
<table border=1>
<thead>
    <tr>
    <th>Id</th>
    <th>Name</th>
    <th>password</th>
    <th>phoneno</th>
    <th>emailid</th>
    <th colspan=2>Action</th>
    </tr>
</thead>
<tbody>
    <jsp:useBean id="users" class="java.util.ArrayList" scope="request" />
    <% for(int i = 0; i < users.size(); i+=1) 
    { 
        UseBean user = (UseBean)users.get(i);
    %>
        <tr>
        <td><%= user.getID() %></td>
        <td><%= user.getName() %></td>
        <td><%= user.getPassword() %></td>
        <td><%= user.getEmailID() %></td>
        <td><%= user.getPhoneo() %></td>
        <td><a href="ControllerTest?action=edit&userId=<%= user.getID() %>" >Update</a></td>
        <td><a href="ControllerTest?action=delete&userId=<%= user.getID() %>">Delete</a></td>
        </tr>
    <% } %>
</tbody>
</table>
<p>
<a href="ControllerTest?action=insert">Add User</a>
</p>

Controllertest.java:

package Controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.UserDao;
import dbBean.UseBean;

public class ControllerTest extends HttpServlet
{
    private static final long serialVersionUID = 1L;
    private static String INSERT_OR_EDIT = "/user.jsp";
    private static String LIST_USER = "/listUser.jsp";

    private UserDao dao;

    public ControllerTest()
    {
        super();
        dao = new UserDao();

    }

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

        String forward = "";
        String action = request.getParameter("action");
        if (action.equalsIgnoreCase("delete"))
        {
            try
            {
                int userId = Integer.parseInt(request.getParameter("userId"));
                dao.deleteUser(userId);
                forward = LIST_USER;
                request.setAttribute("users", dao.getAllUsers());
            }
            catch (NumberFormatException ex)
            {

                System.out.println("Error occured with during conversion delete");
            }
        } 
        else if (action.equalsIgnoreCase("edit"))
        {
            forward = INSERT_OR_EDIT;
            try
            {

                int userId = Integer.parseInt(request.getParameter("userId"));
                UseBean bean = dao.getUserById(userId);
                request.setAttribute("user", bean);

            } catch (NumberFormatException ex)
            {

                System.out.println("Error occured with during conversion edit");
            }
        }
        else if (action.equalsIgnoreCase("listUser"))
        {
            forward = LIST_USER;
            request.setAttribute("users", dao.getAllUsers());
        } else
        {
            forward = INSERT_OR_EDIT;
        }
        RequestDispatcher view = request.getRequestDispatcher(forward);
        view.forward(request, response);

    }

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

        try
        {
            UseBean bean = new UseBean();
            bean.setName(request.getParameter("Name"));
            bean.setPassword(request.getParameter("password"));
            bean.setPhoneo(request.getParameter("Phoneo"));
            bean.setEmailID(request.getParameter("Emailid"));
            String userid = request.getParameter("ID");
            if (userid == null || userid.isEmpty())
            {
                dao.addUser(bean);
            } else
            {
                bean.setID(Integer.parseInt(userid));
                dao.updateUser(bean);
            }
            RequestDispatcher view = request.getRequestDispatcher(LIST_USER);
            request.setAttribute("users", dao.getAllUsers());
            view.forward(request, response);

        }

        catch (Exception e)
        {
            e.printStackTrace();
            System.out.println("erro occuring in update code");
        }

    }
}
like image 689
user3607180 Avatar asked May 06 '14 11:05

user3607180


People also ask

What is Java Lang NumberFormatException null?

NumberFormatException: For input string: "null" is specifically saying that the String you receive for parsing is not numeric and it's true, "null" is not numeric. Many Java methods which convert String to numeric type like Integer. parseInt() which convert String to int, Double.

What is NumberFormatException in Java?

The NumberFormatException occurs when an attempt is made to convert a string with improper format into a numeric value. That means, when it is not possible to convert a string in any numeric type (float, int, etc), this exception is thrown. It is a Runtime Exception (Unchecked Exception) in Java.


2 Answers

You're getting a NumberFormatException when trying to convert the empty string "" into an Integer...

like image 78
Jagdeep Avatar answered Oct 11 '22 13:10

Jagdeep


You get a NumberFormatException, if you do not pass a valid number to Integer.parseInt()

int userId = Integer.parseInt(request.getParameter("userid"));

Try to print the value of request.getParameter("userid") and check.

like image 40
Jay Avatar answered Oct 11 '22 11:10

Jay