I have this program which has a class Ant where:
The default constructor initialises the instance variables queens to only 1
queen named “Beth” and colonySize
to 100,000.
The defined constructor takes in two parameters and initialises the corresponding instance variables.
The method display
, shows the information about an ant in the format
below:
This ant colony has the following queens:
Queen 1: Beth
The colour of the ants: black
The approximate colony size: 100000
Here is what I have done:
public class Ant {
private String[] queens;
private String colour= "black";
private int colonySize;
public Ant(){
queens[0]= "Beth";
colonySize= 100000;
}
public Ant(String[] queens, int colonySize){
this.queens[0]= queens[0];
this.colonySize= colonySize;
}
public void display(){
System.out.println("Queen 1: "+ this.queens[0]);
System.out.println("Colour of the ants: "+colour);
System.out.println("The size of the colony: "+ this.colonySize);
}
}
The prob is arising when I am calling it in the main.
Main Class
public class MainAnts {
public static void main(String[] args) {
// TODO Auto-generated method stub
Ant obj= new Ant();
obj.display();
}
}
I am getting a Null Pointer Exception (because I suppose that's not the right way to call the display method in the main since it has an array variable).