开发者

strange problem in an array

开发者 https://www.devze.com 2023-01-18 13:57 出处:网络
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:

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";
    netInp开发者_运维知识库ut[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.


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.


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


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.


You need some defensive code,

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


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

returns one element array


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消