0

I developed a simple jsp page which contains two text fields and one submit button to add two numbers. After compiling in browser it shows an error:

HTTP Status 500

org.apache.jasper.JasperException: java.lang.NumberFormatException: null

Here the code:

       <%-- 
Document   : index
Created on : Mar 5, 2013, 7:21:57 PM
Author     : VIJI
       --%>

          <%@page contentType="text/html" pageEncoding="UTF-8"%>
        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
               "http://www.w3.org/TR/html4/loose.dtd">

    <html>
     <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <form>
        <input type="text" name="a"></input>
        <input type="text" name="b"></input>
        <input type="submit" value="ADD"></input>
        <%!
         int a,b,c;

        %>
        <%

         a=Integer.parseInt(request.getParameter("a"));
         b=Integer.parseInt(request.getParameter("b"));
         c=a+b;

        %>
        Addition of two numbers is<%=c%>
    </form>
</body>

Brian
  • 14,610
  • 7
  • 35
  • 43
Vijay Kumar
  • 27
  • 1
  • 4
  • 9
  • 1
    [The use of scriptlets (those `<% %>` things) in JSP is highly discouraged since the birth of taglibs (like JSTL) and EL (Expression Language, those `${}` things) over a decade ago. **Avoid scriptlets in new code.**](http://stackoverflow.com/q/3177733/139010) – Matt Ball Mar 05 '13 at 14:06

5 Answers5

0

First of all, please, avoid scriplets. Then if you want to get the int value, you could use this:

int num = Integer.valueOf(request.getParameter("a")); or

int num = Integer.parseInt(request.getParameter("a")) , etc. anyway you like

Oh, now I see - you have null. That means, that the value of that paremeter is actually empty. Check for the null values.

user
  • 3,058
  • 23
  • 45
0

When you first request the page via a GET all of the parameters are null unless you specify them. So, if you just GET /index.jsp (assuming that's what this page is called), parameters "a" and "b" are both null and Integer.parseInt() throws the exception.

Also, you should take the advice of others and avoid the use of scriptlets here.

Todd
  • 30,472
  • 11
  • 81
  • 89
0

What's your complete URL path?

My guess is, that you don't have a parameter for a & b. That results in providing null to Integer.parseInt().

fguchelaar
  • 4,779
  • 23
  • 36
0

In the first time call, you won't have any values for the parameters a and b which causes a NullPointerException. Check the values with if condition as shown below.

<%-- 
Document   : index
Created on : Mar 5, 2013, 7:21:57 PM
Author     : VIJI
       --%>

          <%@page contentType="text/html" pageEncoding="UTF-8"%>
        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
               "http://www.w3.org/TR/html4/loose.dtd">

    <html>
     <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <form>
        <input type="text" name="a"></input>
        <input type="text" name="b"></input>
        <input type="submit" value="ADD"></input>
        <%!
         int a,b,c;

        %>
        <%
         if (request.getParameter("a") != null){
         a=Integer.parseInt(request.getParameter("a"));
         }else{a=0;}
         if (request.getParameter("b") != null){
         b=Integer.parseInt(request.getParameter("b"));
         }else{b=0;}
         c=a+b;

        %>
        Addition of two numbers is<%=c%>
    </form>
</body>
Ravindra Gullapalli
  • 9,049
  • 3
  • 48
  • 70
0

Use Integer.valueOf(); method with validation like

String TempNum=request.getParameter("a");
int num=(TempNum!=null)?Integer.valueOf(TempNum):0;
General Grievance
  • 4,555
  • 31
  • 31
  • 45