【问题标题】:How to call instance method in static method如何在静态方法中调用实例方法
【发布时间】:2013-04-16 14:13:19
【问题描述】:

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

【问题讨论】:

  • 我怀疑该行导致错误。您仔细查看堆栈跟踪了吗?
  • 谢谢大家。我已经修好了

标签: java file-io filereader


【解决方案1】:

试试:

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

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

【讨论】:

  • 没有没有。这就是问题:一个私有实例变量 fileName,它存储 FileController 类将要读取和写入的文件名。 (i) 初始化 (a) 中的 fileName 变量的构造函数。 (ii) 方法 readLine 从 fileName 中指定的文件中读取一行并将该行返回一个字符串。 (iii) 一个方法 writeLine,它接受一个字符串作为参数并将其作为一行写入 fileName 中指定的文件。 (iv) 一个 main() 方法来测试 FileController 类。文件名应该是私有的
  • 使用我提供的代码,您的程序可以运行。私人不是问题。如果您在不提供输入的情况下创建 FileController,则 fileName 将永远不会被声明,因此为 NULL!您将需要您的构造函数看起来像这样。而且您的 fileName 仍然是私有的,在创建 FileController 对象后您将无法对其进行编辑,因为您既没有 getter 也没有 setter :)
  • @Rauryn 所以您隐瞒了一些信息,现在您将其发布在对答案的评论中?您也可以使用此信息更新问题吗?不过 Xavjer 的回答是正确的。
【解决方案2】:

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

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

还有这样的无参数构造函数:

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

【讨论】:

  • 我们必须使用“this”?
  • @AchintyaJha 当然是的。否则,您会将输入参数分配给自身。
【解决方案3】:
FileController fs = new FileController();

应该是

FileController fs = new FileController("fileName");

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

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

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

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

【讨论】:

  • @Rauryn 你能编辑你的问题并向我们展示你遇到了什么错误吗?
  • 这不起作用,因为带有输入字符串的构造函数只会声明输入字符串而不是私有变量。您必须更改构造函数“this.fileName = fileName”
  • ...当然必须,它是同名的。构造函数的每个参数都会隐藏对象的一个​​字段——在构造函数文件名内部是构造函数第一个参数的本地副本。要引用字符串字段 fileName,构造函数必须使用 this.fileName。
【解决方案4】:

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

【讨论】:

    【解决方案5】:

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

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

    而且你的构造函数需要改成:

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

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

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

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

    【讨论】:

      【解决方案6】:

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

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

      【讨论】:

        【解决方案7】:

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

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-12-03
          • 1970-01-01
          相关资源
          最近更新 更多