I want the user to input n numbers of his wish and write a program in java to find the average using arrays. I came up with the following program, but there seems to be a problem when i run it:
Exception in thread "main" java.lang.NullPointerException at wert.main(wert.java:12)
This is the code in question:
import java.util.Scanner;
public class wert {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int gucci[] = null;
System.out.print("Enter the length\n");
int n = sc.nextInt();
System.out.println("enter the numbers : ");
for(int i=0;i<n;i++){
int k = sc.nextInt();
gucci[i] = k;
}
int m = average(gucci);
System.out.println(m/n);
}
public static int average(int x[]){
int total = 0;
for(int f: x){
total =+ f;
}
return total;
}
}
I'm sorry if I am asking a really basic question. I started learning java on my own a few days back.