0

I have data like this:

Redfish Creek Snow Pillow,2013-11-21 17:00:00,-9.9,557,321,125,14.7

However, sometimes it has NULL items in the columns... see the last two.

Brenda Mines Snow Pillow,2013-11-24 05:00:00,-5.065,363,58,,

These are system.out.println(DATA); so I can see what is happening.

I am using OpenCSV to parse this. The data looks to be coming through correctly.

My error is:

11-24 00:08:23.894: E/AndroidRuntime(8423): java.lang.ArrayIndexOutOfBoundsException: length=5; index=5

Here I get all the info with OpenCSV into array:

URL siteURL = new URL(bccsvfile);
                BufferedReader in = new BufferedReader(new InputStreamReader(
                        siteURL.openStream()));
                CSVReader csvreader = new CSVReader(in);
                BCSiteDataList = csvreader.readAll();
                csvreader.close();

Here is where i loop through and add to String Array List:

ArrayList updates = new ArrayList(); for (int i = 1; i < bclistsize; i++) { String bcData[] = BCSiteDataList.get(i);

                  String BCSiteDataResults = bcData[0] + "," + bcData[1]
                          + "," + bcData[2] + "," + bcData[3] + ","
                          + bcData[4] + "," + bcData[5] + "," + bcData[6];

                  updates.add(BCSiteDataResults);
              }

I get the results (with or without data after each comma) from the above:

Redfish Creek Snow Pillow,2013-11-21 17:00:00,-9.9,557,321,125,14.7

Here is my check for the data:

for (int z = 0; z < updates.size(); z++) {

    String items = updates.get(z);
    chartData = items.split(",");

    //Array initialized
    String chartData[] = new String[7];
   ///FYI I also tried String chartData[] = {"0","0","0","0","0","0","0"}

        //check if there is data... if not, make it 0 ... snowWaterEqu[z] is a double
        if(chartData[5].length() > 0) {
          snowWaterEqu[z] = Double.parseDouble(chartData[5]) / 10;
        } else {
          snowWaterEqu[z] = 0;
        }

        //add the series to chart with achartengine
        SWESeries.add(z, snowWaterEqu[z]); ....

Since there is a comma in the output (...w,x,y,z or empty ...w,x,,) why isn't my if else picking that length between the deliminator to be >0? Since it is NULL, shouldn't my else pop in there and add 0? Also, I was under the impression, if I initialized and array with a number the missing ones would be 0. What am I missing here?

jasonflaherty
  • 1,924
  • 7
  • 40
  • 82
  • Post your real code. The code you posted doesn't do anything with OpenCSV, and every element of chartData would be null if that was your real code. – JB Nizet Nov 24 '13 at 09:14
  • @JBNizet added more code. I get the correct data, the NULL columns just are not being changed to 0. Thanks for the comment. – jasonflaherty Nov 24 '13 at 15:19

1 Answers1

0

So this question is simply answered like my other question: Populate String[7] with data even if I don't have 7 items from my parser?

Empty and Null are different. I was checking for NULL and I needed to check for "".

Hope it helps someone.

Community
  • 1
  • 1
jasonflaherty
  • 1,924
  • 7
  • 40
  • 82