-3

Okay, I understand what an NPE is, and why they are caused (most of the time), but I cannot figure out why this is happening.

I have an ArrayList of Strings, which is definitely not empty, because I have been displaying each element in a ListView previously.

I am using a custom Comparator:

public static class NumericalPodcastSort implements Comparator<String> {

        @Override
        public int compare(String o1, String o2) {
            return extractInt(o1) - extractInt(o2);
        }

        int extractInt(String s) {
            String num = s.replaceAll("\\D", ""); << This is where the NPE is occurring.
            // return 0 if no digits found
            return num.isEmpty() ? 0 : Integer.parseInt(num);
        }
    }

to sort the integer part of each String in descending order (1, 2, 3, etc.), as shown here

This is where I do the formatting and sorting of the ArrayList, before returning it and using it elsewhere.

public ArrayList<String> formatMindhiveTitles(ArrayList<String> arrayList) {

    ArrayList<String> formattedTitles = new ArrayList<>();

    for (int i = 0; i < arrayList.size(); i++) {

        String title = arrayList.get(i);

        formattedTitles.add(StringUtils.substringBetween(title, "mindhive podcast/", ".mp3"));
    }

    if (!formattedTitles.isEmpty()) {
        Collections.sort(formattedTitles, new FragmentUtilities.NumericalPodcastSort());
        return formattedTitles;
    }

    return new ArrayList<>();
}

I have shuffled the Collections.sort() call around to several different places, but I continually receive

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.replaceAll(java.lang.String, java.lang.String)' on a null object reference

I apologise if this is a simple fix, but for the life of me, I cannot work out why this is happening. It's not an issue of me using ArrayList<String> instead of List<String> is it? I shouldn't think so, but I'm not sure.

Community
  • 1
  • 1
Jaidyn Belbin
  • 475
  • 2
  • 7
  • 17
  • 3
    In `extractInt()` before replacing, check the value of `s`. It is probably null – TheLostMind Sep 06 '16 at 05:50
  • I know it's null, that's why I put the comment there. But `s` is assigned in the `compare()` method which should be sorting the Strings in the ArrayList I pass to `Collections.sort(list, myComparator)`. What I don't understand is how is the list I am passing containing null values? – Jaidyn Belbin Sep 06 '16 at 05:56
  • Instead of down voting, tell me what's I'm doing wrong. I've spent hours scouring the web and SO trying to answer this myself. Give me a break. – Jaidyn Belbin Sep 06 '16 at 05:57
  • You should really check what values you actually getting for this: `StringUtils.substringBetween(title, "mindhive podcast/", ".mp3")` – TWL Sep 06 '16 at 05:58
  • 2
    @JaidynBelbin - You are probably getting down votes because this seems like a post that can be fixed if you debug your code (and APis) – TheLostMind Sep 06 '16 at 05:59
  • 1
    @JaidynBelbin scouring the web is nice, but a few minutes of debugging would have quickly pointed you to the source of the problem. – shmosel Sep 06 '16 at 05:59
  • I am definitely sure all the Strings I am using that on are being added to the list properly. I can see the correct output when the Strings are added to my ListView. – Jaidyn Belbin Sep 06 '16 at 06:00
  • @JaidynBelbin no you don't. See the answer below. – Sufian Sep 06 '16 at 06:01
  • What String did you pass for o1 and o2? – progyammer Sep 06 '16 at 06:13

1 Answers1

1

StringUtils.substringBetween() specifies several cases in which it will return null. Presumably that's causing null values to be added to your formattedTitles list, which in turn cause an NPE in your extractInt() method.

shmosel
  • 49,289
  • 6
  • 73
  • 138