1

I was wondering if anyone could help me out with this exception I've been getting.

I have a custom class/object named MindsparkPartnerCode with two variables named ieFFCode & chromeCode.

I would like for my Selenium Java program to create an array of MindsparkPartnerCode objects, read from a CSV file, and then store the values read from the first column of that CSV file into the ieFFCode for the first object of the array, and so on until it reaches the first blank line.

However, each time I run the program, I get an ArrayIndexOutOfBoundsException at the line record.setIEFFCode(chunks[0]);

Could anyone tell me what I'm doing wrong? I'm using old code from a co-worker of mine, which is why I'm not using an "index" for the Array. Though I'd be willing to use that if it would make the code more readable.

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

        FileReader r = new FileReader(
                "C:\\DailyLocalGuidePartnerCode.csv");
        BufferedReader bfr = new BufferedReader(r);

        String line = null;
        try {
            while ((line = bfr.readLine()) != null) {
                if (StringUtils.isBlank(line))
                    continue;
            
                System.out.println(line);
                String[] chunks = line.split(",");
                MindsparkPartnerCode record = new MindsparkPartnerCode();
                
                record.setIEFFCode(chunks[0]);
                records.add(record);
            }
        } finally {
            bfr.close();
            r.close();  
        }
1
  • I am having trouble finding the Selenium code in this question.
    – user246
    Commented Dec 2, 2014 at 4:04

2 Answers 2

0

line contains nothing but commas. That causes line.split to return a zero-length array. Since the array is empty, chunks[0] causes an ArrayOutOfBoundsException.

1
  • i included my answer up above.
    – kevin
    Commented Dec 2, 2014 at 10:12
0
        //create a list/array of the partner codes found within the source file
        List<MindsparkPartnerCode> records = new ArrayList<MindsparkPartnerCode>();
        //set the location of the file and create a read buffer
        FileReader r = new FileReader("C:\\DailyLocalGuidePartnerCode.csv");
        BufferedReader bfr = new BufferedReader(r);

        String line = null;
        try {
            while ((line = bfr.readLine()) != null) {
                if (StringUtils.contains(line, ",,,"))
                    continue;
                //System.out.println(line); //for debugging
                String[] chunks = line.split(",");
                MindsparkPartnerCode record = new MindsparkPartnerCode();
                record.setIEFFCode(chunks[0]);
                records.add(record);
                //System.out.println(chunks); //for debugging
                System.out.println("This is the current record's IE/FF Code: " + record.getIEFFCode());
                driver.get(testURL + record.getIEFFCode());
                //find the URL for the mindpark javascript implementation
                //String pageSourceURL = driver.findElement(By.("//meta[@name='keywords']")).getAttribute("content");
                //System.out.println("When using that IE/FF code in a Chrome browser, the partner code is changed to this:" + 
                    //driver.get(testURL + record.getIEFFCode()));
            }
        } finally {
            bfr.close();
            r.close();
            //System.out.println(records); //for debugging
        }
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.