Alright my program is giving me a hard time and I will have to briefly explain what it does and when the exception is thrown to give you a better understanding.

My program goes off and reads a csv file, once it reads it, it adds the information to my bean and then displays the bean on my main program. The csv file is constantly changing with what is inside it, thus I set a timer on the part where my program reads the csv file.

Here is an example of where the exception is not thrown. Lets say in my csv file there are only 2 lines of data. Then I change the csv file to contain 6 lines of data, the main program updates the beans and runs fine without exceptions or warnings.

Here is an example of where the exception is thrown. When the csv file starts out with 6 lines of data and then I remove some lines (any amount) then the main program does not update my beans and throws this exception: ArrayIndexOutOfBoundsException: 1


My csv file always contains only 3 columns of information, however the lines (rows) will vary.

I will post the source where this exception happens. And hopefully you guys will be able to help out.

public ParseResult[] parseResult(BufferedReader reader) throws FileNotFoundException, IOException {
    

        ParseResult[] out;
        ArrayList<ParseResult> infoFound = new ArrayList<ParseResult>();
        
        try {
            boolean bHeadersDone = false;
            while (reader.ready()) {
                String beanInfo = reader.readLine();
                
                if (!bHeadersDone) {
                    if (beanInfo.contains("Ping")) {
                        bHeadersDone = true;
                    }
                } 
                else {
                    String[] values = beanInfo.split("," , 3);
                    infoA = values[0];
[B]                    infoB = values[1];[/B]
                    infoC = values[2];



                    if (!infoC.contains("[n/a]")) {
                        ParseResult pr = new ParseResult(hostname, ip); 
                        infoFound.add(pr);

                    }                   
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        out = (ParseResult[])infoFound.toArray(new ParseResult[infoFound.size()]);   


        return out;
        
    }

I have bolded the source where this exception 'supposedly' happens.

Recommended Answers

All 12 Replies

Can't see the bolding. But a guess: are you trying to read as many lines from the file as there are in the bean?

Hmm I can see it bolded, but instead of doing it that way what tag do I use to show the number next to each line?

The code that I bolded was:

infoB = values[1];

To answer your question, I have the beans on a loop. Basically it takes the length of the result arraylist and adds a bean for every line. In short for every 'infoA' it will place a bean on the main program.

Not sure if that completely answers your question, if not I didn't fully understand it.

It looks like it's reading a line that contains no commas - so the values[] array is only 1 element. Try printing the beanInfo String just before the line where the Ex is thrown

The thing is that its the exact same information, all I'm doing is removing parts of it or adding parts back to test if it works. When I remove or add data I do it be lines.

However each line contains the right amount of commas.

Here is something odd, when I printed out beanInfo it gave me a full list of the information in the csv file. The reason this is weird is because I removed half of it right before the timer set it to reading the info in the csv.

So it works when more information is added to the file but not when information is taken away from the file.


*Edit*

However when I just print out infoB it gives just the information that is actually in that column.


Do you think it might be due to my arrayList perhaps already having that information added to it and maybe I need to clear it after or before each timer scan?

However each line contains the right amount of commas.

If your understanding of how your code were correct then it would work, wouldn't it? Somewhere there is a mismatch between what you think ought to happen and what really does. You need to question all your assumptions and test them one by one.
If every beanInfo contains 3 strings the the values[] array will always have 3 elements, and element [1] will never be out of bounds at that line. Add debugging prints (or use your IDE's debugger) to see what's in beanInfo when the Ex is thrown, then delve deeper to see why.

Alright I'll keep digging deeper and question each part.

Also note I edited my post above probably as you were posting. I'm pointing it out because do you think that it could be the arrayList?

... do you think that it could be the arrayList?

Maybe - that's a part of the code I don't have. But what you say sounds credible for why the old bean info is still there.

I still think that if values[0] is OK and values[1] isn't then values has exactly 1 element. If it's returned from a split then the String being split has only 1 value. If said String comes from a readLine then... etc etc

commented: Thank you for the help on this problem. Mostly thank you for your time as its a valuable resource +1

Alright I actually found out a few problems.

As for the ArrayIndexOutOfBounds exception, you seemed to be on the right track. The reason why I couldn't find the problem, well the only thing I can come up with is that when deleting the data it must have stopped the line after the last piece of data. Leaving a new line ('/n') as a line of data instead of going all the way up to the previous line. So that part is fixed and I'm no longer getting that exception.

That was one of those tedious ones where you have to pay very close attention to whats happening to catch.

I was also right that I had to clear my arrayLists. In order to clear beans and add beans back correctly I had to clear my 2 arrayLists and the panel that I add the bean to in my timer.

Now I'm stuck with one problem, it only updates my beans through 2 cycles :s after that the timer seems to keep going (i have it displaying the countdown) however when it hits the time it is supposed to update the beans on the 3rd cycle it doesn't do it.

Any idea what the problem could be there? If you need me to post source of my method with the timer let me know

Sorry, no ideas at all about latest problem without actual code (and I'm off to bed now and then away, so nothing for a day or two anyway)

I have fixed the timer issue by putting a timer around my method that sets off the update for the beans.

Which leads me to my next question, is it bad practice to have a timer within a timer?

Not as far as I know.

Alright then I am set and this issue is solved.

Thank you for all your help, it is much appreciated

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.