[resolved] NullPointerException with Button.getSelection() [message #628981] |
Sat, 25 September 2010 11:41 |
Chris Messages: 1 Registered: September 2010 |
Junior Member |
|
|
Hi!
I have a minor problem here. I derived a custom class from a composite containing some RadioButtons. In there I have a method to check which one of the buttons is selected. It simply calls the getSelection method. Now when one button is not selected it correctly returns a false. If it is selected though it throws a NullPointerException. Is that maybe a bug? I'm running Mac OS 10.6 by the way. I'll just post the java class code here:
class RadioButtonComposite extends Composite
{
protected Button rb1, rb2, rb3, rb4;
public RadioButtonComposite(Composite parent, int style)
{
super(parent, style);
RowLayout row_layout = new RowLayout(SWT.HORIZONTAL);
row_layout.spacing = 5;
row_layout.marginLeft = 0;
row_layout.marginRight = 0;
this.setLayout(row_layout);
new Label(this, SWT.LEFT).setText("very few");
rb1 = new Button(this, SWT.RADIO);
rb2 = new Button(this, SWT.RADIO);
rb2 = new Button(this, SWT.RADIO);
rb4 = new Button(this, SWT.RADIO);
new Label(this, SWT.LEFT).setText("very much");
refreshUI();
}
public int getSelectedButtonIndex()
{
try{
if (this.rb1.getSelection() == true)
return 1;
else if (this.rb2.getSelection() == true)
return 2;
else if (this.rb3.getSelection() == true)
return 3;
else if (this.rb4.getSelection() == true)
return 4;
else
return 0;
} catch(Exception e)
{
System.out.println("Error in getSelection()");
e.printStackTrace();
return 0;
}
}
public String getAnswerString()
{
int answer = getSelectedButtonIndex();
String a_string;
switch(answer)
{
case 1: a_string = "very few"; break;
case 2: a_string = "few"; break;
case 3: a_string = "much"; break;
case 4: a_string = "very much"; break;
default: a_string = "unspecified";
}
return a_string;
}
protected void refreshUI()
{
pack();
this.getParent().layout(true);
layout(true);
}
}
Edit: Sorry guys. Only a typo..
[Updated on: Sat, 25 September 2010 17:34] Report message to a moderator
|
|
|
|
Powered by
FUDForum. Page generated in 0.02774 seconds