0

I made a program which reads a text file which contains tweets. This text file is 16MB so it's really large. My program reads it line by line and adds words to an ArrayList. While doing this, I got an ArrayIndexOutOfBoundsException. I debugged my code to part where it throws exception. I noticed that, code keeps running after throwing exception and adds words to ArrayList.

java.lang.ArrayIndexOutOfBoundsException: 0
    at Test.main(Test.java:39)
java.lang.ArrayIndexOutOfBoundsException: 0
    at Test.main(Test.java:39)
java.lang.ArrayIndexOutOfBoundsException: 0
    at Test.main(Test.java:39)
java.lang.ArrayIndexOutOfBoundsException: 0
    at Test.main(Test.java:39)
#Jobs: 272
#Job: 269
#jobs: 225
#TweetMyJOBS: 223
#job: 155

That's how it looks on console. After multiple exceptions, my program runs and displays a result. Result is also wrong.For example, first one should be #Jobs: 4251. Does anybody have any idea why would it throw exception then keeps running?

here is the code:

try
        {
            line = reader.readLine();
            // scan = new Scanner(textinput);
            while (line != null)
            {

                String[] splitted = line.split("#"); // Line splitted according to "#".

                for (int x = 1; x < splitted.length; x++)
                {
                String[] temp = splitted[x].split(" "); // String splitted with space and first word is hashtag.
                try
                {
                   //Line 39 is here.
                    hashtags.add(temp[0]); // hashtag added to ArrayList.

                } catch (ArrayIndexOutOfBoundsException e)
                {
                    e.printStackTrace();
                }

            }
            line = reader.readLine();
            linenum++;
        }

    } catch (FileNotFoundException e1)
    {
        e1.printStackTrace();
    }
3
  • How can we possibly help you without seeing the code? Commented Feb 23, 2015 at 13:50
  • 3
    Can you show your code? What's in line 39? Commented Feb 23, 2015 at 13:50
  • while inserting in arraylist, do you insert by giving the index number? Commented Feb 23, 2015 at 13:52

2 Answers 2

1

16MB is not big. Your code keeps running because you catch the exception and just display the stacktrace. The exception results from temp being a zero length array, most likely because splitted[x] was an empty String.

A good way to debug your code is to print (or log) values during the excution, especially when an exception is thrown.

Sign up to request clarification or add additional context in comments.

4 Comments

If I try to add an empty string to ArrayList, this exception happens?
No, you're trying to access an empty array. Empty arrays don't have any elements, so there's no index you can access without getting IndexOutOfBounds. Display your variables in your catch block, and you'll see what's happening.
How can I display them on catch block?
Surely you know how to print things on the console.
0

ArrayIndexOutOfBounds exceptions are not caused by large amount of data. They are caused when you try to access an element that does not exist.

int[] apples = new int[5];
apples[7];

This would give me an ArrayIndexOutOfBoundsException because apples [7] does not exist.

2 Comments

I get the first Exception when ArrayList has size of 14053 and while adding 10596th element. It shouldn't be out of bounds, right?
I don't see how. Try having your program print out the size of the ArrayList with every iteration of the loop. Then check the value right before the exception to see if anything interesting turned up.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.