0
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?

1
  • Why is arr a static field and not just part of the scope of the read method? Commented Jul 7, 2012 at 20:00

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

5 Comments

Now in the top of the code I initialized it but there is still exception error?
Please post the updated code and the new exception stacktrace
How should I initialize the instance d for reading the data from the txt file?
Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Scanner.java:1516) at Test.read(Test.java:41) at Test.main(Test.java:18) it gives this and the line's are String cities4 = scan.nextLine(); //System.out.println(cities4); and ArrayList<Countries> d = read("data.txt");
From what I see you are using scan.nextLine() 7 times in your read(String filename). Does your file contains multiplication of 7 lines (7,14,21,...)?
0

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.

1 Comment

I made it now but there is still something like that. Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Scanner.java:1516) at Test.read(Test.java:41) at Test.main(Test.java:18) first error is for this line String cities4 = scan.nextLine(); //System.out.println(cities4); the other one is ArrayList<Countries> d = read("data.txt");
0

arr variable is not initialized, replace

private static ArrayList<Countries> arr

with

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

Comments

0

You never initialized arr.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.