Results 1 to 6 of 6
  1. #1
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default Simple swing program, help please!

    Hi,

    This is a simple swing program with just a label and a button.
    When I click the button I want the label to change to "a".

    But instead it throws a null pointer exception at runtime (compiles fine)
    please help!
    The program looks big because i am also learning GridBagLayout, I have bolded the line that I want to change.

    Java Code:
    import java.awt.BorderLayout;
    
    
    public class Test extends JFrame {
    
    	private JPanel contentPane;
    
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					Test frame = new Test();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    
    	/**
    	 * Create the frame.
    	 */
    	public Test() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 450, 300);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    		GridBagLayout gbl_contentPane = new GridBagLayout();
    		gbl_contentPane.columnWidths = new int[]{0, 0, 0, 0, 0};
    		gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0};
    		gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
    		gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
    		contentPane.setLayout(gbl_contentPane);
    		
    		[COLOR="Red"][B]JLabel lblNewLabel = new JLabel("New label");[/B][/COLOR]
    		GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
    		gbc_lblNewLabel.insets = new Insets(0, 0, 5, 0);
    		gbc_lblNewLabel.gridx = 3;
    		gbc_lblNewLabel.gridy = 2;
    		contentPane.add(lblNewLabel, gbc_lblNewLabel);
    		
    		JButton btnNewButton = new JButton("New button");
    		btnNewButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				but1ActionPerformed(arg0);
    			}
    		});
    		GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
    		gbc_btnNewButton.insets = new Insets(0, 0, 0, 5);
    		gbc_btnNewButton.gridx = 1;
    		gbc_btnNewButton.gridy = 5;
    		contentPane.add(btnNewButton, gbc_btnNewButton);
    	}
    
    	
    	 private javax.swing.JLabel lblNewLabel;
    
    		private void but1ActionPerformed(ActionEvent arg0) {
    			lblNewLabel.setText("a");			
    		}
    }
    Last edited by N00Bie; 02-17-2011 at 04:42 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,015
    Blog Entries
    7
    Rep Power
    20

    Default

    You both defined a local variable in your constructor and as a member variable. You initialize that local variable but leave the member variable equal to null. Remove that local variable lblNewLabel from your constructor.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    if I remove this:
    JLabel lblNewLabel = new JLabel("New label");
    from the constructor I am getting more errors...

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,015
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by N00Bie View Post
    if I remove this:

    from the constructor I am getting more errors...
    You should initialize your member variable in your constructor, just don't use a local variable with the same name. In your constructor do this:

    Java Code:
    lblNewLabel = new JLabel("New label");
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    What Jos is saying is don't declare the variable twice. Declare it once in the class:

    This is variable declaration and initialization
    Java Code:
    MyFoo myFoo;  // this is variable declaration
    
    myFoo = new MyFoo();  // this is variable initialization or construction
    
    MyFoo myFoo = new MyFoo();  // this is both variable initialization and declaration combined
    And initialize it once by calling new XXX() once either in the class or the constructor.

    Think about why this matters -- when you declare it again in the constructor as you're doing, you're creating a completely new variable, one that is completely distinct from the one declared in the class itself. The variable declared in the constructor is only visible within the constructor and does not exist outside of it.


    Edit: Did not see Jos' reply. Yeah, what he said.

  6. #6
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

Similar Threads

  1. Please help with simple program.. Very simple.
    By jonytek in forum New To Java
    Replies: 7
    Last Post: 02-14-2011, 12:44 AM
  2. Simple program help
    By jtyler in forum New To Java
    Replies: 3
    Last Post: 09-20-2010, 07:43 AM
  3. Java Swing Simple Addition help
    By dbasenoob in forum New To Java
    Replies: 4
    Last Post: 04-27-2010, 01:26 PM
  4. simple program
    By blastoff in forum New To Java
    Replies: 5
    Last Post: 04-14-2010, 11:25 PM
  5. Java Swing JTable Simple Doubt
    By hemanthjava in forum AWT / Swing
    Replies: 1
    Last Post: 11-26-2008, 01:46 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
  •