• Post Reply Bookmark Topic Watch Topic
  • New Topic

submit button not working?  RSS feed

 
Aneeqa Rustam
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The class does get compiled but the submit button doesn't work?

 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

When I ran the code I saw the below error in the console.



On line 97, you have



But the 'genderText' has just been declared on line 21 but not initialized further. However, it is a Radio button and NOT a text box like any others.

Can you modify your code suitably on this ? I am sure you would make it work.
 
Aneeqa Rustam
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't make sense of what your trying to say?
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I meant to say, you are actually capturing the input via a Radiobutton. But inside the code, you are trying to read it as a text box (genderText.getText()). The method getText() works with Text Box but you used it for a Radio Button,.

You need to add an action listener to each radio button because it is also a button. Please see this link http://docs.oracle.com/javase/tutorial/uiswing/components/button.html#radiobutton to modify your code suitably so that whenver you click on a radio button an event gets triggered and you capture the exact response from the User.
 
Aneeqa Rustam
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, could you tell me how I could write out the action listener for the code and what I need to takeaway from this code?
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raghavan Muthu wrote:I meant to say, you are actually capturing the input via a Radiobutton. But inside the code, you are trying to read it as a text box (genderText.getText()). The method getText() works with Text Box but you used it for a Radio Button,.

What's a Text Box? And did you check whether JRadioButton has a method getText()?

Raghavan Muthu wrote:You need to add an action listener to each radio button because it is also a button.

Only if you want to respond to the action of selecting the radio button. That doesn't appear to be the case here.

edit Fixed the Quote tags
 
Aneeqa Rustam
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how do i add an action listener?? also what change do i need to make to the JRadioButton? Are both needed?
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to our GUIs forum. Please don't write such long lines; they make the post illegible.
You add a listener the same way you added it in your previous thread.

You need to think of the design of what you are doing. Radio buttons are not intended to be pressed repeatedly but to maintain their state. You probably want an ordinary button.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aneeqa Rustam wrote:I can't make sense of what your trying to say?


In your previous thread, K. Tsang gave you a link to a tutorial. The code you have posted in this thread suggests that you haven't gone through that tutorial. Please do, and you find if anything there confusing, feel free to ask about it here.
 
Patrick Hayden
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer is in the first line of the stack trace:

genderText is declared but is never initialized. It is referenced in the ActionListener (text5 = genderText.getText()).

At runtime genderText has the value null so this is evaluated as null.getText().

Hence the null pointer exception.

As a separate issue it is recommended that you explicitly start a Swing application in the Event Dispatch Thread:

 
Rob Spoor
Sheriff
Posts: 20903
81
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or in Java 8:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!