0

I'm trying to use the split method over a string. This throws an ArrayOutOfBoundsException and I have no clue why. I've tried many strings and many separators.

    String temp = "abcd&efgh&ijkl&mnop";
    String[] tempArr = temp.split("ijk");

This is the error log:

10-13 12:20:30.619: E/AndroidRuntime(775): FATAL EXCEPTION: main
10-13 12:20:30.619: E/AndroidRuntime(775): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lino/com.example.lino.LinesActivity}:    java.lang.ArrayIndexOutOfBoundsException
10-13 12:20:30.619: E/AndroidRuntime(775):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-13 12:20:30.619: E/AndroidRuntime(775):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-13 12:20:30.619: E/AndroidRuntime(775):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-13 12:20:30.619: E/AndroidRuntime(775):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-13 12:20:30.619: E/AndroidRuntime(775):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-13 12:20:30.619: E/AndroidRuntime(775):  at android.os.Looper.loop(Looper.java:123)
10-13 12:20:30.619: E/AndroidRuntime(775):  at android.app.ActivityThread.main(ActivityThread.java:3683)
10-13 12:20:30.619: E/AndroidRuntime(775):  at java.lang.reflect.Method.invokeNative(Native Method)
10-13 12:20:30.619: E/AndroidRuntime(775):  at java.lang.reflect.Method.invoke(Method.java:507)
10-13 12:20:30.619: E/AndroidRuntime(775):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-13 12:20:30.619: E/AndroidRuntime(775):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-13 12:20:30.619: E/AndroidRuntime(775):  at dalvik.system.NativeStart.main(Native Method)
10-13 12:20:30.619: E/AndroidRuntime(775): Caused by: java.lang.ArrayIndexOutOfBoundsException
10-13 12:20:30.619: E/AndroidRuntime(775):  at com.example.lino.LinesActivity.onCreate(LinesActivity.java:32)
10-13 12:20:30.619: E/AndroidRuntime(775):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-13 12:20:30.619: E/AndroidRuntime(775):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-13 12:20:30.619: E/AndroidRuntime(775):  ... 11 more

Edit

This is the entire class:

    package com.example.lino;

    import java.lang.reflect.Array;
    import java.util.ArrayList;
    import java.util.List;

    import android.os.Bundle;
    import android.app.Activity;
    import android.util.TypedValue;
    import android.view.ViewGroup.LayoutParams;
    import android.widget.LinearLayout;
    import android.widget.TextView;


    public class LinesActivity extends Activity {

        TextView tvTitle;
        String bussinessId, bussinessTitle;
        String[] passedString;
        List<Line> linesList;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.lines_layout);

            tvTitle = (TextView) findViewById(R.id.tvMainTitle);

            Bundle passedBundle = getIntent().getExtras();
            passedString = passedBundle.getString("BussinessName").split(":");

            bussinessId = passedString[0];
            bussinessTitle = passedString[1]; // THIS IS LINE 32, BUT IT'S WORKING WELL

            tvTitle.setText(bussinessTitle);

            try {
                String temp = "abcd&efgh&ijkl&mnop";
                String[] tempArr = temp.split("ijk");

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

            }
        }
    }
4
  • 1
    That split will give you an array of length 2. How are you using it? And where? Commented Oct 13, 2012 at 12:32
  • 2
    Is your split() call actually line 32 in LinesActivity? Commented Oct 13, 2012 at 12:32
  • 1
    String temp = "abcd&efgh&ijkl&mnop"; String[] tempArr = temp.split("ijk"); for(int i=0; i< tempArr.length;i++) { System.out.println(tempArr[i]); }----This works fine. so you should check your other codes. And it will be good to see the code form where you are calling this function. Commented Oct 13, 2012 at 12:36
  • I've added the entire class to the post Commented Oct 13, 2012 at 12:41

3 Answers 3

4

Running :

String temp = "abcd&efgh&ijkl&mnop";
String[] tempArr = temp.split("ijk");
System.out.println(Arrays.toString(tempArr));

I get

[abcd&efgh&, l&mnop]

with no Exception.

In my editor line 32 is actually shown as:

bussinessId = passedString[0];

which would indicate that the first String.split:

passedString = passedBundle.getString("BussinessName").split(":");

was not what you had intended.

Check the content of passedBundle.getString("BussinessName") to see if it contains a : character.

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

1 Comment

You have nailed it. The OP is misinterpreting the evidence.
3
passedString = passedBundle.getString("BussinessName").split(":");

I guess your passedBundle.getString("BussinessName").split(":");

is returning only one element and you are trying to accessing the second element from the passedString by using bussinessTitle = passedString[1];

before fetching the elements from the passedString array print the length of the passedString in your log or

System.out.println(passedString.length);

and before accessing the element from the passedString array it's better to put a condition

if(passedString.length>0)
{
bussinessId = passedString[0];
bussinessTitle = passedString[1]; 

}

Comments

1

The code you have given cannot throw an Exception and crash.

I guess you forgotten to copy the code Line 32 of LinesActivity as stated in the logcat.

I am pretty sure you are reading a non existent line of the Array: tempArr

Comments

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.