简体   繁体   中英

java.lang.NumberFormatException: null Error in servlet

Index.jsp:

<form action="stu_app_serv" method="post">
                    <table class="table">                    
                        <%
                            try {
                                String sql = "SELECT * FROM students WHERE approved = ?";
                                Connection conn = DBConnect.connect();
                                PreparedStatement pstmt = conn.prepareStatement(sql);
                                pstmt.setInt(1, 0);
                                ResultSet rs = pstmt.executeQuery();

                                while (rs.next()) {
                        %>                    
                        <tbody>
                            <tr>
                                <td><input disabled name="id" value="<%=rs.getInt("st_id")%>"></td>
                                <td><%=rs.getString("name")%></td>
                                <td><%=rs.getString("university")%></td>
                                <td><%=rs.getString("index_no")%></td>
                                <td><%=rs.getString("email")%></td>
                                <td>

                                    <button class="btn btn-success" type="submit" name="approve">Approve </button>
                                    <button class="btn btn-danger" type="submit" name="decline">Decline</button>

                                </td>
                            </tr>   
                        </tbody>                  
                        <%}
                                rs.close();
                                pstmt.close();
                                conn.close();
                            } catch (SQLException e) {
                                System.out.println("Error " + e.getMessage());
                            }
                        %>
                    </table>
                </form>

Servlet file is stu_app_serv.java:

String id = request.getParameter("id");
int x = Integer.parseInt(id);

st_id is the Student ID number. There shows the following runtime error:

java.lang.NumberFormatException: null

in servlet line

int x = Integer.parseInt(id);

How do I fix this error?

Check whether id is not null first, and it must be a valid number. you can use Regex to check that or catch that exception..

You should handle Null value and put inside try catch block. One thing don't do business logic in presentation layer. As you're here writing the code in jsp rather do in your service class or servlet.

String id = request.getParameter("id");

    int x = 0;
    if(id!=null){
      try{
        x = Integer.parseInt(id);
       }catch(Exception e){
       }

    }

When creating this simple program:

  public static void main(String[] args)
  {  
    String id  = null;  
    int x = Integer.parseInt(id);
   }

you can see that you will get a NPE.

So it can be assumed the id that you are sending is null.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM