0

I'm tring to print all the elements in the Car array. But the console shows it has null pointer problem.

Assume I have a class named Car, and there is nothing wrong in Car class. CarLeadership class manipulates group of Car objects. I put them in an array called carArr(one attribute in CarLeadership class).

public class CarLeadership {
private Car[] carArr;
private int position; //point to the current element of carDatabase

//pass length of array
public CarLeadership(int maxCar) {  
    Car[] carArr = new Car[maxCar];
    position=0;                                                   
}

//parameterized constructor
public CarLeadership(Car[] carDatabase,int position) {
    carArr = carDatabase;
    this.position = position;
}

//copy constructor
public CarLeadership(CarLeadership c) {
    this.carArr = c.getCarArr();
    this.position = c.position;
} 

public void setCarArr(Car[] carArr) {
    this.carArr = carArr;
}

public void setCarArrElements(int index,Car carObj) {
    if(index >= carArr.length) {
        System.out.println("Index out of bounds");
        System.exit(0);
    }
    carArr[index] = carObj;
}

public Car[] getCarArr() {
    //deep copy
    Car[] anotherCarArr = new Car[this.carArr.length];
    for(int i=0; i<anotherCarArr.length;i++)
        anotherCarArr[i] = this.carArr[i];
    return anotherCarArr;
}

public void setPosition(int position) {
    this.position = position;
}

public int getPosition() {
    return position;
}

public String toString() { //instance has carArr
    String s="";
    for(int i=0;i<carArr.length;i++) 
        if(carArr[i]!= null) 
            s += ("Car: # "+ i + "\nMake: " +this.carArr[i].getMake()
                    +"\nModel: "+this.carArr[i].getModel()
                    +"\nYear: "+this.carArr[i].getYear()
                    +"\nPrice: "+this.carArr[i].getPrice()+"\n");
    return s;
}
}

//driver class
public class CarLeadershipDriver {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    CarLeadership cc = new CarLeadership(5);
    cc.setCarArrElements(0,new Car("aa","cc",2018,242));
    cc.setCarArrElements(1,new Car("aa","aa",2018,242));
    cc.setCarArrElements(2,new Car("aa","cc",2018,242));    
    System.out.println(cc);
}
}

The error message:

 Exception in thread "main" java.lang.NullPointerException
    at CarLeadership.setCarArrElements(CarLeadership.java:31)
    at CarLeadershipDriver.main(CarLeadershipDriver.java:7)
App Dev Guy
  • 5,396
  • 4
  • 31
  • 54
  • 2
    You never initialize `carArr`. Instead, you initialize a local variable with the same name that is hiding the member variable. – azurefrog Jul 16 '19 at 17:24
  • Thank you very much!!! I have deleted the Car[] in constructor and everthing goes well now! – sunyanL1236 Jul 16 '19 at 17:28

3 Answers3

1

Change your constructor

public CarLeadership(int maxCar) {
    Car[] carArr = new Car[maxCar];
    position = 0;
}

to

public CarLeadership(int maxCar) {
    carArr = new Car[maxCar];
    position = 0;
}

that's all.

Now your program run with output:

Car: # 0
Make: aa
Model: cc
Year: 2018
Price: 242
Car: # 1
Make: aa
Model: aa
Year: 2018
Price: 242
Car: # 2
Make: aa
Model: cc
Year: 2018
Price: 242
josejuan
  • 9,338
  • 24
  • 31
1

The issue is that you are initializing a local copy of carArr in your constructor.

Replace

//pass length of array
public CarLeadership(int maxCar) {  
    Car[] carArr = new Car[maxCar];
    position=0;                                                   
}

with

//pass length of array
public CarLeadership(int maxCar) {  
    carArr = new Car[maxCar];
    position=0;                                                   
}

or if you want to be more explicit

//pass length of array
public CarLeadership(int maxCar) {  
    this.carArr = new Car[maxCar];
    position=0;                                                   
}

newbane2
  • 130
  • 5
0

You created new array in your constructor, while you should assign the declared size to the existing array.

this.carArr = new Car[maxCar];

Mohamed Sweelam
  • 1,109
  • 8
  • 22