我正在尝试编写一个接受房屋数组和价格 priceLimit 的方法。我希望该方法为价格小于或等于 priceLimit 的房屋返回一组房屋。我想传递数据以确定返回数组的大小。问题是我不知道如何将 House 对象从 house 数组添加到 lowPriceHouses 数组。我得到的错误是:
Exception in thread "main" java.lang.NullPointerException at
prob3.HouseTester.getLowPriced(HouseTester.java:76) at
prob3.HouseTester.main(HouseTester.java:52)
编码
private static House[] getLowPriced(House[] houses, double priceLimit)
{
House[] lowPriceHouses = null;
int index = 0;
for (int i = 0; i <= houses.length; i++)
{
double prices = houses[i].getPrice();
if (prices <= priceLimit)
lowPriceHouses[index++] = houses[i];
}
return lowPriceHouses;
}