1

I'm trying to use a text file to populate a 2D array for a game board. I can't seem to get the reader to actually read the file though. I keep getting the following error:

"Unable to open file 'C://git/cse116f16/src/boardpieces/board.txt'Exception in thread "main" java.lang.NullPointerException".

I know it's because I'm not using the correct syntax for the filepath, but I can't for the life of me figure out what that should be. I have it stored at C:\Users\myname\git\cse116f16\src\boardpieces\board.txt.

What is the string I would need to pass into FileReader to read this file?


EDIT: Here's the method, maybe the problem isn't what I think it is.

Also it should be noted this is on a git repository so I need to be sure it can be read by everyone who pulls it.

    public Character[][] populateArray(String fileName){
        String line = null;
        Character[][] retVal = new Character[26][28];
        int lineNum = 0;
        try {
            FileReader fileReader = new FileReader(fileName);
            BufferedReader bufferedReader = new BufferedReader(fileReader);

            //Assigns each character to it's corresponding place in the array
            while((line = bufferedReader.readLine()) != null) {
                for (int i = 0; i < 26; i++){
                    retVal[lineNum][i] = line.charAt(i);
                }                   
            }   
            bufferedReader.close();         
        }
        catch(FileNotFoundException ex) {
            System.out.println("Unable to open file '" + fileName + "'");                
        }
        catch(IOException ex) {
            System.out.println("Error reading file '" + fileName + "'");                  
        }
        return retVal;
    }

    public void populateBoard(){
        String path = "C:\\Users\\myname\\git\\cse116f16\\src\\boardpieces\\board.txt";

        Character[][] arr = populateArray(path);

        //the rest of the method...
1
  • 1
    I am not clear here, whats the need of \\ instead of \, I am able to do it by using only one slash only, as well can you share the content of your file to provide better answer Commented Oct 19, 2016 at 15:17

3 Answers 3

1
File myFile = new File("C:\\Users\\myname\\git\\cse116f16\\src\\boardpieces\\board.txt");
BufferedReader reader = new BufferedReader(new FileReader(myFile));
String text;
while (reader.hasNextLine()){
    text += reader.nextLine();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Try to display it as code by adding four spaces in front of your code.
You could do it be writing \\\\ instead of \\
0

Please try

final File file =
        new File("C:\\Users\\myname\\git\\cse116f16\\src\\boardpieces\\board.txt");
final FileReader fileReader = new FileReader(file);

Comments

0

Try this:

String fileName = "C:\\Users\\myname\\git\\cse116f16\\src\\boardpieces\\board.txt";
//or "C:/Users/myname/git/cse116f16/src/boardpieces/board.txt"
File file = new File(fileName);
FileReader fileReader = new FileReader(file);

The slashs must either be like \\ or like /.

(Both \ and / work but since \ is an escape character java reads \ this as an escape character, so by adding another one you essentially only write only slash.)

Then you try to open the File and then pass it to the FileReader

3 Comments

@Bryan The problem is that you are doing new FileReader(fileName), you should be doing new FileReader(new File(fileName))
@Bryan Did you remember to change myname to your username?
@Bryan I recommend going to the actual file location click on copy path, then using your IDE, notepad++ or any other editor, replace all the \ with /. (That's what I do).

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.