当我尝试运行该程序时,出现以下错误:
Exception in thread "main" java.lang.NullPointerException at TemperatureMonth.getMaxTempWeekly(TemperatureMonth.java:25) at runner_TemperatureMonth.main(runner_TemperatureMonth.java:41)
我尝试将字段变量周初始化为二维数组温度的长度,但没有成功。
我只是想弄清楚这个错误到底是什么,以及以后如何避免它。
如果修复 NullPointerException 后有任何错误,我可以自己修复。
也许看看最后一个方法,我在删除返回新的二维数组温度后开始出现错误
{
private double[][] temperatures;
private double[] week;
private double[] week_low;
private double[] week_high;
public TemperatureMonth(double[][] t)
{
temperatures = t;
}`
`
`public double[] getMaxTempWeekly()
{
// Remove return and implement getMaxTempWeekly method here
double high;
for (int x = 0; x < temperatures.length; x++) {
high = temperatures[x][0];
for (int y = 0; y < temperatures[0].length; y++) {
if (temperatures[x][y] > high) {
high = temperatures[x][y];
}
}
week[x] = high;
}
return week;
}`
` public double[] getMinTempWeekly()
{
// Remove return and implement getMinTempWeekly method here
double high;
for (int x = 0; x < temperatures.length; x++) {
high = temperatures[x][0];
for (int y = 0; y < temperatures[0].length; y++) {
if (temperatures[x][y] < high) {
high = temperatures[x][y];
}
}
week[x] = high;
}
return week;
}`
public double getRange()
{
// Remove return and implement getRange method here`
double high;
double low;
for (int x = 0; x < temperatures.length; x++) {
high = temperatures[x][0];
low = temperatures[x][0];
for (int y = 0; y < temperatures[0].length; y++) {
if (temperatures[x][y] > high) {
high = temperatures[x][y];
}
else if (temperatures[x][y] < low) {
low = temperatures[x][y];
}
}
week_high[x] = high;
week_low[x] = low;
}
double max = week_high[0];
double min = week_low[0];
for (int x = 0; x < week_high.length; x++) {
if (week_high[x] > max) {
max = week_high[x];
}
if (week_low[x] < min) {
min = week_low[x];
}
}
return max - min;
}
`public double getCertainTemp(int week, int day)
{
// Remove return and implement getCertainTemp method here`
double number = temperatures[week][day];
return number;
}
}`