Solved

java.lang.NumberFormatException

Posted on 2008-06-10
5
721 Views
Last Modified: 2010-08-05
I am getting the following error, what can be the cause?

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: An exception occurred processing JSP page /price.jsp at line 47

44:             
45:             System.out.print("barnesAndNoblePriceString "+barnesAndNoblePriceString);
46:             if(barnesAndNoblePriceString != "Price Not Available")
47:                 barnesAndNoblePrice = Double.valueOf(barnesAndNoblePriceString);
48:             
49:       }
50:       %>


Stacktrace:
      org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:524)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:435)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


root cause

java.lang.NumberFormatException: For input string: "Price Not Available"
      sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
      java.lang.Double.valueOf(Unknown Source)
      org.apache.jsp.price_jsp._jspService(price_jsp.java:106)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
0
Comment
Question by:COMPUTER_SC_STUDENT
  • 3
5 Comments
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
The string must contain a valid floating point value - it doesn't
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 50 total points
Comment Utility
...and that should be

if("Price Not Available".equals(barnesAndNoblePriceString) == false)

Open in new window

0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 50 total points
Comment Utility
(otherwise you're just comparing the references of each String, which is of no interest)
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
try:

46:             if(!"Price Not Available".equals(barnesAndNoblePriceString))

That'll fix your problem, and also avoid NPE's.

Might also be worth using as constant for "Price Not Available"
0
 
LVL 11

Expert Comment

by:spoxox
Comment Utility
I'm going to guess that barnesAndNoblePriceString is either a price (represented by a String) or the String "Price Not Available".

The correct method (and reason) for comparing Strings is shown above.

Sometimes,

if(barnesAndNoblePriceString != "Price Not Available")

will work anyway, but you can't rely on it. (I assume - dangerous, I know - the run time environment might maintain only one copy of identical Strings.) If you've had this experience, then you'll have a hard time believing what's written above!

Some other tools that might be of use:

if(!"Price Not Available".equalsIgnoreCase(barnesAndNoblePriceString.trim()))

equalsIgnoreCase(String anotherString)
          Compares this String to another String, ignoring case considerations.
trim()
          Returns a copy of the string, with leading and trailing whitespace omitted.

http://java.sun.com/javase/6/docs/api/java/lang/String.html
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now