• Post Reply Bookmark Topic Watch Topic
  • New Topic

Quick question JButton ActionListener IllegalArgumentException  RSS feed

 
Jim Stevens
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quick question... I have a JButton action listener. Inside the action listener when I press the open account button it takes the name entered into the JTextField and opens a Bank account with the name. My question is when the GUI is opened and I press the Open account button before actually putting input into the name textfield, It throws and illegalargumentexception and the program is unusable unless i restart it. How to I avoid this. I have tryed to do a dowhile loop but that doesnt seem to work. I have a try catch but it doesnt seem to work either. THANK YOU.

 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using try-catch in this case is solving the symptoms not the problem. The question is why is an exception thrown.

If you know that then solving the problem will be much easier. Also a stacktrace would be really helpful.
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Stevens wrote:My question is when the GUI is opened and I press the Open account button before actually putting input into the name textfield, It throws and illegalargumentexception and the program is unusable unless i restart it. How to I avoid this.


I prefer not to enable buttons unless they are going to do something meaningful. I especially try to avoid enabling them if they are going to screw things up. So my suggestion would be to not enable the button if there is no data in the name text field.
 
Jim Stevens
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
quick question Pual. I use this code to disable to button but it doesnt seem to work....

 
Jim Stevens
Greenhorn
Posts: 28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
solved
if(name.getText()!= null) needs to be

if(name.getText().length != 0)
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a String method called isEmpty which might be useful. You can also create a regular expression to match money and test whether the text fits that.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!