java - 简单的记录器类但抛出空指针异常?

标签 java

我编写了一个简单的记录器类,我可以创建一个实例,它允许我将信息写入文件。不久前它在一个示例中工作,但现在抛出 NULL 指针异常错误。我实在不知道为什么会这样。我已将类代码放在下面。

类代码:

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Date;

/*This class is a simple logger that currently only has one message but will be added to it in time*/
public class DDHLogger  {
    Date timeStamp = new Date();
    PrintWriter pw = null;
    FileWriter fw;
    File file;

    //FileOutputStream   fstre;  
    //OutputStreamWriter outFileStream;


    /**
     * Used for creating the file when the object is implemented.
     * @param yourstring
     * @param yourfilename
     * @throws IOException -- file check
     */
    public DDHLogger(String filePath, String yourfilename) {
        System.out.println("filePath: " + filePath);
        System.out.println("yourfilename: " + yourfilename);
         //String fileName = "C:\\Users\\home-1\\Desktop\\" + yourfilename;
         String fileName = filePath + yourfilename;
         System.out.println("fileName::--> " + fileName);
        // file = new File(fileName);//Creates the file
         file = new File(fileName);//Creates the file
         System.out.println("file: " + file);
         try {
            fw = new FileWriter(file, true);
        } catch (IOException e) {
            e.printStackTrace();
        }//allows append to the file without over writing. The TRUE keyword is used for append
    }

    /**
     * Must supply two parmaters. The first parmater is the string that is to be written to the file. The second parmater is used for
     * the file name. The extension C: for the default path.
     * @param yourstring
     * @param yourfilename
     */
    void Error(String yourstring, String lineNumber){
        openPrintWriterStream(pw);
        try{
            pw.println("ERROR: " + yourstring + " * " + timeStamp.toString() +" * " + " Line Number: " + lineNumber + "\n");
            pw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }  
    }

    void Debug(String yourstring, String lineNumber){
         openPrintWriterStream(pw);
         try{
             pw.println("DEBUG: " + yourstring + " ******* " + timeStamp.toString() +" ******* " + " Line Number: " + lineNumber + "\n");
             pw.close();
         } catch (Exception e) {
             e.printStackTrace();
         }   
   }

   void Info(String yourstring, String lineNumber) {
       openPrintWriterStream(pw);
       try{
           pw.println("INFORMATION: " + yourstring + " ******* " + timeStamp.toString() +" ******* " + " Line Number: " + lineNumber + "\n");
           pw.close();
       } catch (Exception e) {
           e.printStackTrace();
       } 
   }

   void Warn(String yourstring, String lineNumber){
       openPrintWriterStream(pw);
       try{
           pw.println("Warning: " + yourstring + " ******* " + timeStamp.toString() +" ******* " + " Line Number: " + lineNumber + "\n");
           pw.close();
       } catch (Exception e) {
           e.printStackTrace();
       }    
  }

   void openPrintWriterStream(PrintWriter pw) {
       pw = new PrintWriter(fw);
         try {
            fw = new FileWriter(file, true);
        } catch (IOException e) {
            e.printStackTrace();
        } 
   }
}

空指针异常:

