Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting value of radio button

 
duhit Choudhary
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,
I made two radio buttons as:


but when i tried to fetch the value of radio button via following line of code:


I got NullPointerException on the same line. So can anyone tell me the right way to get the value of radio button.

Thanks in Advance
 
Devendra Bhati
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear friend,

Why you hard coded value of radio control in java code. The best practice says, you need to get the value of input controls dynamically. So try to fetch the values of radio buttons from javaScript/jQuery and then request to server to GET/POST method.

I'm not sure but might getParameter() method will accept radio name instead of value. So please check out this.

All the Best. :)
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You must use the name, not the value of the radio button.
 
duhit Choudhary
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:You must use the name, not the value of the radio button.


I have used name with following line of code


But now the problem is it is not giving the string value. i used toString() method for it but still not getting the string. what should i do???
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getParameterValues() returns a string array. If that's not what you want, why are you calling it?
 
duhit Choudhary
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:getParameterValues() returns a string array. If that's not what you want, why are you calling it?




this will not helping me at all... the final thing i want to do is to:


But it is not going inside if(). What i am doing wrong here
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java, how does one compare strings?

Hint: it's not with ==
 
duhit Choudhary
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok.. i got it.
with .equals()!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic