如何在静态方法中调用实例方法

时间:2013-04-16 14:13:19

标签: java file-io filereader

所以我要做的是读取.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()那一行,但我不知道如何在静态方法中调用实例方法。

7 个答案:

答案 0 :(得分:2)

FileController fs = new FileController();

应该是

FileController fs = new FileController("fileName");

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

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

如果参数名称和类变量名称不同,则可以执行此操作。

public FileController(String name) {
    fileName = name;
}

答案 1 :(得分:2)

尝试:

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

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

答案 2 :(得分:2)

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

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

这样的无参数构造函数:

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

答案 3 :(得分:0)

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

答案 4 :(得分:0)

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

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

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

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

您可以更改默认构造函数:

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

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

答案 5 :(得分:0)

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

因此,正确指出使用新关键字可以解决您的问题。

答案 6 :(得分:0)

问题在于调用新的FileController();你没有初始化fileName字段。 您可能希望构造函数看起来像这样:

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

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

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

相关问题