[英]Trouble with array of integers in java
This program should allow user to enter values of fall-outs for each month and then output average value of fall-outs and the driest season. 该程序应允许用户输入每个月的尘埃落差值,然后输出尘埃落差和最干旱季节的平均值。 It is compiling, but the problem is i can't enter data into array.
它正在编译,但是问题是我无法将数据输入数组。 It lets me to enter the value of the first array element and then tells me there's an error:
它让我输入第一个数组元素的值,然后告诉我有一个错误:
Enter precipitation for each month:
Month #01 = 10
Exception in thread "main" java.lang.NullPointerException
at Seasons.In(Seasons.java:16)
at Seasons.main(Seasons.java:41)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Process finished with exit code 1
Here is the code: 这是代码:
import java.util.Scanner;
import java.util.ArrayList;
public class Seasons {
private ArrayList<Integer> monthList;
private int winter,spring,summer,autumn;
public void In(){
Scanner in = new Scanner(System.in);
System.out.println("Enter precipitation for each month: ");
for(int i=0;i<12;i++){
System.out.print("Month #"+i+1+" = ");
int a=in.nextInt();
monthList.add(i, a);
}
}
public int Calculate(){
int Sum = 0;
for(int element:monthList){
Sum+=element;
}
winter = monthList.get(11) + monthList.get(0) + monthList.get(1);
spring = monthList.get(2) + monthList.get(3) + monthList.get(4);
summer = monthList.get(5) + monthList.get(6)+ monthList.get(7);
autumn = monthList.get(8) + monthList.get(9) + monthList.get(10);
return Sum;
}
public void Out(int Result){
System.out.println("Average precipitation for this year is "+ Result/12);
if(winter<spring && winter<summer && winter<autumn) System.out.println("Winter was the driest season - "+winter);
else if(spring<summer && spring<autumn) System.out.println("Spring was the driest season - "+spring);
else if(autumn<summer) System.out.println("Autumn was the driest season - "+autumn);
else System.out.println("Summer was the driest season - "+summer);
}
public static void main(String[] args){
Seasons Precipitation = new Seasons();
Precipitation.In();
Precipitation.Out(Precipitation.Calculate());
}
}
Will be thankful for any help. 感谢您的帮助。
UPD.: Thank you for noticing my unattentive mistake, now it works! UPD .:谢谢您注意到我的细心错误,现在可以使用了!
With 同
private ArrayList<Integer> monthList;
Currently your list is null(default value of object) and when you do monthList.add();
当前,您的列表为null(对象的默认值),当您执行
monthList.add();
means null.add()
giving you Null Pointer
.Change it to 意味着
null.add()
给您Null Pointer
其更改为
private ArrayList<Integer> monthList=new ArrayList<Integer>();
初始化ArrayList:
ArrayList<Integer> monthList =new ArrayList<Integer>();
Your array list is not initialized 您的数组列表未初始化
private ArrayList<Integer> monthList;
should be changed to 应该更改为
private ArrayList<Integer> monthList = new ArrayList<>();
HERE YOU GO 干得好
You didn't initialize the ArrayList. 您没有初始化ArrayList。 That's all
就这样
public static void main(String[] args) {
Seasons Precipitation = new Seasons();
Precipitation.monthList = new ArrayList<Integer>();//INITIALISE
Precipitation.In();
Precipitation.Out(Precipitation.Calculate());
}
If it works, kindly mark it as accepted. 如果可行,请将其标记为接受。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.