【发布时间】:2016-12-01 03:21:48
【问题描述】:
我正在尝试将 fileReader 类中的站 ArrayList 中的数据移动到 CTAStops 类中的另一个 ArrayList 中。每当我测试这个时,我都会得到一个空指针异常。传输数据的正确方法是什么?
任何帮助都将不胜感激! :)
public class FileReader {
public static ArrayList<CTAStops> stations = new ArrayList<CTAStops>();
public FileReader(){
String csvFile = "CTAStops (2).csv";
File file = new File(csvFile);
try {
Scanner inputStream = new Scanner(file);
inputStream.nextLine();
inputStream.nextLine();
while (inputStream.hasNextLine()) {
String data = inputStream.nextLine();
String var[] = data.split(",");
stations.add(new CTAStops(var[0],Double.parseDouble(var[1]),
Double.parseDouble(var[2]),
var[3],Boolean.parseBoolean(var[4]),
Integer.parseInt(var[5]),
Integer.parseInt(var[6]),
Integer.parseInt(var[7]),
Integer.parseInt(var[8]),
Integer.parseInt(var[9]),
Integer.parseInt(var[10]),
Integer.parseInt(var[11]),
Integer.parseInt(var[12])));
}
inputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static ArrayList<CTAStops> getList() {
return stations;
}
}
import java.util.ArrayList;
public class CTAStops extends GeoLocation {
private String StationName; //instance variables that define a ctaStop
private double Latitude;
private double Longitude;
private String Location;
private boolean WheelChair;
private int RedLine;
private int GreenLine;
private int BlueLine;
private int BrownLine;
private int PurpleLine;
private int PinkLine;
private int OrangeLine;
private int YellowLine;
public static ArrayList<CTAStops> stations = new ArrayList<CTAStops>();
static FileReader read = new FileReader();
public CTAStops(){//default constructor
StationName = "";
Latitude = 0;
Longitude = 0;
Location = "elevated";
WheelChair = true;
RedLine = 0;
GreenLine = 0;
BlueLine = 0;
BrownLine = 0;
PurpleLine = 0;
PinkLine = 0;
OrangeLine = 0;
YellowLine = 0;
stations = FileReader.getList();
}
public CTAStops(String StationName, double Latitude, double Longitude, String Location, boolean wheelChair, int RedLine,int GreenLine,int BlueLine, int BrownLine, int PurpleLine, int PinkLine, int OrangeLine, int YellowLine){//nondefault constructor
this.StationName = StationName;
this.Latitude = Latitude;
this.Longitude = Longitude;
this.Location = Location;
this.WheelChair = WheelChair;
this.RedLine = RedLine;
this.GreenLine = GreenLine;
this.BlueLine = BlueLine;
this.BrownLine = BrownLine;
this.PurpleLine = PurpleLine;
this.PinkLine = PinkLine;
this.OrangeLine = OrangeLine;
this.YellowLine = YellowLine;
}
}
【问题讨论】:
-
你在哪一行得到错误?
-
我测试的主要方法之一
-
CTAStops.main(CTAStops.java:164) 的线程“main”java.lang.NullPointerException 中的异常
-
错误出现在 CTAStops
-
等一下...为什么你有两个主要的方法?
标签: java class arraylist nullpointerexception