Given this code:
public class SoundAndFilterCollection {
private final Sound[] sounds;
private final Filter[] filters;
public SoundAndFilterCollection(Sound[] sounds, Filter[] filters) {
this.sounds = sounds.clone();
this.filters = filters.clone();
Arrays.sort(sounds);
Arrays.sort(filters);
}
public Sound[] getSounds() {
return sounds.clone();
}
public Filter[] getFilters() {
return filters.clone();
}
}
I'm having a NullPointerException when doing getSounds():
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object gab[].clone()' on a null object reference
at com.mycompany.messenger.push2talk.domain.SoundAndFilterCollection.getSounds(SoundAndFilterCollection.java:24)
at com.mycompany.messenger.shareinchat.chatbar.hiddenpanel.views.soundstickers.presenter.SoundStickersPanelPresenter.setSoundsViews(SoundStickersPanelPresenter.java:92)
at com.mycompany.messenger.shareinchat.chatbar.hiddenpanel.views.soundstickers.presenter.SoundStickersPanelPresenter.access$000(SoundStickersPanelPresenter.java:20)
at com.mycompany.messenger.shareinchat.chatbar.hiddenpanel.views.soundstickers.presenter.SoundStickersPanelPresenter.access$200(SoundStickersPanelPresenter.java:20)
at com.mycompany.messenger.shareinchat.chatbar.hiddenpanel.views.soundstickers.presenter.SoundStickersPanelPresenter$3.onDoneWithContext(SoundStickersPanelPresenter.java:59)
at com.mycompany.messenger.shareinchat.chatbar.hiddenpanel.views.soundstickers.presenter.SoundStickersPanelPresenter$3.onDoneWithContext(SoundStickersPanelPresenter.java:56)
at com.mycompany.deferred.Done$UIContextual.onDone(Done.java:35)
My question is: how can I have a null object in getSounds() with this implementation? I've tested Array clone() in JDK 1.8 and in a couple of Android devices and it's never returning null.
About the 2 Arrays.sort() I'm aware that they're sorting the wrong reference and I don't think they're relevant for this crash, but I wanted to share the code just like it is.
PS: Also, I'm having this crash in a huge variety of phones, so I don't think it is related with a strange implementation of clone() in a modified phone.