當我調用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);
}
}
的可能重複的[這是一個空指針異常?](http://stackoverflow.com/questions/218384/what- is-a-null-pointer-exception) –