【问题标题】:Storing a single csv object into an array将单个 csv 对象存储到数组中
【发布时间】:2013-11-06 07:19:49
【问题描述】:

嘿,我有一段代码应该将 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");
}
}

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

【问题讨论】:

  • 我如何初始化它以使最大记录数为1000000(100万)

标签: java arrays object csv io


【解决方案1】:

当您在 Java 中创建一个对象数组时,它会被初始化为所有空值。这就是为什么你会得到一个空指针异常。

所以你需要为每个数组位置创建一个对象。实际上,您可以将该方法设置为构造函数,而不是调用方法;那会更简单。

我还注意到另一个错误:方法设置了局部变量,它只存在于方法的生命周期中,而实际上它应该设置实例变量。

然后我注意到了第三个错误:您刚刚捕获了异常,并假设这表明缺少列;事实上,它可以指示列内的无效类型数据(例如字符串而不是整数)。

【讨论】:

  • 嘿,谢谢你的反馈,但你是什么意思让方法成为构造函数?对不起,我很新
  • 你知道什么是构造函数吗?
  • 是的,只是有点熟悉
  • 我的意思是您可以将processLine 更改为WarehouseItem 类的构造函数。
  • records[numRows] = new WarehousItem(warehouseItem) 而不是records[numRows].processLine(warehouseItem)。
【解决方案2】:

你错过了对象创建records[numRows]=new WarehouseItem();

你的代码需要是这样的。

while (warehouseItem != null) {
      records[numRows]=new WarehouseItem();  //here you forgot the object creation.
      warehouseItem = bufRdr.readLine();
      records[numRows].processLine(warehouseItem);
      numRows++; 
 }

【讨论】:

  • 这将产生IndexOutOfBoundsException。
  • @nikpon 是的,如果行数超过 1000000,那么它将抛出 IndexOutOfBoundsException
  • 所以,你的代码是不可预知的行为,你甚至没有抓住它。
  • OP只捕获IOException,不关心使用数组时的索引也不关心容量占用。
【解决方案3】:

因为您没有为记录[numRows] 分配任何内容。您只将记录定义为 WarehouseItem 对象的数组。 您应该先为数组的该索引分配一个新的 WarehouseItem,然后才能使用。

records[numRows] = new WarehouseItem();
warehouseItem = bufRdr.readLine();
records[numRows].processLine(wareHouseitem);

【讨论】:

  • 这将产生IndexOutOfBoundsException。
【解决方案4】:

您没有初始化数组,这是代码中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();

【讨论】:

  • 不,他只是没有创建个人WarehouseItems。
  • @Kevin 如果processLine 被调用,怎么不创建?
  • 尝试调用processLine 是NullPointerException。
  • @nikpon 不是你的,原来OP的抱怨是他没有初始化数组元素引起的NPE。
  • 是的,在我看来你只是在暗示错误是由太多行引起的。编辑以明确原始错误是由未初始化引起的,这只是一种更好的方法,然后我将收回我的 dv。
猜你喜欢
  • 2013-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-12
  • 1970-01-01
相关资源
最近更新 更多