0

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.

2
  • 2
    Please provide the code of the split method Commented Oct 1, 2010 at 11:05
  • @Carlos I read that as data.split(","); good observation. Commented Oct 1, 2010 at 13:01

6 Answers 6

10

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.

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

2 Comments

+1 to What @carlos said. Almost all of us here assumed that split works as expected.
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.
2

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 ",").

Comments

2

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.

4 Comments

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: 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.
please note that the code is NOT (directly) calling String.split()!
@Carlos: Thanks dude. That was a good catch. I've updated my answer accordingly.
1

You need some defensive code,

if(netInput.length > 1)
   pos_arr = PApplet.parseInt(netInput[0]);
   rohr_value = PApplet.parseInt(netInput[1]); 

1 Comment

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.
1

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

returns one element array

3 Comments

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?
then I can assume that FERNROHR_ID - is an empty string.
Or target is an empty string
1

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?

1 Comment

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.

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.