当我调用 add 方法时,我不断收到空指针异常,代码看起来不错,但我不知道出了什么问题。
public class CarRunner {
public static void main(String[] args) {
TrafficLight light = new TrafficLight();
RoadRunnable run1 = new RoadRunnable(1,light);
String car="car";
for (int i =0;i<100;i++)
{
car="car"+i;
run1.add(car);
}
Thread traffic1 = new Thread(run1);
traffic1.start();
}
}
public class RoadRunnable implements Runnable{
private LinkedList<String> queue;
private int number;
private TrafficLight light;
public RoadRunnable(int roadNumber, TrafficLight aLight)
{
number = roadNumber;
light = aLight;
}
public void add(String car)
{
queue.add(car);
}
}