0

所以我想要做的是读取一个.txt文件并使用 eclipse 在上面添加一些记录。我将我命名为“ fileName”的资源设置为私有,当我尝试在 main 方法中调用它时,出现了一些错误。这是我的代码:

public class FileController {
    private String fileName;

    public FileController() {
    }

    public FileController(String fileName) {
        fileName = "student.txt";
    }

    public void readLine() {

        try {
            FileReader fr = new FileReader(fileName);
            Scanner sc = new Scanner(fr);

            // read in the file line by line
            while (sc.hasNextLine()) {
                System.out.println(sc.nextLine());
            }

            fr.close();
        } catch (FileNotFoundException exception) {
            System.out.println("The file " + fileName + " was not found.");
        } catch (IOException exception) {
            System.out.println(exception);
        }

    }

    public void writeLine() {

        try {
            // create the PrintWriter
            FileWriter fw = new FileWriter(fileName, true);
            BufferedWriter bw = new BufferedWriter(fw);
            PrintWriter outFile = new PrintWriter(bw);

            // write value out to the file
            outFile.println("Coke is nice");
            outFile.println("Diet Coke is even better cos won't put on weight =)");

            // close the file
            outFile.close();

            System.out.println("File created: " + fileName);
        } catch (IOException exception) {
            System.out.println(exception);
        }
    }

    public static void main(String[] args) {
        FileController fs = new FileController();
        fs.readLine();
        fs.writeLine();
    }

}

任何人都可以给我一些线索?这些代码不断给我NullPointerException 错误。我知道它来自FileController fs = new FileController()那一行,但我不知道如何在静态方法中调用实例方法。

4

7 回答 7

2
FileController fs = new FileController();

应该

FileController fs = new FileController("fileName");

您还应该像这样编辑您的构造函数。由于构造函数中的类变量文件名和参数名称具有相同的名称,您必须使用“this”关键字进行赋值。

public FileController(String fileName) {
    this.fileName = fileName;
}

如果参数名称和类变量名称不同,您可以这样做。

public FileController(String name) {
    fileName = name;
}
于 2013-04-16T14:14:56.487 回答
2

尝试:

public FileController() {
    fileName = "student.txt";
}

public FileController(String fileName) {
    this.fileName = filename;
}
于 2013-04-16T14:16:31.863 回答
2

我认为你的构造函数应该是这样的:

public FileController(String fileName) {
    this.fileName = fileName;
}

像这样的无参数构造函数:

public FileController() {
    this("student.txt");
}
于 2013-04-16T14:16:56.133 回答
0

您没有在构造函数中传递文件的名称。您应该在其中传递一个有效的文件名,以便您的逻辑正常工作

于 2013-04-16T14:16:54.430 回答
0

您需要使用参数调用构造函数,或者默认构造函数应提供默认文件名,例如,在您的 main 中:

FileController fs = new FileController("somefile.txt")

并且您的构造函数需要更改为:

public FileController(String filename) {
     this.fileName = filename;
}

你可以改变你的默认构造函数:

public FileController() {
     this("someDefaultFile.txt");
}

请记住,只有在要查找默认文件时,后一个选项才有意义,否则您应该明确传递文件的名称。

于 2013-04-16T14:17:25.133 回答
0

可以使用您尝试调用其方法的类的对象引用在静态方法中访问实例方法。

因此,正如您正确指出的那样,使用 new 关键字将解决您的问题。

于 2013-04-16T14:18:27.560 回答
0

问题是通过调用 new FileController(); 您没有初始化 fileName 字段。您可能希望您的构造函数如下所示:

    public FileController() {
        this.fileName = "student.txt";
    }

    public FileController(String fileName) {
        this.fileName = fileName;
    }

然后将调用 new FileController(); 合法的。

于 2013-04-16T14:22:48.520 回答