How do I fix this source not found error? Reading text file with buffered reader - Java

Mascros

Im trying to read deck.txt which is in the src directory of the application. The error I get is:

Exception in thread "main" java.lang.NullPointerException at Deck.(Deck.java:15) at TwentyOne.main(TwentyOne.java:7)

Line 7 in TwentyOne is just:

Deck deck = new Deck();

The file has a single integer on each line and is 104 lines long.

Where cards[i].suit.... is the line throwing the exception

try (BufferedReader br = new BufferedReader(new FileReader("src/deck.txt"))) {   
        for (int i = 0; i < 104; i++) {                 
            cards[i].suit = Integer.parseInt(br.readLine());
            cards[i].rank = Integer.parseInt(br.readLine());
        }

    } catch (IOException e) {
        e.printStackTrace();
    }

To try see what was wrong I copied this code out of eclipse into another directory with the file, this time just println(br.readLine()) rather than assigning it to the array and it worked fine.

Have I got the file in the wrong location or is there something else I'm missing? Thanks for your help

starf

You are attempting to read 208 lines from your 104-line file. Each time you call br.readLine() the next line is read.

I'm assuming the format of the file is alternate lines for suit and rank, so try reducing the number of iterations to 52.

Edit: From the comments, the card array was declared but the card objects not initialised. Initialising the objects in the array before use fixed the NullPointerException.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Reading a file in java using buffered reader correctly

Buffered Reader is not reading my whole file java

Reading through a File with a Buffered Reader

Java Buffered reader only reading last line of CSV file

How to save lines from a text file using buffered reader

How do i temporarily close a buffered reader in java gwt server side?

How can i get a number obtained from a buffered reader file?

How to read and write a file in java using buffered reader and bufferedwriter

How do I fix this cp950 "illegal multibyte sequence" UnicodeDecodeError when reading a text file?

How do i fix this CopyPNG File error?

How do I fix json file reading code NodeJS Electron

How do i print a line from an imported text file in java if the inputted int is found in that text file?

How do I fix the "processing-java: command not found" error in the terminal

There is an error java.lang.NullPointerException in jsp. How do I fix the source code?

How do I fix this Java notarization error?

After windows install, Fedora no longer boots up. Getting error: file '/vmlinuz' not found found. How do I fix the boot?

how do I catch a file not found error?

How do I fix the directory not found for option -F error

How do I fix 'jekyll: command not found' error permanently?

How do I fix key not found error in python?

How do I avoid Python Unicode encoding error when reading in a text file?

Java - Reading from a buffered reader (from a socket) is pausing the thread

InputStreamReader, Buffered Reader, Java issue with reading standard input

Java Problem at scanner (Line not found) How do i fix this?

How to handle exceptions when reading files with buffered reader?

I am not sure, why does my java buffered reader code only read last line of CSV file?

How to fix this logical error in reading a file to a hashmap

In PHP, how do I use conditionals while reading text file?

Why am I getting "Data source name not found and no default driver specified" and how do I fix it?