简体   繁体   English

在两个不同的类中尝试捕获NullPointerException错误

[英]try-catch NullPointerException Error within two different classess

I have a NullPointerException Within the Tournament Class and the Test Class. 我在Tournament类和Test类中有一个NullPointerException 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. 我在Test类中调用Tournament的方法来定义,然后在Tournament类中对其进行分配以创建Team对象。 it gives me a NullPointerException on Tournament tour = new Tournament(); 它为我提供了Tournament tour = new Tournament();NullPointerException Tournament tour = new Tournament(); and File file = new(fileLocation); 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. 我认为这是因为我声明了错误的文件路径,但该文件位于项目文件夹中。为了简化起见,我在Test类中设置了该文件的目录,以确切地知道问题出在哪里,并在需要时更改文件。 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");
        }
    }

You should initialise the file variable inside of the setFile method. 您应该在setFile方法内部初始化file变量。

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. 当前,它正在尝试在设置位置之前使用fileLocation初始化文件,因此会出现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. 为了进一步说明,构造锦标赛时,它将初始化所有变量,这意味着它将按您的要求调用new File(fileLocation) 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. 事实是,当它创建一个新实例时, fileLocation = null并且在尝试对其进行初始化之前,您将没有机会对其进行设置。

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. 问题是,你的初始化fileTournament在一流的施工时间类,但fileLocation之后,在初始化setFile方法。 To solve this move the construction of File into the setFile method: 要解决此问题,请将File的构造移到setFile方法中:

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. 在这两个示例中,似乎都不需要fileLocation成员变量,因此已将其删除。

Wrong path will never throw a NullPointerException , so problem is not in here. 错误的路径永远不会抛出NullPointerException ,因此问题不在这里。

EDIT: File can throw a NullPointerException when the specified path is null . 编辑:当指定的路径为null时, File可以抛出NullPointerException And you're calling it before you initialize the fileLocation , try 并且在初始化fileLocation之前先调用它,然后尝试

public Tournament(String x){
     fileLocation=x;
     file = new File(fileLocation);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM