1

I have a class that contains two arraylists which I'm trying to store objects into, one for each object type. In my main class, I'm inserting the objects like so:

 for (int i =0; i < 3; i++)
 {
     Cat cat = new Cat("meow",i);
     Dog dog = new Dog("woof",i);
     objList.addCat(cat);
     objList.addDog(dog);
 }

My ObjectList (objList) class is setup like:

import java.util.ArrayList;

public class ObjectList {
    public ArrayList  cats;
    public ArrayList  dogs;

    public ObjectList()
    {
        this.cats   = new ArrayList();
        this.dogs   = new ArrayList();
    }

    public void addCat(Cat c)
    {
        this.cats.add(c);
    }

    public void addDog(Dog d)
    {
        this.dogs.add(d);
    }
}

I get a java.lang.NullPointerException starting at the objList.addCat(cat); line however. I printed out the cat objects properties right before this line, and both values seem to be set. I tried to see if I could just pass an int to my addCat arrayList, but I got the same error, so I assume I'm using arraylist within my class incorrectly. Is there an error in my code that's readily apparent?

1
  • Whenever you ask something related to exception, don't forget to include the stack trace, please. Commented Dec 3, 2009 at 7:00

2 Answers 2

6

Couple of things I'd suggest:

  1. Don't make your fields public;
  2. Use interfaces where possible (List instead of ArrayList);
  3. Favour immutability or at least make your data members final if you're not going to change them; and
  4. (unrelated to this) adopt Java code conventions.

So try this:

public class ObjectList {
  private final List cats;
  private final List dogs; 

  public ObjectList() {   
    cats = new ArrayList();
    dogs = new ArrayList();
  }

  public void addCat(Cat c) {
    cats.add(c);
  }

  public void addDog(Dog d) {
    dogs.add(d);
  }
}

This should avoid any external changes that might be happening. Or, better yet, use generics:

public class ObjectList {
  private final List<Cat> cats;
  private final List<Dog> dogs; 

  public ObjectList() {   
    cats = new ArrayList<Cat>();
    dogs = new ArrayList<Dog>();
  }

  public void addCat(Cat c) {
    cats.add(c);
  }

  public void addDog(Dog d) {
    dogs.add(d);
  }
}

And you are initializing your object list instance right?

ObjectList objList = new ObjectList();
for (int i=0; i<3; i++)  {
  Cat cat = new Cat("meow",i);
  Dog dog = new Dog("woof",i);
  objList.addCat(cat);
  objList.addDog(dog);
}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks. this worked. i'm using the processing.core library, and was initializing my ObjectList object (which was a property of my main class) within the processing setup function. i thought it would globally accessible this way, but it didn't seem to work. however, initializing ObjectList before the loop makes more sense anyway. thanks for the code and words of advice.
Much more complete answer. +1
3

Did you initialize your objList before using it?

ObjectList objList = new ObjectList();

(before doing your loop)

1 Comment

@cletus: which constructor? I am not talking about the ArrayList (initialized in the constructor of ObjectList), but about the instance of ObjectList

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.