考虑以下代码:
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();
}
}
我在执行 getSounds() 时遇到了 NullPointerException:
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)
我的问题是:在这个实现中,如何在getSounds()中有一个空对象?我已经测试了JDK 1.8中的Array clone()和一些Android设备,它从未返回null。
关于2个Arrays.sort(),我知道它们正在排序错误的引用,我不认为它们与此崩溃相关,但我想分享代码就像它是的那样。
另外,我在许多手机上都遇到了这个崩溃,所以我不认为它与修改后的手机中clone()的奇怪实现有关。
sounds
不为null
吗? - Michael Dodd