-1

I have a NullPointerException Within the Tournament Class and the Test Class. I call the method of the Tournament to define in the Test class, then i assign it in the Tournament class to create Team objects. it gives me a NullPointerException on Tournament tour = new Tournament(); and File file = new(fileLocation);.

i think its because i declared the wrong file path, but the file is in the project folder.To make it easier i set the directory of the file in the Test class to exactly know where the problem is and also change file if i needed. Please comment if you need any more blocks of the classes.

Test Class:

 Tournament tour = new Tournament();
 tour.setFile("Tourney2Teams.csv"); 

Tournament :

Scanner input;
    public String fileLocation;

    public void setFile(String x)
    {
        this.fileLocation=x;
    }
    File file = new File(fileLocation);

and this is the method to read the file:

public void readingFile()
    {
        try{
            input = new Scanner(file).useDelimiter(",");
        }
        catch (FileNotFoundException e)
        {
            System.out.print("File Not Found\n");
        }
    }
ann
  • 85
  • 1
  • 5
  • Hard to see what's going on with that little amount of code, and no stack trace. Also see http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it/218510#218510 – copeg May 03 '15 at 00:32
  • Do you have debugger? – MaxZoom May 03 '15 at 00:33
  • Please add full code, can't understand what you are doing. Like in your tournament class you are initializing file outside any method or constructor, why? And in your Test class you are creating Tournament object and and executing set method outside of any method. – Paramvir Singh Karwal May 03 '15 at 00:47

3 Answers3

3

You should initialise the file variable inside of the setFile method.

public void setFile(String x)
{
    this.fileLocation = x;
    file = new File(fileLocation);
}
File file;

Currently, it's trying to initialise the file using fileLocation before you've set the location, hence the NullPointerException.

To further explain, when you construct the Tournament, it initialises all variables, which means it calls new File(fileLocation) as you've asked it to. It doesn't matter that you've placed the method first in your code; fact is when it creates a new instance, fileLocation = null and you won't get a chance to set it before it tries to initialise it.

Makoto
  • 104,088
  • 27
  • 192
  • 230
Kane O'Riley
  • 2,482
  • 2
  • 18
  • 27
1

The problem is that you are initialising the file in the Tournament class at class construction time, but fileLocation is initialised after that in the setFile method. To solve this move the construction of File into the setFile method:

class Tournament {

    private File file;
    public void setFile(String fileLocation)
    {
        this.file = new File(fileLocation);
    }
    // ...
}

or create the file in a constructor instead:

class Tournament {

    private File file;
    public Tournament(String fileLocation)
    {
        this.file = new File(fileLocation);
    }
    // ...
}

In both these examples the fileLocation member variable didn't seem to be needed so it has been removed.

krock
  • 28,904
  • 13
  • 79
  • 85
0

Wrong path will never throw a NullPointerException, so problem is not in here.

EDIT: File can throw a NullPointerException when the specified path is null. And you're calling it before you initialize the fileLocation, try

public Tournament(String x){
     fileLocation=x;
     file = new File(fileLocation);
}
Andrii Liakh
  • 619
  • 3
  • 16