tags:

views:

144

answers:

2

Hey,

I'm a bit new to java, so please bear with me. I have a class that contains 2 arraylists which i'm trying to store objects into, one for each object type. in my main class, i'm insert 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 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?

+4  A: 

Did you initialize your objList before using it?

ObjectList objList = new ObjectList();

(before doing your loop)

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

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);
}
cletus
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.
minimalpop
Much more complete answer. +1
VonC