1

The error I am getting is: HTTP Status 500 – Internal Server Error Type Exception Report

I am following a tutorial and am doing just as the instruction giver says.

The error I am getting is: Cannot parse null string The server encountered an unexpected condition that prevented it from fulfilling the request. java.lang.NumberFormatException: Cannot parse null string

Servlet1.java

package com.servlets;
 
import java.io.IOException;
 
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
@WebServlet("/servlet1")
public class Servlet1 extends HttpServlet{
 
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // TODO Auto-generated method stub
         
        int number1=Integer.parseInt(req.getParameter("num1"));
        int number2=Integer.parseInt(req.getParameter("num2"));
         
        int sum=number1+number2;
         
        req.setAttribute("s", sum);
        RequestDispatcher rd=req.getRequestDispatcher("/servlet2");
        rd.forward(req, resp);
    }
}

Servlet2.java

package com.servlets;
 
import java.io.IOException;
import java.io.PrintWriter;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
@WebServlet("/servlet2")
public class Servlet2 extends HttpServlet{
     
     
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // TODO Auto-generated method stub
 
        int number1=Integer.parseInt(req.getParameter("num1"));
        int number2=Integer.parseInt(req.getParameter("num2"));
         
        resp.setContentType("index/html");
         
        int product=number1*number2;
        int sum=(int) req.getAttribute("s");
        PrintWriter out=resp.getWriter();
    }
}

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <h1>This is the Form</h1>
    <br>
    <br>
     
    <form action="servlet1" method="post">
        <label>Enter the first number:</label>
        <input type="text" name"num1"/>
         
        <label>Enter the second number:</label>
        <input type="text" name"num2"/>
         
        <button type="submit">Submit</button>
         
    </form>
</body>
</html>

1 Answer 1

2

This parsing logic you have in your servlets is not safe:

int number1=Integer.parseInt(req.getParameter("num1"));
int number2=Integer.parseInt(req.getParameter("num2"));

Java will not be able to parse int from parameter if parameter is null or not a number. In your particular case one of the parameters is null. But generally, as it's a user input, you need to validate if parameters num1 and num2 can be parsed or you need to catch an exception and process it (show meaningful error like "Please input correct data" etc.). You may check this question, you'll get the idea.

Also you have a bug in the HTML, = is missing, so your inputs do not have a name, here you already loosing your parameters, they will not correctly come to the servlet. The code should look something like that:

<form action="servlet1" method="post">
    <label>Enter the first number:</label>
    <input type="text" name="num1"/>
         
    <label>Enter the second number:</label>
    <input type="text" name="num2"/>
         
    <button type="submit">Submit</button>
         
</form>
Sign up to request clarification or add additional context in comments.

10 Comments

Is this code right? try { number1=Integer.parseInt(req.getParameter("num1")); } catch (NumberFormatException e) { System.out.print("Error"); } try { number2=Integer.parseInt(req.getParameter("num2")); } catch (NumberFormatException e) { System.out.print("Error"); }
@ssc1234 with this code you're catching the exception. Is it correct or not, depends on what you want to achieve :) What you're doing is, basically, you're suppressing the error and write it in log. Maybe the better idea would be to put an error inside response and display meaningful message to your user.
I am putting 2 numbers in textbox but why does it still give me an error?
@ssc1234 try to extract your req.getParameter("numX") into variables and output them into console or into log before parsing. Then you will see what do you parse. Is it happening in the Servlet1 or Servlet2? Logging the variables will allow you to see how you pass those parameters.
It worked. resp.setContentType("text/html"); instead of resp.setContentType("index/html");
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.