0

If I run this code without any changes, I get that error Exception in thread "Thread-1" java.lang.ArrayIndexOutOfBoundsException: -1

If I remove syntax of comment line System.out.println(data);, I don't get any error.

I don't want to print any situation of my app. Variable of data is important for me. How can I solve this problem without using System.out?

Could you help me?

private static ArrayList<String> myList = new ArrayList();
private BufferedReader in1;
private String[] s;
private String data;
...
...
...
...

while (close) {
            try {
                if ((data = in1.readLine()) != null) {
                    //System.out.println(data); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                    if (data.contains("#")) {
                        s = data.split("#");
                        if (s[0].equals("enter")) {
                            if (s[1].equals("ok")) {
                            } else if (s[1].equals("no")) {
                            } else {
                                synchronized (myList) {
                                    myList.set(myList.indexOf(id), s[1]);
                                }
                            }
                        }
                    }
                }
            } catch (Exception e) {
                break;
            }
        }
Nickname
  • 49
  • 9
  • 3
    do you know what an ArrayIndexOutOfBoundsException: -1 is? – Stultuske Jun 04 '18 at 12:53
  • 2
    What is `id`? (In the line `myList.set(myList.indexOf(id), s[1]);`) I'm willing to bet that it's `-1` – GBlodgett Jun 04 '18 at 12:56
  • 1
    who else is using the arrayList? those threads have to lock on the list too. – Nathan Hughes Jun 04 '18 at 12:59
  • In addition to what GBlodgett said, some other thread may have messed up the value of `id`. In that case, your list won't contain that value. The method `myList.indexOf(id)` returns `-1`, causing `myList.set(...)` to throw an exception – Robert Kock Jun 04 '18 at 13:19
  • id its just a simple elements of class. Initially "String id;" and after the constructive method id is gaining new value. It never has a null value. Arraylist is synchronized (locked) in everywhere – Nickname Jun 04 '18 at 13:36
  • I actually replace value of arraylist from old to new. `myList.set(myList.indexOf(id), s[1]);` so id is a member of the arraylist(myList) – Nickname Jun 04 '18 at 13:45
  • 1
    myList.set(myList.indexOf(id), s[1]), this line may cause the exception if given id is not present in the list. – Vijayakumar Jun 04 '18 at 14:17
  • You may try https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CopyOnWriteArrayList.html instead of arraylist – Vijayakumar Jun 04 '18 at 14:19
  • You should show all places in your code where `id` is assigned. There is obviously a scenario when the value of `id` is not in your list. – DodgyCodeException Jun 04 '18 at 14:22
  • Also the code works fine when I delete the top line **synchronized (myList){}**. – Nickname Jun 04 '18 at 15:44
  • You say in your comment "id is a member of the arraylist(myList)". Can you prove it? – DodgyCodeException Jun 05 '18 at 08:28

0 Answers0