-1

In the following program, I am trying to get values in an array from user. However, I am getting NullPointerException after entering only two values. Can someone explain why I am getting this exception?

import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class MemoriseMe{

    static int arr1[],count;

    public static void main(String[] args) throws IOException 
    {
        BufferedReader brObj = new BufferedReader(new InputStreamReader(System.in));

        int firstQuery =Integer.parseInt(brObj.readLine());
        System.out.println("Enter the elements : ");  
        String line = brObj.readLine(); // to read multiple integers line
        String[] strs = line.trim().split("\\s+");

        for(int i =0;i<firstQuery;i++)
        {
            arr1[i]=Integer.parseInt(brObj.readLine());
        }
    }
}
Matthew
  • 1,905
  • 3
  • 19
  • 26

1 Answers1

1

Your arr1 is never initialized, and is null -- you will get a NullPointerException when you try to access it with an index. It seems like you want it to have a length of firstQuery, so:

arr1 = new int[firstQuery];
vlumi
  • 1,321
  • 1
  • 9
  • 18