Java: Empty string warning

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!

Who is Participating?
avdejConnect With a Mentor Commented:
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:

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.
fsze88Connect With a Mentor Commented:
try {
// do somethings
} catch (java.lang.NumberFormatException e){

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.
The new generation of project management tools

With’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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).

dutaAuthor Commented:
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");
                      process  ( );
mrcoffee365Connect With a Mentor Commented:
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 )
objectsConnect With a Mentor Commented:
I agress with avdej, you're calling parseFloat() with an empty string.
the full stack trace should tell you where it is occurring

dutaAuthor Commented:
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.
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.
dutaAuthor Commented:
Thanks a lot for very nice words.

I will take time to try your kind tips.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.