Why am I getting this NulPointerException:
Please Give the length of the Array: 5 Please give the entries for the array for 0th index 2 Exception in thread "main" java.lang.NullPointerException at Array.main(Array.java:18)
In this code?
public class Array{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Please Give the length of the Array: ");
int num= scan.nextInt();
int arr[] = null;
int i;
for (i=0; i<num;i++)
{
if(i<num-1)
{
System.out.println("Please give the entries for the array for " +i+"th index");
arr[i]=scan.nextInt();
}
else
{
System.out.println("Please give the entries for the array for last index");
arr[i]=scan.nextInt();
}
}
scan.close();
}
}