简体   繁体   English

无法写入Java txt文件

[英]Can not write to java txt file

I am currently using blueJ to learn java and I have an assignment where I have to write to a txt file, check if the file exists and read the file. 我当前正在使用blueJ学习Java,并且有一个作业,我必须在其中写入txt文件,检查该文件是否存在并读取该文件。 The code I have is below which compiles but when I try to run the write() method,I receive the follow error java.lang.nullpointerexception; 我下面的代码可以编译,但是当我尝试运行write()方法时,收到以下错误java.lang.nullpointerexception;

I do not know where I am going wrong and it is beginning to drive me nuts at this stage. 我不知道我要去哪里错,在这个阶段它开始使我发疯。

import java.io.*;

public class ReadWrite
{
// instance variables - replace the example below with your own
private String file;
private String text;

/**
 * Constructor for objects of class ReadWrite
 */
public ReadWrite(String file, String text)
{
    // initialise instance variables
    file=this.file;
    text=this.text;
}

public void write() 
{


    try{
        FileWriter writer = new FileWriter(file);

        writer.write(text);
        writer.write('\n');
        writer.close();
    }
    catch(IOException e)
    {
        System.out.print(e);
    }


}

public boolean writeToFile()
{

    boolean ok;

    try{

        FileWriter writer = new FileWriter(file);

        {
          write();
        }

        ok=true;
    }

    catch(IOException e) {

        ok=false;

    }

    return ok;

    }

public void read(String fileToRead)
{
    try {
         BufferedReader reader = new BufferedReader(new        FileReader(fileToRead));
         String line = reader.readLine();

           while(line != null) {
               System.out.println(line);
               line = reader.readLine();
            }

            reader.close();

                }
                catch(FileNotFoundException e) {


                }  
                catch(IOException e) {

                } 
}

} }

Your constructor is assigning the values in reverse. 您的构造函数正在反向分配值。 At the moment you have 此刻你有

public ReadWrite(String file, String text)
{
    // initialise instance variables
    file=this.file;
    text=this.text;
}

This is assigning the incoming variables file and text to the instance variables, which are null. 这会将传入变量filetext分配给实例变量,这些变量为null。

What you need to have is this: 您需要具备的是:

public ReadWrite(String file, String text)
{
    // initialise instance variables
    this.file = file;
    this.text = text;
}

A useful way to avoid this in the future is to make your parameters final - this means you can't assign anything to them, and you'll catch this in the compiler. 将来避免这种情况的一种有用方法是使您的参数final可用-这意味着您无法为其分配任何内容,并且会在编译器中捕获到该错误。

public ReadWrite(final String file, final String text)
{
    // won't compile!
    file = this.file;
    text = this.text;
}

A further improvement would be to make the instance variables file and text final , which means they have to be assigned. 进一步的改进是使实例变量filetext final ,这意味着必须对其进行分配。 This way, you're using the compiler to help you catch errors. 这样,您正在使用编译器来帮助您捕获错误。

public class ReadWrite
{
    private final String file;
    private final String text;

    public ReadWrite(final String file, 
                     final String text)
    {
        this.file = file;
        this.text = text;
    }

    // ...
}

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

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