Comparing data numbers, Iterator.remove() throwing NullPointerException

I am trying to make an Android app that can compare integers. However, when I am trying to compare integers and I need to delete some entries because they are redundant or they do not fit in, I cannot use Iterator.remove() without encountering a NullPointerException. Here is the specific output:

E/AndroidRuntime: FATAL EXCEPTION: main
          Process: com.daita.musicale, PID: 22649
          java.lang.UnsupportedOperationException
              at java.util.AbstractList.remove(AbstractList.java:638)
              at java.util.AbstractList$SimpleListIterator.remove(AbstractList.java:75)
              at com.daita.musicale.MainActivity.checkNotes(MainActivity.java:115)
              at com.daita.musicale.MainActivity$3.onClick(MainActivity.java:74)
              at android.view.View.performClick(View.java:5198)
              at android.view.View$PerformClick.run(View.java:21147)
              at android.os.Handler.handleCallback(Handler.java:739)
              at android.os.Handler.dispatchMessage(Handler.java:95)
              at android.os.Looper.loop(Looper.java:148)
              at android.app.ActivityThread.main(ActivityThread.java:5417)
              at java.lang.reflect.Method.invoke(Native Method)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Here is the code region(at com.daita.musicale.MainActivity.checkNotes(MainActivity.java:115)) that is giving me the error.

public boolean[] checkNumbers(String code1, String code2) {
    if (code != null) {
        List<String> c1 = Arrays.asList(code1.replace("-1.0,", "").replace("null", "").split(","));
        List<String> c2 = Arrays.asList(code2.replace("-1.0,", "").replace("null", "").split(","));
        Iterator<String> c1iterator = c1.iterator();
        Iterator<String> c2iterator = c2.iterator();
        boolean strike = false;
        double prev = 0;
        int i = 0;
        boolean[] correctness = new boolean[c1.size()];
        while (c1iterator.hasNext()) {
            double c1hertz = Double.parseDouble(c1iterator.next());
            double c2hertz = Double.parseDouble(c2iterator.next());
            double difference = c1hertz - c2hertz;
            if (difference > -1 && difference < 1) {
                correctness[i] = true;
                double prevDifference = prev - c1hertz;
                if (prevDifference > -1 && prevDifference < 1) {
                    c1iterator.remove(); //specific issue location! (this is line 115 of MainActivity)
                    c2iterator.remove();
                    i = 0;
                    correctness = new boolean[c1.size()];
                } else {
                    prev = c1hertz;
                    correctness[i] = true;
                }
            } else {
                if (strike) {
                    correctness[i] = false;
                    try {
                        correctness[i - 1] = false;
                    } catch (ArrayIndexOutOfBoundsException aioobe) {
                        aioobe.printStackTrace();
                    }
                } else {
                    correctness[i] = true;
                }
            }
            i++;
        }
        return correctness;
    } else {
        return null;
    }
}

I have looked at other questions on both Code Review and Stack Overflow, for example:

Calling remove in foreach loop in Java
Removing object from ArrayList in for each loop
Removing elements on a List while iterating through it
I would greatly appreciate any help. Thank you!

2
Leave a Reply

avatar
2 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

The Arrays.asList version is immutable, that’s the exact reason why it’s throwing an UnsupportedOperationException.

as stated by java docs:

Arrays.asList returns a fixed-size list backed by the specified array.

that simply means you cannot modify the list.

Solution:

List<String> c1 = new ArrayList<>(Arrays.asList(code1.replace("-1.0,", "")
.replace("null", "").split(",")));

List<String> c2 = new ArrayList<>(Arrays.asList(code2.replace("-1.0,", "")
.replace("null", "").split(",")));

Jason
Guest

Use an ArrayList because it supports removal with iterating

List<String> c1 = new ArrayList<>(Arrays.asList(code1.replace("-1.0,", "").replace("null", "").split(",")));
List<String> c2 = new ArrayList<>(Arrays.asList(code2.replace("-1.0,", "").replace("null", "").split(",")));