[英]transferring arraylist from one class to another
I'm trying to move the data from the stations ArrayList
in the fileReader
class into the another ArrayList in the CTAStops
class. 我试图从站到移动数据
ArrayList
中fileReader
类为在另一个ArrayList的CTAStops
类。 Whenever I test this i get a null pointer exception. 每当我对此进行测试时,都会得到一个空指针异常。 What would be the correct way to transfer the data over?
传输数据的正确方法是什么?
Any help at all would really be appreciated! 任何帮助都将不胜感激! :)
:)
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;
}
}
Why do you have two main methods in two classes? 为什么在两个类中有两个主要方法?
I guess CTAStops is your Main class? 我猜CTAStops是您的主班?
What I'd do is have a constructor for FileReader class with filename as parameter and in it have a method returning ArrayList. 我要做的是为FileReader类构造一个以filename为参数的构造函数,并且在其中有一个返回ArrayList的方法。
Create an instance of FileReader in CTAStops with the input file name(args[0]), then call the method of FileReader and set it equal to any ArrayList. 在CTAStops中使用输入文件名(args [0])创建FileReader的实例,然后调用FileReader的方法并将其设置为等于任何ArrayList。
Also try avoiding FileReader/ FileWriter as class name as it can conflict with the java FileReader and FileWriter classes/constructors. 还要避免将FileReader / FileWriter作为类名,因为它可能与java FileReader和FileWriter类/构造函数冲突。
You should get an idea from this: 您应该从中得到一个想法:
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: 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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.