I'm creating a class that has a List of objects as a constructor parameter, but I'll getting a null pointer exception when I try to use the initialized List. My class
public class ControlUnit {
private List<Sensor> sensors;
public void constructor(List<Sensor> sensorList) {
sensors = sensorList;
}
public void pollSensors() {
for (Sensor sensor : sensors) {
System.out.println("do something");
}
}
}
used like this:
List<Sensor> sensors = new ArrayList<Sensor>();
sensors.add(new FireSensor());
sensors.add(new SmokeSensor());
ControlUnit unit = new ControlUnit();
unit.pollSensors();
and I'm getting the error
java.lang.NullPointerException at ControlUnit.pollSensors(ControlUnit.java:15)
What am I missing in my constructor?