Code: Select all
/**
* Main Entry point for Application
*/
class App
{
/**
* Static method main() excuted when app starts
* @param String arguments
*/
public static void main(String[] args)
{
Zoo myZoo = new Zoo("Chester");
for (int i = 0; i < args.length; i++)
{
myZoo.addAnimal(new Animal(args[i]));
}
}
}
/**
* Zoo class
* Stores animals
*/
class Zoo
{
/**
* The name of this Zoo
*/
private String name;
/**
* A container for animals in this Zoo
*/
private Animal[] animals;
/**
* Constructor
* @param String name
*/
public Zoo(String myName)
{
name = myName;
}
/**
* Get the name of this Zoo
* @return String
*/
public String getName()
{
return name;
}
/**
* Add a new Animal to this Zoo
* @param String animal
*/
public void addAnimal(Animal animal)
{
animals[animals.length] = animal;
}
}
/**
* An animal which lives in the Zoo
*/
class Animal
{
/**
* The name of the animal
*/
private String name;
/**
* Constructor
* @param String name
*/
public Animal(String itsName)
{
name = itsName;
}
}Code: Select all
[d11wtq@pc-cac Zoo]$ ./runme Monkey Elephant
Exception in thread "main" java.lang.NullPointerException
at Zoo.addAnimal(runme)
at App.main(runme)