• Post Reply Bookmark Topic Watch Topic
  • New Topic

empty string error  RSS feed

 
Kip Bodey
Ranch Hand
Posts: 43
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will post all of my code with a comment on the line where the issue is. but the string isnt empty to me... here is what i am trying to do. you click a number it appears in the solutionField. then hit plus, it will change the add option to true and others to false, then it will read that string in the solutionField which is the number that was clicked and set double firstNum equal to that. when i run the calculator it works fine. but this error occurs. also... it doesnt throw an error for equal, which is the same code so im a little more confused. I am only working on getting the add function to work without error right now, which is why everything else looks blah, thank you



by the way this is the error message
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at sun.misc.FloatingDecimal.parseDouble(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at CalculatorView$theHandler.actionPerformed(CalculatorView.java:165)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll definitely want to check that the solutionField component actually contains something before you call Double.parseDouble() on it. There's nothing in your code that prevents that from happening, as far as I can see.
 
Kip Bodey
Ranch Hand
Posts: 43
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay, I will take care of that at some point thanks, but that still isnt fixing my current problem
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your current problem is that you're taking the contents of the solutionField, which are an empty string, and calling Double.parseDouble on that value without checking that it's not blank. Alternatively you could avoid doing the check, but then you should catch the exception and deal with it appropriately when the user clicks the + button and the field is empty.
 
Kip Bodey
Ranch Hand
Posts: 43
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay, but the field is not empty thats what i am not getting, i understand if i didnt click a button. but lets say i click a number and it is up in the field, then i hit plus it reads in the text field which has a 5 in it, it says empty string? i finished the program for everything, and everything works the way it should but the plus sign still throws that error. everything only works fine if i do everything correct i know i need to fix that still. but assume i pushed the button i am not understanding
 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you do a system.out.println(solutionField.getText()) at the line before the error, what's the value? Is it null? Empty? Something that's not all numbers?
 
Kip Bodey
Ranch Hand
Posts: 43
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it prints the value that was in the box, then throws the error? here ill show you my new code, everything works, but that, i mean it still works, it just throws error

 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use a decimal point does it still throw the error?
 
Kip Bodey
Ranch Hand
Posts: 43
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah it does
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to run your code, but I can't because it isn't an SSCCE (follow that link). I tried deleting all the references to the CalculatorModel class but that didn't help because there's no public static void main(String[] args) method. So I'm stuck, sorry.
 
Kip Bodey
Ranch Hand
Posts: 43
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well there are two other parts thats why it wouldnt run, here is the rest of the code


 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kip Bodey wrote:Well there are two other parts thats why it wouldnt run, here is the rest of the code

Did you read the SSCCE link Paul posted above?
 
Kip Bodey
Ranch Hand
Posts: 43
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i did, i can trim it to where the error occurs, but im just as concerned with why it isnt happening for the other operand buttons, they have the exact same code copied and pasted

now only one throws an error, but they do the exact same thing?
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would help if you could produce an SSCCE so that other people can run your code. (Hint: what does the first "S" in that acronym stand for?)
 
Kip Bodey
Ranch Hand
Posts: 43
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it stands for short, so i made short code for just the plus sign and three numbers and it works, which again will not solve my problem, there id something small in my entire code that is wrong i think and i cant find it here is the code for it to work

 
Kip Bodey
Ranch Hand
Posts: 43
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i put in an event handler for the plus button twice, thats what was causing the issue. but thanks for the help, and for the advice on how to ask for help in the future!!
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, I can see how that would cause the problem. And your last bit of posted code is much better for an SSCCE -- it's hard to make Swing code short but it's always worth removing near-duplicate code, like what you did.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!