Skip to content
Advertisement

java.lang.NumberFormatException: null with error status 500

I faced issue with submitting data from html to servlet Can’t

This is my html file

<form action="Maths" method="post">


<input type="radio" id="add" name="math" value="add">
  <label for="add">Addition</label><br>
  <input type="radio" id="sub" name="math" value="sub">
  <label for="sub">Subtraction</label><br>
  <input type="radio" id="multi" name="math" value="multi">
  <label for="multi">Multiplication</label><br>
  <input type="radio" id="div" name="math" value="div">
  <label for="div">Division</label><br><br>
  Enter first number : <input type="number" id="first" required><br>
  Enter second number : <input type="number" id="second" required><br>
  <input type="submit" value="Check Result">
  </form>

this is the servlet file for getting the data

 package com.mathematical;

import java.io.IOException;
import java.io.PrintWriter;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;


public class Maths  extends HttpServlet {
    private static final long serialVersionUID = 1L;
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException,NumberFormatException {
        
        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();
        int num1 = Integer.parseInt(request.getParameter("first"));
        int num2 = Integer.parseInt(request.getParameter("second"));
        System.out.println("Hello");
        String str = request.getParameter("math");
        
        int result = 0;
        if(str.equals("add"))
            result = num1 + num2;
        else if(str.equals("sub"))
            result = num1 - num2;
        else if(str.equals("multi"))
            result = num1 * num2;
        else
            result = num1 / num2;
        pw.println(result);
        
    }
}

error:

HTTP Status 500 – Internal Server Error Type Exception Report

Message null

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

java.lang.NumberFormatException: null
    java.base/java.lang.Integer.parseInt(Integer.java:614)
    java.base/java.lang.Integer.parseInt(Integer.java:770)
    com.mathematical.Maths.doPost(Maths.java:19)
    jakarta.servlet.http.HttpServlet.service(HttpServlet.java:689)
    jakarta.servlet.http.HttpServlet.service(HttpServlet.java:770)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

Note The full stack trace of the root cause is available in the server logs.

Advertisement

Answer

Use name parameter instead of id. Change below 2 lines in your code and check if it works.

Enter first number : <input type="number" id="first" name="first" required><br>
Enter second number : <input type="number" id="second" name="second" required><br>

Best Regards, Haridas.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement