Results 1 to 6 of 6
  1. #1
    izzy is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Unhappy [SOLVED] Null pointer exception

    Basically, my code is like this:

    Java Code:
    public class Viewer()
    {
        private ArrayList<Button> buttonsList;
    
        private Button button1;
    
    public Viewer()
    {
        buttonsList = new ArrayList<Button>();
        addButtons();
    }
    
    public void addButtons()
    {
        //other code emitted
        button1 = new Button("title");
        if (button1 != null)
        {
            buttonsList.add(button1);  **
        } 
    }
    It compiles, but I get a run time error saying there's a null pointer exception where I've put the **. I'm not really sure what the problem is, I've initialised button1 and my array list so neither are null, and I've used an if statement so it only adds to array list if button1 is NOT null. i've tried initializing button1 to null, nothing seems to be working.
    Any suggestions would be much appreciated!
    Last edited by izzy; 03-17-2010 at 01:59 PM. Reason: added code tags

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Then buttonsList is null. Use Code tags when posting code.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    It's valid to add a null entry to a List, so it really doesn't matter if button1 is null or not. buttonsList is what's null, not button1. My guess is that you are either setting it to null in the ommited code, or redefining a local buttonsList and failing to initialize it.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Please send the omitted code segment here to look at.

    And also please use code tags next time when you post codes here in the forum.

  5. #5
    izzy is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default

    thanks for help, problem has been sorted

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 06:48 PM
  2. Null pointer exception?
    By coffee in forum New To Java
    Replies: 4
    Last Post: 08-03-2009, 03:22 AM
  3. Null pointer exception
    By talha06 in forum JDBC
    Replies: 5
    Last Post: 07-14-2009, 01:12 AM
  4. Help with null pointer exception
    By gammaman in forum New To Java
    Replies: 4
    Last Post: 07-14-2009, 12:23 AM
  5. null pointer exception
    By anthonym2121 in forum New To Java
    Replies: 7
    Last Post: 04-06-2009, 03:25 AM

Posting Permissions

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