【问题标题】:transferring arraylist from one class to another将arraylist从一个类转移到另一个类
【发布时间】: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


【解决方案1】:

为什么两个类中有两个主要方法?

我猜 CTAStops 是你的 Main 类?

我要做的是使用文件名作为参数的 FileReader 类的构造函数,其中有一个返回 ArrayList 的方法。

在CTAStops中用输入文件名(args[0])创建一个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);


   }
}

【讨论】:

  • 你能详细解释一下吗?只是有点失落
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多