Reputation: 5416
Now I have been stuck by an error which shows the following error in the Eclipse browser.The Error looks like:
13:53:12,294 INFO [STDOUT] 30
13:53:12,294 ERROR [[Expthsmon]] Servlet.service() for servlet Expthsmon threw exception
java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Expthsmon.doGet(Expthsmon.java:44)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:241)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:580)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)
I believe that this part of the code
int amountofnoon=0;
int amountofafternoon=0;
int noofnoon=0;
int noofafternoon=0;
String amountnoon=null;
String amountafternoon=null;
amountnoon=request.getParameter("amountnoon");
amountafternoon=request.getParameter("amountafternoon");
//if((amountnoon !=null )&&(amountafternoon !=null))
amountofnoon=Integer.parseInt(amountnoon);
System.out.println(""+amountofnoon);
amountofafternoon=Integer.parseInt(amountafternoon);
System.out.println(""+amountofafternoon);
is creating problem,where amountofnoon and amountofafternoon should be integer value.What should I do?
I am also adding the jsp part of code from where it is getting the value:
<form name="expenditurefthsmon" action="Expthsmon">
<div align="center">Enter The Amount of Days Expenditure</div>
<div align="center"><input type="text" name="amountnoon"></input></div>
<div align="center">Enter The Amount of Nights Expenditure</div>
<div align="center"><input type="text" name=amountafternoon"></input></div>
<div align="center"><input type="submit" value="Calculate"></input></div>"
</form>
Upvotes: 1
Views: 1246
Reputation: 5619
The above code in your html misses a quotation mark for the name attribute
<div align="center"><input type="text" name=amountafternoon"></input></div>
It should be
<div align="center"><input type="text" name="amountafternoon"></input></div>
Other than that, you must never trust the input from the user and always validate it againts invalid or null values before going into your core logic.
Upvotes: 2
Reputation: 57316
Most likely, one of these two calls returns null
:
amountnoon=request.getParameter("amountnoon");
amountafternoon=request.getParameter("amountafternoon");
Trying logging/printing these two values before passing them to Integer.parseInt
.
If you pass null
to parseInt
, you'll get a NumberFormatException: null
error as you described.
Upvotes: 1
Reputation: 2221
you should check for null value. e.g.
if(amountnoon != null){
amountofnoon=Integer.parseInt(amountnoon);
}
Upvotes: 1
Reputation: 240890
amountnoon
AND/OR amountafternoon
has value null
, You need to validate request parameters for its nullity
Upvotes: 2