我在尝试将项添加到arrayList时遇到了nullPointerException。以下是执行此操作的功能:
public void addToUtilityCardItems() {
int[] utilitiesLogos = {R.drawable.kenya_power, R.drawable.nairobi_water, R.drawable.startimes, R.drawable.zuku};
String[] utilitiesNames = {"Kenya Power", "Nairobi Water", "Startimes", "Zuku"};
UtilitiesModel utilities = new UtilitiesModel();
for (int i = 0; i < utilitiesNames.length; i++) {
utilities.setUtilityLogo(utilitiesLogos[i]);
utilities.setUtilityName(utilitiesNames[i]);
utilityCardItems.add(utilities);
}
}
我得到的错误是:
java.lang.NullPointerException
at com.sce.kbs.lient.FragmentTwo.addToUtilityCardItems(FragmentTwo.java:82)
at com.sce.kbs.lient.FragmentTwo.onActivityCreated(FragmentTwo.java:56)
请帮助
答案 0 :(得分:1)
如果要添加多个实用程序对象:
public void addToUtilityCardItems() {
int[] utilitiesLogos = {R.drawable.kenya_power, R.drawable.nairobi_water, R.drawable.startimes, R.drawable.zuku};
String[] utilitiesNames = {"Kenya Power", "Nairobi Water", "Startimes", "Zuku"};
for (int i = 0; i < utilitiesNames.length; i++) {
UtilitiesModel utilities = new UtilitiesModel();
utilities.setUtilityLogo(utilitiesLogos[i]);
utilities.setUtilityName(utilitiesNames[i]);
utilityCardItems.add(utilities);
}
}