-1

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?

wogsland
  • 9,106
  • 19
  • 57
  • 93
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – jrook Mar 03 '20 at 18:08
  • 6
    Naming a method `constructor` does not make it a constructor. – kaya3 Mar 03 '20 at 18:10
  • Worth noting that if you made `sensors` `final`, the compiler would have picked up the error. – Tom Hawtin - tackline Mar 03 '20 at 18:12

5 Answers5

3

The constructor is defined completely wrong. Check the following code snippet.

public class ControlUnit {

    private List<Sensor> sensors;

    public ControlUnit(List<Sensor> sensorList) {
        sensors = sensorList;
    }

    public void pollSensors() {
        for (Sensor sensor : sensors) {
            System.out.println("do something");
        }
    }
}

And use it like this.

List<Sensor> sensors = new ArrayList<Sensor>();
    sensors.add(new FireSensor());
    sensors.add(new SmokeSensor());
    ControlUnit unit = new ControlUnit(sensors);
    unit.pollSensors();
davidm
  • 1,570
  • 11
  • 24
2
public void constructor(List<Sensor> sensorList) {

This is not a constructor declaration. You need

public ControlUnit(List<Sensor> sensorList) {

and then call it with the list you want to use.

daniu
  • 14,137
  • 4
  • 32
  • 53
1

You're constructing your class wrongly.

A constructor in Java takes the form of a method with the name of the class and without any return type:

public ControlUnit(List<Sensor> sensorList) { ... }
Huatxu
  • 111
  • 5
0

You need to pass the list to the object when creating an instance of it as a parameter so like this: ControlUnit unit = new ControlUnit(sensors); and also change the constructor method to

public ControlUnit(List<Sensor> sensorList) {
    sensors = sensorList;
}
InsertKnowledge
  • 1,012
  • 1
  • 11
  • 17
0

For your class ControlUnit you have not got a constructor, so the Idea which you use will generate an empty constructor for your class that you call when you initialize your ControlUnit object here: ControlUnit unit = new ControlUnit()

So you will not initialize your List<Sensor> sensors list and you will not be able to iterate through it because it is pointing to null.

In order to do it properly, you have to write your constructor in the right way like this:

public class ControlUnit {

    private List<Sensor> sensors;

    public void pollSensors() {
        for (Sensor sensor : sensors) {
            System.out.println("do something");
        }
    }

    public void ControlUnit (List<Sensor> sensorList) {
        sensors = sensorList;
    }
}

Your constructor method has to be the same name as your class. When you call new ControlUnit(), you will call that constructor. But in this case, you have a parameter there, so we have to pass it at the calling like this: ControlUnit unit = new ControlUnit(sensors);

So when the constructor will run, it will assign the value to your list, so you can iterate through it.

AME
  • 302
  • 2
  • 17