java - 将单个 csv 对象存储到数组中

标签 java arrays object csv io

嘿,我得到了一段代码,它应该将 csv 文件的行加载到一个对象数组中:

public static WarehouseItem[] loadRecords(WarehouseItem[] records) {
  FileInputStream fileStrm = null;
  InputStreamReader rdr;
  BufferedReader bufRdr;
  int numRows = 0;
  String warehouseItem;
  String filename;

  filename = ConsoleInput.readLine("Please enter the filename (DataMillion.csv OR DataThousand.csv)");

  try {
    fileStrm = new FileInputStream(filename);
    rdr = new InputStreamReader(fileStrm);
    bufRdr = new BufferedReader(rdr);

    warehouseItem = bufRdr.readLine();
    records[numRows] = new WarehouseItem(warehouseItem);  //NULL POINTER EXCEPTION HERE
    System.out.println(records[0].toString(records[0].columnVals));
    while (warehouseItem != null) {
      numRows++;
      records[numRows] = new WarehouseItem(warehouseItem);
      warehouseItem = bufRdr.readLine();
    }
  fileStrm.close();
  }

  catch (IOException e) {
    if (fileStrm != null) {
      try { 
    fileStrm.close(); 
      } catch (IOException ex2) {}
    }
    System.out.println("Error in file processing: " + e.getMessage());
    }
    main(null);
    return records;
}

但是当我运行它时,我得到了一个 NullPointerException

records[numRows] = new WarehouseItem(warehouseItem);

有什么我遗漏的吗??

这里是 WarehouseItem 构造函数 + toString:

public class WarehouseItem {
  String[] columnVals;
  int numColumns = 5;

public WarehouseItem(String warehouseItem) {
  String key, brand, model, price, weightInKG;
  int intWeightInKG;
  double doublePrice; 
  StringTokenizer strTok;

  strTok = new StringTokenizer(warehouseItem, ",");
    key = strTok.nextToken();
    brand = strTok.nextToken();
    model = strTok.nextToken();
    intWeightInKG = Integer.parseInt(strTok.nextToken());
    doublePrice = Double.valueOf(strTok.nextToken());

    weightInKG = String.valueOf(intWeightInKG);
    price = String.valueOf(doublePrice);
  String[] columnVals = {key, brand, model, weightInKG, price};

  if(columnVals.length != 5)
    throw new IllegalStateException("Invalid CSV: not enough columns");
}

public String toString(String[] columnVals) {
  return ("Key:     " + columnVals[0] + "\n" + 
      "Brand:   " + columnVals[1] + "\n" + 
      "Model:   " + columnVals[2] + "\n" + 
      "Weight:  " + columnVals[3] + "\n" + " kg" + 
      "Price:   " + "$" + columnVals[4] + "\n");
}
}

我的问题是值没有正确存储到数组记录中,我不确定为什么

最佳答案

您没有初始化数组,这是您代码中的 NullPointerException 原因,但如果您不能使用数组,请不要使用它。

行数可能超过数组的容量,请改用List

List<WarehouseItem> records = new ArrayList<>();

String line = bufRdr.readLine();
while (line != null) {
  WarehauseItem warehauseItem = new WarehauseItem();
  records.add(warehauseItem);
  warehauseItem.processLine(line);
  line = bufRdr.readLine();
}
numRows = records.size();

关于java - 将单个 csv 对象存储到数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19619212/

相关文章:

java - 如何在 Java 7 中使用集合文字?

java - 使用 <s : iterator> tag 附加静态值

python - 在 Python 中使用对象作为字典中的键 - 哈希函数

c# - 为什么传入带有 ref 关键字的方法的实例在创建新对象后可以保持不变?

java - Logstash-logback 事件特定的自定义字段(使用 StructuredArguments)未添加到 JSON

java - Spring 中的命令对象

javascript 在数组中查找具有特定属性的对象

java - 在 Java 中,如何通过对象的构造函数将对象添加到数组?

Java数组——消除中间的连续数

ruby-on-rails - 如何在 erb 模板中呈现数组的最佳方式?