Solved

Java: Empty string warning

Posted on 2009-04-02
10
1,384 Views
Last Modified: 2012-05-06

Dear Experts:

I have a following code.
This code works fine. But  
________________________________________________
if   ( balTxt.getText().trim( ).equals ( "") )
     JOptionPane.showMessageDialog(this, "Enter your balance");
__________________________________________________

I got an empty string warning as below:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String
      at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
      at java.lang.Float.parseFloat(Unknown Source)
____________________________________________________

I need to put a proper exception code.

Can you kindly instruct me how to do that!

Thanks!
0
Comment
Question by:duta
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 15

Assisted Solution

by:fsze88
fsze88 earned 50 total points
ID: 24050306
try {
// do somethings
} catch (java.lang.NumberFormatException e){

}
0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 24050434
You should ideally check whether  balTxt.getText() is null before you try to run the trim method on it.  That's the source of your exception.
0
 
LVL 3

Expert Comment

by:avdej
ID: 24050911
Hi duta!

I think the solution to your problem is rather simple.
I suppose the NumberFormatException you have mentioned here has nothing to do with prompting the user:
    if   ( balTxt.getText().trim( ).equals ( "") ) ...

but happens where you try to actually PARSE a float from the user provided empty string.

To tell you more and to provide the right solution I need some more surrounding code (inclusive the line which initiates a  balTxt-parsing).

0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

Author Comment

by:duta
ID: 24051416
Thank you so much.

My code is:

_________________________
{  if    (  ( balTxt.getText().trim( ).equals ( "") )
                  || ( creditLimitTxt.getText().trim( ).equals ( "") ) )
           
           
            {    
                       System.out.print ( " Enter your bal and credit limit");
           
            }
       
             
    else
           
                      process  ( );
               }
______________________________________________
0
 
LVL 3

Accepted Solution

by:
avdej earned 200 total points
ID: 24051871
Duta, one more time, the code you provided is not the cause of the problem.

You get the Exception BECAUSE you SEND an empty string to sun.misc.FloatingDecimal.readJavaFormatString() method (see javadoc here: http://www.docjar.com/docs/api/sun/misc/FloatingDecimal.html#readJavaFormatString(String))

1. Find THE line in your code where you do THIS, i.e. send a string fetched from a text field to method sun.misc.FloatingDecimal.readJavaFormatString()
2. Block THIS method call (using if-checks for string emptiness etc. and prompting the user) until the user has provided an acceptable input.
0
 
LVL 27

Assisted Solution

by:mrcoffee365
mrcoffee365 earned 150 total points
ID: 24051918
The exception is caused by this line:
if   ( balTxt.getText().trim( ).equals ( "") )

Change it to

if   ( balTxt != null && balTxt.getText() != null && balTxt.getText().trim( ).equals ( "") )


Then add a section to handle those error conditions as well:

if   ( balTxt == null || balTxt.getText() == null )
0
 
LVL 92

Assisted Solution

by:objects
objects earned 100 total points
ID: 24054964
I agress with avdej, you're calling parseFloat() with an empty string.
the full stack trace should tell you where it is occurring

0
 

Author Comment

by:duta
ID: 24055317
Thank all of you for your kind, patient help.

I am a very beginner, and I have a hard time implementing your valuable tips.

I am so sorry about that.
0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 24056409
No need to apologize -- we've all been beginners.

Make the code change I've posted for you, and let us know what happens for you.

At that point you can start debugging in other parts of your code, as "objects" and avdej are interested in having you do.
0
 

Author Comment

by:duta
ID: 24056425
Thanks a lot for very nice words.

I will take time to try your kind tips.

0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
eclipse java  build path 6 59
tomcat not starting 6 45
hibernate jars 4 32
Which non-HTML GUI front end to use with Java? 3 22
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

803 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