0

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>
ssc1234
  • 13
  • 4

1 Answers1

0

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>
Andrej Istomin
  • 2,527
  • 2
  • 15
  • 22
  • 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 Jun 30 '23 at 20:07
  • @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. – Andrej Istomin Jun 30 '23 at 20:10
  • I am putting 2 numbers in textbox but why does it still give me an error? – ssc1234 Jun 30 '23 at 20:11
  • @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. – Andrej Istomin Jun 30 '23 at 20:16
  • I see, for example, you have this line in the `index.html`: `` - it doesn't look correct to me. Looks like the `=` is missing. It must be `name="num1"`, right? – Andrej Istomin Jun 30 '23 at 20:21
  • I give up. Thank you for your help. – ssc1234 Jun 30 '23 at 20:21
  • @ssc1234, ah, come on. Just fix the lines `` to be `` and for "num2" as well. Then it should start working. + add outputs. Then you'll have control over your software. – Andrej Istomin Jun 30 '23 at 20:23
  • Still doesn't work. – ssc1234 Jun 30 '23 at 20:26
  • 1
    It worked. resp.setContentType("text/html"); instead of resp.setContentType("index/html"); – ssc1234 Jun 30 '23 at 20:41
  • @ssc1234 perfect!!! Glad to hear you won it!!! – Andrej Istomin Jul 01 '23 at 02:50