tags:

views:

125

answers:

6

I'm working on a little server app with Java. So, I'm getting informations from different client, and if information comes in, the following method is called:

public void writeToArray(String data) {
    data = trim(data);
    String[] netInput = new String[5];
    netInput[0]="a";
    netInput[1]="a";
    netInput[2]="a";
    netInput[3]="a";
    netInput[4]="a";
    netInput = split(data, ",");
    pos_arr = PApplet.parseInt(netInput[0]);
    rohr_value = PApplet.parseInt(netInput[1]); // THIS LINE KICKS OUT THE ERROR.
    if(pos_arr >0 && pos_arr<100) {
        fernrohre[pos_arr] = rohr_value;
        println("pos arr length: " + fernrohre[pos_arr]);
        println("pos arr: " + pos_arr);
    }

The console on OS X gives me the following error:

Exception in thread "Animation Thread"
java.lang.ArrayIndexOutOfBoundsException:1
 at server_app.writeToArray(server_app.java:108) at server_app.draw(server_app.java:97)
 at processing.core.PApplet.handleDraw(PApplet.java:1606)
 at processing.core.PApplet.run(PApplet.java:1503)
 at java.lang.Thread.run(Thread.java:637)

As you can see, I tried to fill the array netInput with at least 5 entries, so there can't be an ArrayIndexOutOfBoundsException.

I don't understand that, and I'm thankful for your help!

It would work already for me, if I can catch the error and keep the app continuing.

+10  A: 

You put 5 Strings into the array, but then undo all your good work with this line;

netInput = split(data, ",");

data obviously doesn't have any commas in it.

Qwerky
or the `split` method is not working as assumed...
Carlos Heuberger
+1 to What @carlos said. Almost all of us here assumed that `split` works as expected.
Manoj Govindan
I think the fundamental misunderstanding here is the array re-initialization even if the problem itself exists in the split-method or not. The way that the array is incorrectly "pre-initialized" with five strings really to me points out that it's not expected for the split to return enough values.
Mikko Wilkman
+1  A: 

You make netInput = split(data, ","); and
split(data, ",");

returns one element array
Andriy Sholokh
What I do, when I send is: c.write(FERNROHR_ID + "," +target +"\n");So data has a "," in it. I want the first part, BEFORE the comma is netInput[0], and the second part, AFTER the comma is in netInput[1]. Can someone help me with that?
nbuechi
then I can assume that FERNROHR_ID - is an empty string.
Andriy Sholokh
Or target is an empty string
Andriy Sholokh
+2  A: 

Update

The split() method is custom, not String.split. It too needs to be checked to see what is going wrong. Thanks @Carlos for pointing it out.

Original Answer

Consider this line:

netInput = split(data, ",");

This will split the data string using comma as a separator. It will return an array of (number of commas + 1) resulting elements. If your string has no commas, you'll get a single element array.

Apparently your input string doesn't have any commas. This will result in a single element array (first element aka index = 0 will be the string itself). Consequently when you try to index the 2nd element (index = 1) it raises an exception.

Manoj Govindan
Yes, I understand this, but I send: c.write(FERNROHR_ID + "," +target +"\n"); So data has a "," in it. I want the first part, BEFORE the comma is netInput[0], and the second part, AFTER the comma is in netInput[1]. Can someone help me with that?
nbuechi
@nbuechi: you need to double check the data. Try printing it to sysout and see what turns up. You logic for splitting is correct, but apparently the data is not showing up in the way want.
Manoj Govindan
please note that the code is NOT (directly) calling `String.split()`!
Carlos Heuberger
@Carlos: Thanks dude. That was a good catch. I've updated my answer accordingly.
Manoj Govindan
+1  A: 

You are re-assigning your netInput variable when the split() method is called.

The new value might not have an array count of 5.

Can you provide the source for the split() method?

Koekiebox
I assume the split() method is part of the class/super-class where the writeToArray() method is called. Since String is final and cannot be extended.
Koekiebox
+2  A: 

In this line

netInput = split(data, ",");

your array is being reinitialized. Your split method probably returns an array with only 1 element (I can guess that data string doesn't contain any ",").

Roman
+1  A: 

You need some defensive code,

if(netInput.length > 1)
   pos_arr = PApplet.parseInt(netInput[0]);
   rohr_value = PApplet.parseInt(netInput[1]); 
DeliveryNinja
thanks a lot, maybe this is already enough, because it's working for an uncertain time, and maybe one time, he doesn't get the comma, and then it crashes. thanks.
nbuechi