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
              at java.util.AbstractList.remove(
              at java.util.AbstractList$SimpleListIterator.remove(
              at com.daita.musicale.MainActivity.checkNotes(
              at com.daita.musicale.MainActivity$3.onClick(
              at android.view.View.performClick(
              at android.view.View$
              at android.os.Handler.handleCallback(
              at android.os.Handler.dispatchMessage(
              at android.os.Looper.loop(
              at java.lang.reflect.Method.invoke(Native Method)

Here is the code region(at com.daita.musicale.MainActivity.checkNotes( 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(;
            double c2hertz = Double.parseDouble(;
            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)
                    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) {
                } else {
                    correctness[i] = true;
        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!

Leave a Reply

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

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.


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(",")));


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(",")));