Reputation: 35
I have splited string. There may be 1 part or may be more parts. I would like to get max 100 first parts, but few of them may not exist. And when part have no value i'm getting an error trying to debug app. This is what i've tryed to do, but it doesn't work:
String[] a = null;
a = out.split("\\{");
String c1 = "0";String c2 = "0";String c3 = "0";String c4 = "0";String c5 = "0";String c6 = "0";String c7 = "0";String c8 = "0";String c9 = "0";String c10 = "0";
String c11 = "0";String c12 = "0";String c13 = "0";String c14 = "0";String c15 = "0";String c16 = "0";String c17 = "0";String c18 = "0";String c19 = "0";String c20 = "0";
String c21 = "0";String c22 = "0";String c23 = "0";String c24 = "0";String c25 = "0";String c26 = "0";String c27 = "0";String c28 = "0";String c29 = "0";String c30 = "0";
String c31 = "0";String c32 = "0";String c33 = "0";String c34 = "0";String c35 = "0";String c36 = "0";String c37 = "0";String c38 = "0";String c39 = "0";String c40 = "0";
String c41 = "0";String c42 = "0";String c43 = "0";String c44 = "0";String c45 = "0";String c46 = "0";String c47 = "0";String c48 = "0";String c49 = "0";String c50 = "0";
String c51 = "0";String c52 = "0";String c53 = "0";String c54 = "0";String c55 = "0";String c56 = "0";String c57 = "0";String c58 = "0";String c59 = "0";String c60 = "0";
String c61 = "0";String c62 = "0";String c63 = "0";String c64 = "0";String c65 = "0";String c66 = "0";String c67 = "0";String c68 = "0";String c69 = "0";String c70 = "0";
String c71 = "0";String c72 = "0";String c73 = "0";String c74 = "0";String c75 = "0";String c76 = "0";String c77 = "0";String c78 = "0";String c79 = "0";String c80 = "0";
String c81 = "0";String c82 = "0";String c83 = "0";String c84 = "0";String c85 = "0";String c86 = "0";String c87 = "0";String c88 = "0";String c89 = "0";String c90 = "0";
String c91 = "0";String c92 = "0";String c93 = "0";String c94 = "0";String c95 = "0";String c96 = "0";String c97 = "0";String c98 = "0";String c99 = "0";String c100 = "0";
if(!a[2].isEmpty()){
String[] b1 = a[2].split(",");c1 = b1[1].split(":")[1];
}
if(!a[3].isEmpty()){
String[] b2 = a[3].split(",");c2 = b2[1].split(":")[1];
}
if(!a[4].isEmpty()){
String[] b3 = a[4].split(",");c3 = b3[1].split(":")[1];
}
if(!a[5].isEmpty()){
String[] b4 = a[5].split(",");c4 = b4[1].split(":")[1];
}
if(!a[6].isEmpty()){
String[] b5 = a[6].split(",");c5 = b5[1].split(":")[1];
}
if(!a[7].isEmpty()){
String[] b6 = a[7].split(",");c6 = b6[1].split(":")[1];
}
So far only a[2] and a[3] have some values and a[4] should be empty. But it looks like it doesn't... this is an error:
java.lang.ArrayIndexOutOfBoundsException: 4
at someapp.stan(sapp.java:86)
at someapp.<init>(sapp.java:417)
at someapp.run(sapp.java:39)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$300(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
And this is line 39
frame.getContentPane().add(new TestPane());
And line 417(stan(id) is public string where is my splited string)
stant = stan(id);
And line 86
if(!a[4].isEmpty()){
Upvotes: 1
Views: 170
Reputation: 32145
Here the line if(!a[4].isEmpty()){
throws the ArrayIndexOutOfBoundsException: 4
Exception because the index 4
is higher than the length
of a
and it doesn't have this index.
So you should always check wether the index you are using is lower than the array length or not.Try to print the length
of the array a
and see the number of Strings you got.
You can use a for loop to loop through the array Strings or check for it in the given if condition, something like this:
if(a.length>4 && !a[4].isEmpty()){
Upvotes: 1
Reputation: 2696
You should check if
a.length < 4
Now you already call an item which does not exist, do instead
for(int i=0;i<a.length;i++) // will never get an IndexOutOfBounds
a[i];// <-- do something....
Upvotes: 1