sergerde
sergerde

Reputation: 173

Why I get the null pointer exception with the array?

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class Test {
private static ArrayList<Countries> arr =null;
static String country;
static String capital;
static String cities;
static String answer;

public static void main(String args[]) throws IOException{
    Scanner keybord = new Scanner(System.in);
    ArrayList<Countries> d = read("data.txt");
    String res = "";
    for(int i = 0; i < d.size(); i++){
             res += d.get(i).toString();
        answer = keybord.nextLine();
        if(answer.equalsIgnoreCase(d.get(i).getCapital()))
            res+= "The answer is true!";
        else
            res += "The answer is not true" + "The answer is " + d.get(i).getCapital();
        System.out.println(res);
    }
}

public static ArrayList<Countries> read(String filename) throws IOException{
    Scanner scan = new Scanner(new File(filename));
    while(scan.hasNext()){
        country = scan.nextLine(); //System.out.println(country);
        String cities1 = scan.nextLine(); //System.out.println(cities1);
        String cities2 = scan.nextLine(); //System.out.println(cities2);
        String cities3 = scan.nextLine(); //System.out.println(cities3);
        String cities4 = scan.nextLine(); //System.out.println(cities4);
        String capital = scan.nextLine(); //System.out.println(capital);
        Countries c = new Countries(cities1, cities2, cities3, cities4, capital); 
        arr.add(c); // where i get the exception
        scan.nextLine();
    }
    return arr;

}

I could not understand why I'm getting the null pointer exception while I'm trying to add the countries to the array list? should not I instantiate it as a null before creating it?

Upvotes: 0

Views: 878

Answers (4)

Jigar Joshi
Jigar Joshi

Reputation: 240898

You haven't initialized arr so by default it is null

Initialize it by

private static ArrayList<Countries> arr = new ArrayList<Countries>();

Also,

You check if next element exist by

 while(scan.hasNext()){

and then you are reading next 7 line

country = scan.nextLine(); //System.out.println(country);
String cities1 = scan.nextLine(); //System.out.println(cities1);
String cities2 = scan.nextLine(); //System.out.println(cities2);
String cities3 = scan.nextLine(); //System.out.println(cities3);
String cities4 = scan.nextLine(); //System.out.println(cities4);
String capital = scan.nextLine(); //System.out.println(capital);

scan.nextLine();

Which can fail

Upvotes: 1

Yossi
Yossi

Reputation: 12100

You never initialized arr.

Add arr = new ArrayList<Countries>(); to your main.

Upvotes: 0

dbf
dbf

Reputation: 6499

arr variable is not initialized, replace

private static ArrayList<Countries> arr

with

private static ArrayList<Countries> arr = new ArrayList<Countries>();

Upvotes: 0

Oleksi
Oleksi

Reputation: 13097

You need to initialize arr:

private static ArrayList<Countries> arr = new ArrayList<Countries>();

If you omit the new ArrayList<Countries>(); part, the default value will be null, and then when you call arr.add(), you get that NullPointerException.

Upvotes: 0

Related Questions