无论我怎么做,我都会一直得到nullpointerexception。如果需要,可以提供更多代码。
public int howManyBetweenTheseYears(int startYear, int endYear){
ArrayList<Lamborghini> selectedCars = new ArrayList<Lamborghini>();
for(Lamborghini c : inventory){
if(c == null){
continue;
}
if(c.getModelYear()>= startYear && c.getModelYear()<= endYear){
selectedCars.add(c);
}
}
return selectedCars.size();
}
答案 0 :(得分:0)
看起来public class MainActivity extends Activity
implements SharedPreferences.OnSharedPreferenceChangeListener {
SharedPreferences settings;
ProgressDialog dialog;
Map<String, String> params;
String INSTALL_PREFERENCE = "installpref";
String REFERRAL_KEY = "referral";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dialog = new ProgressDialog(this);
params = new HashMap<String, String>();
settings = this.getSharedPreferences(INSTALL_PREFERENCE, 0);
settings.registerOnSharedPreferenceChangeListener(this); //added
}
@Override
public void onResume() {
super.onResume();
String referrerString = settings.getString(REFERRAL_KEY, null);
if (referrerString != null){
params.put("referrer", referrerString);
//call whatever methods need params to have a value
//.......
}
else{
//Show progress dialog and wait for BroadcastReceiver to populate referrer
dialog.setMessage("loading");
dialog.show();
}
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPrefs, String key) {
if (key.equals(REFERRAL_KEY)){
//dismiss dialog if it's showing
if (dialog.isShowing()){
dialog.dismiss();
}
String referrerString = sharedPrefs.getString(REFERRAL_KEY, null);
if (referrerString != null){
params.put("referrer", referrerString);
//call whatever methods need params to have a value
//.......
}
}
}
}
为空。
答案 1 :(得分:0)
如果getModelYear
返回null,那么您将获得NullPointerException
答案 2 :(得分:0)
2职位: -
大多数情况下,您的广告资源对象引用为空
你的if代码 -
if(c.getModelYear()&gt; = startYear&amp;&amp; c.getModelYear()&lt; = endYear)
如果相反,请尝试以下 -
if(((c.getModelYear()!=null)?c.getModelYear():0)>=startYear&&
c.getModelYear()<= endYear)
我确信你在'startYear'和'endYear'之间有一些适当的价值,除了零。
答案 3 :(得分:0)
如果你可以使用java 8,下面的代码应该可以工作:
if(inventory==null){
return 0;
}
return inventory.stream().filter((Lamborghini c)-> c!=null && c.getModelYear()>= startYear && c.getModelYear()<= endYear).count();
但是如果没有你告诉我们确实发生了无效指针,它只是在黑暗中拍摄。
答案 4 :(得分:0)
你没有两个ifs。因为“for”循环只会继续每个c值。
public int howManyBetweenTheseYears(int startYear, int endYear)
{
ArrayList<Lamborghini> selectedCars = new ArrayList<Lamborghini>();
for(Lamborghini c : inventory)
{
if(c.getModelYear()>= startYear && c.getModelYear()<= endYear)
{
selectedCars.add(c);
}
}
return selectedCars.size();
}
还有一个保存返回值的变量,并通过给出大小为0来启动它。否则,如果找不到任何匹配的汽车,它会给你nullpointerexception。
答案 5 :(得分:0)
最初我在初始化库存arraylist时有(ArrayList Lamborghini):
public class LamborghiniCarLot
{
private String lotName;
private ArrayList<Lamborghini> inventory;
public LamborghiniCarLot()
{
ArrayList<Lamborghini> inventory = new ArrayList<Lamborghini>();
}
public LamborghiniCarLot(String lotName){
ArrayList<Lamborghini>inventory = new ArrayList<Lamborghini>();
setLotName(lotName);
然后我尝试在库存之前删除(ArrayList Lamborghini)并修复了所有内容:
public LamborghiniCarLot()
{
inventory = new ArrayList<Lamborghini>();
}
public LamborghiniCarLot(String lotName){
inventory = new ArrayList<Lamborghini>();
setLotName(lotName);
如果有人能够向我解释这些变化是什么以及为什么在库存之前它不适用于arraylist lamborghini,我们将不胜感激。我是编程新手,所以请详细说明。