java.lang.NumberFormatException:null i

时间:2014-05-06 11:14:26

标签: java jsp servlets numberformatexception

这里我在执行某些操作时遇到问题,例如更新,删除和插入但是它返回一些空值而异常,如果错误的话可能会看一下代码:

错误:

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");
        }

    }
}

5 个答案:

答案 0 :(得分:12)

尝试转换空字符串&#34时,您收到了NumberFormatException;&#34;整数......

答案 1 :(得分:8)

如果未将有效数字传递给Integer.parseInt(),则会出现NumberFormatException

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

尝试打印request.getParameter(&#34; userid&#34;)的值并检查。

答案 2 :(得分:1)

您要转发的页面可能包含可能正在创建错误的字符串到整数转换

答案 3 :(得分:0)

在解析字符串之前,应先进行null检查,然后修剪并解析。如果发生NumberFormatException,请进行处理。

每当遇到此类麻烦时,请放置调试语句并进行分析
实际导致异常的原因。空还是空或空格或字符串?这样您就可以轻松处理它。

Integer userId= parseToNumber(request.getParameter("userId"));
//check for not null and proceed


// returns null if receivedParam = "", "some string", null or number with space
private Integer parseToNumber(String receivedParam)
{
    System.out.println("received param:"+receivedParam);
    if (receivedParam != null && !receivedParam.trim().isEmpty())
    {
        try
        {
            return Integer.parseInt(receivedParam.trim());
        }
        catch (NumberFormatException nfe)
        {
            System.out.println("received param is not a number");
            return null;
        }
    }
    else
    {
        System.out.println("received param is null or empty");
        return null;
    }
}

答案 4 :(得分:0)

在文件user.jsp和listuser.jsp中。您使用了用于URL重写的空间。

您应该使用

`
 import 'package:flutter/material.dart';
 import 'feedPage.dart'
class RootPage extends StatefulWidget {
  @override
  _RootPageState createState() => new _RootPageState();
}
class _RootPageState extends State<RootPage> {
  FeedPage feedPage;

  Widget currentPage;

  @override
  void initState() {
    super.initState();
    feedPage = FeedPage();

    currentPage = feedPage;
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      //Current page to be changed from other classes too?
      body: currentPage
    );
  }
}`