Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

java.lang.NullPointerException with JRadioButton

843804Apr 30 2005 — edited Apr 30 2005
Hi,

Well, I'm using JRadioButton for the first time. So far, I haven't had much luck. I'm trying to check whether one of the radio buttons I have in a ButtonGroup are selected. I have one of the two radiobuttons selected from the get-go.

The following class is an inner-class. There error is caused when I call before.isSelected() in the getSelection() function.
	private class radioButtonClass extends JPanel
	{
		private JRadioButton after;
		private JRadioButton before;
		private ButtonGroup group;

		public radioButtonClass()
		{

			JRadioButton before = new JRadioButton("Before the current row?", true);
			//before.setSelected( true );
			JRadioButton after = new JRadioButton("After the current row?  ");

			group = new ButtonGroup();
	    	        group.add( before );
	    	        group.add( after );

			this.add( before );
			this.add( after );
			//this.add( group );
			
			//setBackground( new Color(234, 2, 43) );
		}

		public Dimension getPreferredSize()
		{
			return new Dimension( 150, 80 );
		}

		public Dimension getMaximumSize()
		{
	    	return getPreferredSize();
	    }
	    
	    public int getSelection()
	    {
	    	//System.out.println("getSelection()");
	    	//System.out.println("Button Count: " + group.getButtonCount() );
	    	
	    	//System.out.println("isSelected" + group.isSelected( after.getModel() ) + "... yup!");
	    	

                //java.lang.NullPointerException is raised/thrown in this function below.
	    	if( before.isSelected() )
	    	{
	 			System.out.println("1");
	    		return 1;
	    	}
	    		
	    	else
	    	{
	    		
	    		System.out.println("2");
	    		 return 2;
	    	}
	    	
	    }

	}
I'm guessing this is a really easy error, yet I have been unable to find the cause. Any help would be much appreciated.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 28 2005
Added on Apr 30 2005
2 comments
242 views