Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » [resolved] NullPointerException with Button.getSelection()
[resolved] NullPointerException with Button.getSelection() [message #628981] Sat, 25 September 2010 11:41 Go to next message
Chris  is currently offline Chris Friend
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

Re: NullPointerException with Button.getSelection() [message #629088 is a reply to message #628981] Mon, 27 September 2010 07:19 Go to previous message
Daniel Krügler is currently offline Daniel KrüglerFriend
Messages: 853
Registered: July 2009
Senior Member
On 25.09.2010 13:41, Chris wrote:
> 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?

That looks like a bug to me - which SWT/Eclipse version are you using?

Greetings from Bremen,

Daniel Krügler
Previous Topic:SWT List Selection
Next Topic:Problems with PrinterData
Goto Forum:
  


Current Time: Sat Apr 27 03:23:03 GMT 2024

Powered by FUDForum. Page generated in 0.02774 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top