• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

submit button not working?

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The class does get compiled but the submit button doesn't work?

 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 73961
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Marshal
Posts: 22441
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or in Java 8:
 
reply
    Bookmark Topic Watch Topic
  • New Topic