Results 1 to 2 of 2
  1. #1
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Null pointer exception

    Hello.
    I'm trying to add to my JLabel array list a JLabel and I'm getting a null pointer exception.
    Here is a piece of my code:


    Java Code:
    import java.applet.Applet;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.util.ArrayList;
    
    import javax.swing.*;
    
    
    public class Calculate extends Applet
    {
    	private ArrayList<JLabel>label;
    	private ArrayList<JButton>button;
    	private ArrayList<JTextField>tfield;
    	private GridBagConstraints c;
    	
    	public void init()
    	{
    		setLayout(new GridBagLayout());
    		c = new GridBagConstraints(); 
    		label.add(new JLabel("Your weight:"));
    		c.gridx = 0;
    		c.gridy = 0;
    		add(label.get(0),c);
    		
                } 
    
    }
    The exception happens in this line:
    label.add(new JLabel("Your weight:"));
    java.lang.NullPointerException
    at Calculate.init(Calculate.java:20)
    at sun.applet.AppletPanel.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    I know it's an applet but it's more a java syntax/coding question.

    Thanks in advanced.

  2. #2
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: Null pointer exception

    That was such a stupid mistake I just noticed you may delete the thread if you want.
    I didn't initalize the ArrayList.

Similar Threads

  1. Null pointer Exception
    By aortell24 in forum New To Java
    Replies: 5
    Last Post: 07-21-2012, 12:38 AM
  2. Null pointer exception
    By DBaskov in forum New To Java
    Replies: 14
    Last Post: 07-11-2011, 12:16 AM
  3. Null pointer exception
    By izzy in forum New To Java
    Replies: 5
    Last Post: 03-22-2010, 06:19 PM
  4. null pointer exception
    By jyothi.priyanka in forum New To Java
    Replies: 12
    Last Post: 03-11-2009, 06:04 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
  •