Results 1 to 4 of 4

Thread: Null Pointer

  1. #1
    theen3my is offline Member
    Join Date
    Oct 2009
    Posts
    6
    Rep Power
    0

    Default Null Pointer

    Error: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at HW2.stateChanged(HW2.java:54)

    lines 51-55:

    Java Code:
    public void stateChanged( ChangeEvent e )
    	{
    		work.setRadius (radiusSlider.getValue());
    		textArea.setText( "I want to change this text based on the new value from JSlider." );
    	}
    the line of code it is referring to is the textArea.setText("I want to change this text based on the new value from JSlider" );


    I am not very familiar with exceptions yet.

    Anything blatantly obvious? Am I using the method correctly?

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*; 
    import javax.swing.event.*;
    
    public class HW2 extends JFrame implements ChangeListener 
    {
    	private JSlider radiusSlider;  // Slider to select a Radius. 
    	private Homework2 work;  // Panel to draw Circle.
      	private JTextArea textArea;
      	private int radiusX;
      	private double diameter;
      	private double area;
      	private double circum;
      	 
      	public HW2()
      	{ 
      		super( "HW2 Circle - INSY 4305 " );
      		work = new Homework2();  // Creates Panel to draw circle.
      				
    		//Setting up JSlider to control radius value.
    		
    		radiusSlider = new JSlider( SwingConstants.HORIZONTAL, 100, 200, 150 );
    		radiusSlider.setMajorTickSpacing( 10 );
    		radiusSlider.setPaintTicks( true );
    		radiusSlider.setPaintLabels( true );
    		
    		radiusX = radiusSlider.getValue();
    		diameter = ( 2 * radiusX );
    		area = ( Math.PI )*( radiusX ) * ( radiusX );
    		circum = 2 * ( Math.PI ) * ( radiusX );
    		
    		// JText Area to display the Radius,Area,Diameter and Circumference.
    		
    		JTextArea textArea = new JTextArea( "Radius: "+ radiusX + "\nDiameter: " + diameter + "\nArea: " + area + "\nCircumference: " + circum );
    		textArea.setEditable( false );
    		
    		//Registers Jslider Event Listener.
    		
    		radiusSlider.addChangeListener(	this );
    						
    		Container container= getContentPane();
    		container.add( work, BorderLayout.NORTH );
    		container.add( textArea, BorderLayout.CENTER );
    		container.add( radiusSlider, BorderLayout.SOUTH );
    		setSize( 500, 600 );
    		setVisible( true );
    				
    	}
    	
    	[COLOR="Red"]public void stateChanged( ChangeEvent e )
    	{
    		work.setRadius (radiusSlider.getValue());
    		textArea.setText( "I want to change this text based on the new value from JSlider." );
    	}[/COLOR]
    	
    	public class Homework2 extends JPanel
    	{
    		int radius =150;
    	
    		public void paintComponent( Graphics g )
    		{
    			super.paintComponent( g );
    			g.drawOval( 150, 150, radius, radius );
    		} // paint Method Ends.
    	
    		public void setRadius( int newRadius )
    		{
    			radius=( newRadius>=0? newRadius : 150 );
    			repaint();
    		}
    
    		public Dimension getPreferredSize()
    		{
    			return new Dimension( 400, 400 );
    		}
    	
    		public Dimension getMinimumSize()
    		{
    		return getPreferredSize();
    		} 
    	}
    
    	public static void main(String args[])
    	{
    		HW2 application = new HW2();
    		application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );  
    
    	}
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You declare textArea twice, once in the class where it is never initialized:
    Java Code:
    public class HW2 extends JFrame implements ChangeListener 
    {
    	private JSlider radiusSlider; 
    	private Homework2 work; 
      	private JTextArea textArea;  // **** here ****
    and once in the constructor where it is initialized:
    Java Code:
      	public HW2()
      	{ 
      		super( "HW2 Circle - INSY 4305 " );
      		work = new Homework2();  // Creates Panel to draw circle.
      				
    		// .... code deleted to make post briefer ....
    		
    		
    		JTextArea textArea = new JTextArea( "Radius: "+ radiusX + "\nDiameter: " + diameter + "\nArea: " + area + "\nCircumference: " + circum );
    The textArea in the constructor is a completely different one from the one in the class.

    Then you try to call a method on the uninitialized variable which of course is null.

    Solution: it is OK to construct the textArea variable in the constructor, just don't redeclare it. So do this instead:

    Java Code:
      	public HW2()
      	{ 
      		super( "HW2 Circle - INSY 4305 " );
      		work = new Homework2();  // Creates Panel to draw circle.
      				
    		// .... code deleted to make post briefer ....
    		
    		// ***  Note the important difference below! ***
    		textArea = new JTextArea( "Radius: "+ radiusX + "\nDiameter: " + diameter + "\nArea: " + area + "\nCircumference: " + circum );
    p.s., thanks for using code tags!

  3. #3
    theen3my is offline Member
    Join Date
    Oct 2009
    Posts
    6
    Rep Power
    0

    Default

    Thank you so very much. I can't tell you how often I spend several hours trying to fix the most obvious things. It is really pathetic but hopefully I will get better before I get discouraged.

    Again, thank you!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You are welcome. I see nothing pathetic in your questions, as we've all stumbled with these same problems when learning. Fortunately there is a cure for programming ignorance: practice, practice, practice. Keep plugging and you'll get there.

Similar Threads

  1. Null pointer exception?
    By coffee in forum New To Java
    Replies: 4
    Last Post: 08-03-2009, 04:22 AM
  2. null pointer help
    By mayhewj7 in forum New To Java
    Replies: 5
    Last Post: 02-18-2009, 12:51 AM
  3. Null Pointer Exception
    By andre1011 in forum Advanced Java
    Replies: 4
    Last Post: 02-07-2009, 04:30 AM
  4. Null Pointer Exception
    By demiser55 in forum New To Java
    Replies: 1
    Last Post: 09-22-2008, 07:33 PM
  5. null pointer exception
    By cityguy503@yahoo.com in forum New To Java
    Replies: 4
    Last Post: 08-22-2008, 08:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •