简体   繁体   English

为什么在另一个类中使用PrintWriter时会出现PrintWriter空指针异常

[英]Why do I get a PrintWriter null pointer exception when using PrintWriter in another class

I have been using Java for a while for tasks and support tools, but I have never worked out how to use PrintWriter in a class that I can then use from another class, for example, I have several XML files that I want to parse, and then output to tables from a single input source (an .xls report), and I have several output files for the different XML's and child nodes. 我使用Java做任务和支持工具已有一段时间了,但是我从来没有想过如何在一个类中使用PrintWriter ,然后可以从另一个类中使用它,例如,我有几个XML文件要解析,然后从单个输入源(.xls报告)输出到表,我有几个针对不同XML和子节点的输出文件。 So I created this class to create a PrintWriter and report if the output file exists already: 因此,我创建了此类来创建PrintWriter并报告输出文件是否已存在:

public class PrintAdmin 
{
    public PrintWriter pout;
    public int PrintFromElement = 0;

    public PrintAdmin(String Dirname) throws IOException
    {
        PrintWriter pout = new PrintWriter(new BufferedWriter(new FileWriter((Dirname))));
        int PrintFromElement = 0;
        File f1 = new File("FirstNode.CSV");
        if(f1.exists()) 
        { PrintFromElement = 1; }
        else
        { PrintFromElement = 0; }
    }
    public void poutclose()
    {
        this.pout.close();
    }

}

This is called from: 这被称为:

public class AuditParser 
{
    public static File Auditor;

    public static void main(String[] args) throws IOException 
    { PrintAdmin xmltype1out = new PrintAdmin(xmltype1);
      ...
      for (int xml1 = PrintAdmin.PrintFromElement: xml1 < xml1parsed.header.size();xml1++)
      {
          ...dostuff...
      }

And this pattern is repeated 4 or 5 times. 并将此模式重复4到5次。

My problem is that while I get a good compile using PrintAdmin in AuditParser , at runtime I get a NullPointerException . 我的问题是,尽管我在AuditParser使用PrintAdmin进行了良好的编译,但在运行AuditParser得到了NullPointerException

I want to do it this way to have the simple class that creates a PrintWriter which instructs to print the header record or not depending on whether the file exits already. 我想通过这种方式来完成一个简单的类,该类创建一个PrintWriter ,根据文件是否已退出来指示是否打印标题记录。 It is useful here, and I am sure it would be useful for me elsewhere.... 这在这里很有用,我相信它对其他地方的我也将有用。

You're defining a local variable in the constructor that's shadowing the field, which is never assigned. 您在构造函数中定义了一个局部变量,该局部变量遮蔽了从未分配的字段。 Drop the PrintWriter token in the constructor. PrintWriter令牌放在构造函数中。

The problem is with this 问题是这个

public PrintWriter pout;

declared an instance variable but it is never initialized but question is why because in your constructor you did something like this 声明了一个实例变量,但是它从未被初始化,但是问题是为什么,因为在构造函数中您做了这样的事情

public PrintAdmin(String Dirname) throws IOException{
   PrintWriter pout = new PrintWriter(...);
}

which is creating a new local variable pout and it is initialized but the field defined above is never initialized. 它正在创建一个新的局部变量pout并被初始化,但是上面定义的字段从未初始化。 you can fix this by doing it this way 您可以通过这种方式解决此问题

public PrintAdmin(String Dirname) throws IOException{
       pout = new PrintWriter(...);
    }

you also dont need that PrintFromElement again in your constructor you have already defined it as the field 您也不需要在构造函数中再次需要PrintFromElement ,您已经将其定义为字段

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

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