Jad Sapida
Jad Sapida

Reputation: 57

Storing a single csv object into an array

hey ive got a chunk of code thats supposed loads lines of csv file into an array of objects:

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;
}

but when i run it, i get a NullPointerException on the line

records[numRows] = new WarehouseItem(warehouseItem);

is there anything that i missed??

heres the WarehouseItem constructor + 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");
}
}

What my problem is the values aren't getting stored into the array records properly and im not sure why

Upvotes: 1

Views: 92

Answers (4)

user2173738
user2173738

Reputation:

You didn't initialize array, that was a cause NullPointerException in your code, but if you can't use array don't use it.

The number of lines might exceed the capacity of array, use List instead

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();

Upvotes: 2

regulus
regulus

Reputation: 1622

Because you haven't assigned anything to records[numRows]. You only defined records as an array of WarehouseItem objects. You should assign a new WarehouseItem to that index of the array before you can use.

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

Upvotes: 1

Prabhakaran Ramaswamy
Prabhakaran Ramaswamy

Reputation: 26094

You missed object creation records[numRows]=new WarehouseItem();

Your code need to be like this.

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

Upvotes: 1

Robin Green
Robin Green

Reputation: 33033

When you create an array of objects in Java, it is initialized with all nulls. That is why you get a nullpointerexception.

So you need to create an object for each array position. In fact instead of calling a method you could just make that method a constructor; that would be simpler.

I also noticed another mistake: the method sets local variables, which only exist for the lifetime of the method, when in fact it should set instance variables.

Then I noticed a third mistake: You just caught exception, and assumed that that indicated missing columns; in fact it could indicate invalidly-typed data inside a column (e.g. string instead of integer).

Upvotes: 1

Related Questions