-1

I want to take integers from a file and put put them into an array but it keeps giving me a null pointer error. The text file is 100 lines with a single integer on each. When I debug it the file is being read and the proper integer is being stored but when I try to send it to my popBArray method it doesn't work.

public void readBoardFile(String filename) {
    try {
        Scanner filescanner = new Scanner(new File(filename));
        while (filescanner.hasNextLine()) {
            String line = filescanner.nextLine();
            int num = Integer.parseInt(line);
            popBarray(num);
        }
        filescanner.close();
    } catch (IOException e) {
        System.out.println(e);
    } catch (Exception e) {
        System.out.println(e);
    }
}
int c = 0;
public void popBarray(int sent) {
    BoardA[c] = sent;
    c++;
}
AMC
  • 2,466
  • 7
  • 11
  • 31
Bill
  • 11
  • 3

1 Answers1

0

You may not initialize BoardA.

The code may help you.

    public void readBoardFile(String filename) {
        try {
            Scanner filescanner = new Scanner(new File(filename));
            while (filescanner.hasNextLine()) {
                String line = filescanner.nextLine();
                int num = Integer.parseInt(line);
                popBarray(num);
            }
            filescanner.close();
        }catch (IOException e){
            System.out.println(e);
        }catch (Exception e){
            System.out.println(e);
        }
    }

    int c = 0;

    public int[] BoardA = new int[100];  // initialize BoardA.

    public void popBarray(int sent) {
        BoardA[c] = sent;
        c++;
    }

YouXiang-Wang
  • 1,091
  • 6
  • 15