我正在尝试将数据从ArrayList
类中的工作站fileReader
移动到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;
}
}
答案 0 :(得分:-1)
为什么两个类中有两个主要方法?
我猜CTAStops是你的主类?
我要做的是拥有FileReader类的构造函数,其中filename作为参数,并且其中有一个返回ArrayList的方法。
使用输入文件名(args [0])在CTAStops中创建FileReader实例,然后调用FileReader的方法并将其设置为等于任何ArrayList。
同时尝试避免使用FileReader / FileWriter作为类名,因为它可能与java FileReader和FileWriter类/构造函数冲突。
你应该从中得到一个想法:
public class FileProcessor {
private ArrayList<CTAStops> stations = new ArrayList<CTAStops>();
private String filename;
public FileProcessor(String filenameIn) {
filename = filenameIn;
}
public ArrayList<CTAStops> getStations() throws IOException {
String csvFile = filename;
File file = new File(csvFile);
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
String var[] = line.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])));
}
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return stations;
}
public ArrayList<CTAStops> getList() {
return stations;
}
}
CTAStops:
import java.io.IOException;
import java.util.ArrayList;
public class CTAStops {
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>();
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;
}
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;
}
public static void main(String[] args) throws IOException {
FileProcessor reader = new FileProcessor("inpp.txt");
stations = reader.getStations();
//Testing
CTAStops a = new CTAStops();
a=stations.get(0);
System.out.println(a.StationName +" "+ a.Latitude);
}
}