Hi all. Learning Java - sorry for my stupidity if you see any.

Here is the code:

private static void readFile() throws Exception {
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DATE, DAYS);

    String fOther = "filename.csv";
    String fOtherImp = "filename_imp.csv";

    LineNumberReader lineCounter = new LineNumberReader(
    new InputStreamReader(new FileInputStream(fOther)));
    String nextLine = null;

    try {
	while ((nextLine = lineCounter.readLine()) != null) {
	if (nextLine == null)
              System.out.printf("Null line");
              continue;
        }
	if (lineCounter.getLineNumber() < 10001) {

          BufferedReader br = new BufferedReader(new FileReader (fOther));
          CsvWriter csvOutput = new CsvWriter(new FileWriter(fOtherImp, true), ',');
          log.info("File is OK.  Creating new import file.");

          // Create Headers
          csvOutput.write("Date");
          csvOutput.write("Resort");
          csvOutput.write("Source");
          csvOutput.write("Medium");
          csvOutput.write("Campaign");
          csvOutput.write("Keyword");
          csvOutput.write("Pageviews");
          csvOutput.write("Unique Page");
          csvOutput.write("Exits");
          csvOutput.write("Time on Page");
          csvOutput.write("Bounces");
          csvOutput.endRecord();
--> 
         //String strLine;
          String strLine = null;
          String[] strLine1;

          ArrayList<String> list = new ArrayList<String>();

          while ((strLine = br.readLine()) != null)   {
                    list.add(strLine);
          }

           for (int i = 0; i < list.size(); i++) {
               Pattern p = Pattern.compile("(?<=[c|C]{2}\.)([0-9]+)?(?=\_.|.[html|xml])");
               String row = list.get(i);
               String col1 = row.split(",")[0];
               String col2 = row.split(",")[1];
               String col3 = row.split(",")[2];
               String col4 = row.split(",")[3];
               String col5 = row.split(",")[4];
               String col6 = row.split(",")[5];
               String col7 = row.split(",")[6];
               String col8 = row.split(",")[7];
               String col9 = row.split(",")[8];
               String col10 = row.split(",")[9];
               String col11 = row.split(",")[10];
               if (row == null) {
                    System.out.printf("Null line");
                    continue;
                   }
                String url = row.split(",")[1];
                String resortid = new String();
                //String result = new String();
                Matcher m = p.matcher(url);
                while (m.find()) {
                    resortid = m.group();
                    // Add Data
                    csvOutput.write(col1);
                    csvOutput.write(resortid);
                    csvOutput.write(col3);
                    csvOutput.write(col4);
                    csvOutput.write(col5);
                    csvOutput.write(col6);
                    csvOutput.write(col7);
                    csvOutput.write(col8);
                    csvOutput.write(col9);
                    csvOutput.write(col10);
                    csvOutput.write(col11);
                    csvOutput.endRecord();

                    }
 ---->
             }
        log.info(fOtherImp + " created.");
                csvOutput.close();

        } else {
            log.error("File exceeded 10001 lines. File rejected");
        }
       	} catch (Exception ex) {
          log.error(ex);
        //}  catch (ArrayIndexOutOfBoundsException e) {
	//log.error( "OutOfBounds: "  +  e.getMessage());
   }

I think I know which part of the code (denoted by the --->) that is causing the problem. But I am missing something because of my lack of knowledge :(. Thanks for any info!

Print the whole of the out-of-bounds exception ( e.printStackTrace(); ) in your catch block - that will tell you the exact line of your program on which it happened.

Edited 5 Years Ago by JamesCherrill: n/a

The stack trace would be useful.
However, the problem might be here:

String url = row.split(",")[1];

This gets the second item from the string: if the original string is "XXXXX, YYYY" this would set url to "YYYY". Is this what you intend? If row does not have a comma, this will give you the out of bounds exception.


Similarly, in this part:

String row = list.get(i);
               String col1 = row.split(",")[0];
               String col2 = row.split(",")[1];
               String col3 = row.split(",")[2];
               String col4 = row.split(",")[3];
               String col5 = row.split(",")[4];
               String col6 = row.split(",")[5];
               String col7 = row.split(",")[6];
               String col8 = row.split(",")[7];
               String col9 = row.split(",")[8];
               String col10 = row.split(",")[9];
               String col11 = row.split(",")[10];

You're assuming that your data is well-formed. If row isn't cooperative, again, you'll have that exception.
Also, you might want to do something like this instead, to save some gratuitous method calls:

String[] row = list.get(i).split(",");
               String col1 = row[0];
               String col2 = row[1];  

               ....

Print the whole of the out-of-bounds exception ( e.printStackTrace(); ) in your catch block - that will tell you the exact line of your program on which it happened.

You rock! Thank you! Now I know what is causing the issue and I am beating my head on the desk because I should have known it :D

thanks again!!

Just one more word on this... best practice says to make your try block as short as possible, and catch exactly the exceptions you're anticipating.

That is, something like this:

{
... safe code ...
try {
  potentiallyUnsafeOperation();
  }
catch (NowYou'veDoneItException e)
{
  //log exception and try to recover
}
... more safe code...
}

After dealing with this issue, you know one of the good reasons for this.

Edited 5 Years Ago by jon.kiparsky: n/a

Comments
gud!
This article has been dead for over six months. Start a new discussion instead.