filePath: C:\Users\home-1\Desktop\
yourfilename: PokerLogger.txt
fileName::--> C:\Users\home-1\Desktop\PokerLogger.txt
file: C:\Users\home-1\Desktop\PokerLogger.txt
java.lang.NullPointerException
    at DDHPokerGame.DDHLogger.Info(DDHLogger.java:76)
    at DDHPokerGame.DDHGamePanel$1.mouseEntered(DDHGamePanel.java:150)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.trackMouseEnterExit(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
java.lang.NullPointerException
    at DDHPokerGame.DDHLogger.Info(DDHLogger.java:76)
    at DDHPokerGame.DDHGamePanel$1.mouseExited(DDHGamePanel.java:155)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.trackMouseEnterExit(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
java.lang.NullPointerException
    at DDHPokerGame.DDHLogger.Info(DDHLogger.java:76)
    at DDHPokerGame.DDHGamePanel$1.mouseEntered(DDHGamePanel.java:150)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.trackMouseEnterExit(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
java.lang.NullPointerException
    at DDHPokerGame.DDHLogger.Info(DDHLogger.java:76)
    at DDHPokerGame.DDHGamePanel$1.mouseExited(DDHGamePanel.java:155)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.trackMouseEnterExit(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

新的和清理的代码:

/*This class is a simple logger that currently only has one message but will be added 
  to it in time*/

public class DDHLogger  {
    Date timeStamp = new Date();
    PrintWriter pw = null;
    FileWriter fw = null;
    File file;

    /**
     * Used for creating the file when the object is implemented.
     * @param yourstring
     * @param yourfilename
     * @throws IOException -- file check
     */
    public DDHLogger(String filePath, String yourfilename) {
        System.out.println("filePath: " + filePath);
        System.out.println("yourfilename: " + yourfilename);
         //String fileName = "C:\\Users\\home-1\\Desktop\\" + yourfilename;
         String fileName = filePath + yourfilename;
         System.out.println("fileName::--> " + fileName);
        // file = new File(fileName);//Creates the file
         file = new File(fileName);//Creates the file
         System.out.println("file: " + file);
         try {
            fw = new FileWriter(file, true);
        } catch (IOException e) {
            e.printStackTrace();
        }//allows append to the file without over writing. The TRUE keyword is used for append


    }

    /**
     * Must supply two parmaters. The first parmater is the string that is to be written to the file. The second parmater is used for
     * the file name. The extension C: for the default path.
     * @param yourstring
     * @param yourfilename
     */
    void Error(String yourstring, String lineNumber){

        try{
            pw.println("ERROR: " + yourstring + " * " + timeStamp.toString() +" * " + " Line Number: " + lineNumber + "\n");
        } catch (Exception e) {
            e.printStackTrace();
        }  
    }

    void Debug(String yourstring, String lineNumber){

         try{
             pw.println("DEBUG: " + yourstring + " ******* " + timeStamp.toString() +" ******* " + " Line Number: " + lineNumber + "\n");
         } catch (Exception e) {
             e.printStackTrace();
         }   
   }

   void Info(String yourstring, String lineNumber) {

       try{
           pw.println("INFORMATION: " + yourstring + " ******* " + timeStamp.toString() +" ******* " + " Line Number: " + lineNumber + "\n");
   } catch (Exception e) {
           e.printStackTrace();
       } 
   }

   void Warn(String yourstring, String lineNumber){

       try{
           pw.println("Warning: " + yourstring + " ******* " + timeStamp.toString() +" ******* " + " Line Number: " + lineNumber + "\n");
       } catch (Exception e) {
           e.printStackTrace();
       }    
  }

   void openPrintWriterStream(PrintWriter pw) {
        pw = new PrintWriter(fw);

        try {
            fw = new FileWriter(file, true);
        } catch (IOException e) {
            e.printStackTrace();
        } 
   }

    void closeFile() {
   pw.close();
     }
}

相同的空指针异常:

我更改了代码,但仍然收到相同的空点异常。我什至不需要 PrintWriter pw = null 变量?

filePath: C:\Users\home-1\Desktop\
yourfilename: subtract.txt
fileName::--> C:\Users\home-1\Desktop\subtract.txt
file: C:\Users\home-1\Desktop\subtract.txt
java.lang.NullPointerException
    at DDHTestBufferedImage.DDHLogger.Debug(DDHLogger.java:62)
    at DDHTestBufferedImage.SubtractBufferedImage.main(SubtractBufferedImage.java:40)

最佳答案

您的openPrintWriterStream实际上并不影响名为pw的成员变量。另外,由于您在方法的最开始重新分配了 pw 引用(并且不返回更新后的引用),因此分配基本上是无效的。去掉 pw 参数,以便赋值实际上更新成员变量。您需要进行检查以确保 pw 尚未打开。

关于java - 简单的记录器类但抛出空指针异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20913711/

相关文章:

java - 我可以配置 Wildfly 从一个域名重定向到另一个域名吗?

java - 未找到 Eclipse 部署描述符

java - 如何在java程序中使用MongoExport命令?

java - Wiremock 代理记录功能 - 它可以替代响应中的 Host 或 IP 吗?

java - hibernate:父级的子ID检索列表

java - Spark 缓存与数据库的数据检索速度慢

java - 用于指示复选框已选中的验证注释

java - 创建对象数组时的构造函数

java - 使用多个数组列表或带有 stringtokenizer 的数组在 java 中编写解释器

Java 正则表达式模仿数字的 if-else