java.lang.NumberFormatException: null solution

Keywords: Java Back-end

I wonder if you, like me, often add a space in the code or write a wrong letter in your name. After reporting the error, you can't find out what's wrong. Ask your colleagues to help solve the problem. After finding the problem, you really want to slap yourself. You always make such a low-level mistake and are speechless. Next, let's get to the point:

I want to click the corresponding update | delete to jump to the corresponding page. The code is as follows:

<c:forEach items="${listManager}" var="manager">

    <tr class="tr2">
       <td>${manager.id}</td>
       <td>${manager.name}</td>
       <td>${manager.price}</td>
       <td>${manager.introduce}</td>
       <td><a href="<%=path%>/cn/servlet/ManagerServlet?flag = update & m_id =${manager.id}">to update</a>&nbsp;&nbsp;&nbsp;

       <a href="<%=path%>/cn/servlet/ManagerServlet? flag =delete & m_id= ${manager.getId()}">delete</a>
      </td>

     </tr>
</c:forEach>

Corresponding MangerServlet Code:

 if (flag.equals("delete")){

            System.out.println("=======delete"+flag);
            String m_id = req.getParameter("m_id");
            System.out.println(m_id);
            //M of type String_ Convert ID to int

            Integer id = Integer.parseInt(m_id.trim());
            System.out.println("---id--"+id);

            ManagerDao managerDao = new ManagerDao();
            int m = managerDao.delManager(id);

            String path = "";
            if (m==1){
                List<Manager>listManager ;
                listManager = managerDao.list();
               // HttpSession session = req.getSession();
                //session.setAttribute("listManager",listManager);
                req.setAttribute("listManager",listManager);
                path="/manager/menu.jsp";
            }else {
                path = "/error.jsp";
            }
            RequestDispatcher dispatcher = req.getRequestDispatcher(path);
            dispatcher.forward(req,resp);

        }else if (flag.equals("update")){

            System.out.println("=======update"+flag);

            String m_id = req.getParameter("m_id");
            System.out.println(m_id);

            int id = Integer.parseInt(m_id);
            ManagerDao managerDao = new ManagerDao();

            Manager manager = managerDao.getManager(id);
            req.setAttribute("manager",manager);

            RequestDispatcher dispatcher = req.getRequestDispatcher("/manager/update.jsp");
            dispatcher.forward(req,resp);

ManagerDao layer code:

public int delManager(int id) {

        //Database connection
        Connection conn  = JDBC.getConnection();
        Statement state = null;
        // System.out.println(u);
        try {
            state  = conn.createStatement();
            String sql = "delete  from menus where  id="+id;
            System.out.println(sql);

            m = state.executeUpdate(sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        JDBC.close(conn, state,null);
        return  m;
    }
    public Manager getManager(int id) {
        //Database connection
        Connection conn  = JDBC.getConnection();
        Statement state = null;
        //The local variable has no initial value
        Manager manager = null;
        // System.out.println(u);
        try {
            state  = conn.createStatement();
            String sql = "select *  from menus where  id="+id;
            ResultSet rs = state.executeQuery(sql);
            if(rs.next()){
                manager = new Manager();
                manager.setId(rs.getInt(1));
                manager.setName(rs.getString(2));
                manager.setPrice(rs.getString(3));
                //manager.setPrice(rs.getBigDecimal(3));

                manager.setIntroduce(rs.getString(4));

            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        JDBC.close(conn, state,null);
        return  manager;

    }

Screenshot of running result error:

  I went to the Internet and tried all kinds of methods, but I couldn't solve it. Finally, I called a friend to help me. Finally, she said that there should be no spaces here, otherwise she would report a 500 error,

  Also, I wrote update as updata, and then the interface couldn't jump over and report errors. I convinced myself

Summary:

1. Most of the time, it is the problem of type conversion:
For example, the conversion is as follows: int state = Integer.parseInt(request.getParameter("state")) may have spaces during the conversion process, or forget to pass the parameters when passing the url value, or pass errors, resulting in the obtained value being null.

There is also a low-level error, that is, there are empty parameters in the database written by mysql or you, which will lead to problems
You have to fill in the parameter value. There can be no vacancy.

2.

resolvent:

2.1. Remove spaces before conversion, Integer.parseInt(numString.trim());

2.2. Empty string before conversion: if(s! = "") {conversion}

2.3. Empty judgment before conversion: if (s! = null) {conversion}

Add a console print statement at an appropriate location to check for errors in that statement.

Finally, you must be careful and patient when typing code~~~

ps: the first time I write a blog, there are many deficiencies. I hope you can correct me. I will improve slowly!

Posted by sansoo on Fri, 03 Dec 2021 15:36:36 -0800