虽然我知道Log4J,并且我确定还有其他人,但我使用java.util.logging
来写入文件。
错误源自nextTitleActionPerformed
,它靠近源代码的底部。
我已经在SO上查看了几个问题,但必须误解记录到文件的机制。
但是,记录到文件会产生NullPointerException
,如下所示:
BUILD SUCCESSFUL
Total time: 3 seconds
Jul 27, 2014 12:21:50 AM net.bounceme.dur.client.gui.LoggingGUI <init>
INFO: starting log..
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at net.bounceme.dur.client.gui.LoggingGUI.nextTitleActionPerformed(LoggingGUI.java:103)
at net.bounceme.dur.client.gui.LoggingGUI.access$000(LoggingGUI.java:10)
at net.bounceme.dur.client.gui.LoggingGUI$2.actionPerformed(LoggingGUI.java:56)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3311)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
thufir@dur:~/NetBeansProjects/Log$
显然,我没有正确附加处理程序?
代码:
package net.bounceme.dur.client.gui;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
public final class LoggingGUI extends javax.swing.JFrame {
private static final Logger log = Logger.getLogger(LoggingGUI.class.getName());
private Title title = null;
private FileHandler fh = null;
public LoggingGUI() throws IOException {
initComponents();
fh = new FileHandler("gui.log");
log.addHandler(fh);
log.setLevel(Level.ALL);
log.info("starting log..");
}
public static void main(String... args) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
new LoggingGUI().setVisible(true);
} catch (IOException ex) {
Logger.getLogger(LoggingGUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
stateGroup = new javax.swing.ButtonGroup();
jTabbedPane1 = new javax.swing.JTabbedPane();
titlePanel = new javax.swing.JPanel();
nextTitle = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
nextTitle.setText("next");
nextTitle.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
nextTitleActionPerformed(evt);
}
});
javax.swing.GroupLayout titlePanelLayout = new javax.swing.GroupLayout(titlePanel);
titlePanel.setLayout(titlePanelLayout);
titlePanelLayout.setHorizontalGroup(
titlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(titlePanelLayout.createSequentialGroup()
.addGap(94, 94, 94)
.addComponent(nextTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 557, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(144, Short.MAX_VALUE))
);
titlePanelLayout.setVerticalGroup(
titlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(titlePanelLayout.createSequentialGroup()
.addGap(73, 73, 73)
.addComponent(nextTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 286, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(101, Short.MAX_VALUE))
);
jTabbedPane1.addTab("tab1", titlePanel);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(26, 26, 26)
.addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 803, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(30, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jTabbedPane1)
.addContainerGap())
);
pack();
}// </editor-fold>
private void nextTitleActionPerformed(java.awt.event.ActionEvent evt) {
title = null;
Date date = new Date();
Timestamp timestamp = new Timestamp(date.getTime());
log.info(title.toString());
title.setTitle("modified from GUI");
}
// Variables declaration - do not modify
private javax.swing.JTabbedPane jTabbedPane1;
private javax.swing.JButton nextTitle;
private javax.swing.ButtonGroup stateGroup;
private javax.swing.JPanel titlePanel;
// End of variables declaration
}
handlers=java.util.logging.FileHandler, java.util.logging.ConsoleHandler
.level=ALL
myapp.ui.level=ALL
myapp.business.level=CONFIG
myapp.data.level=SEVERE
net.bounceme.dur.level=ALL
java.util.logging.ConsoleHandler.level=SEVERE
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.FileHandler.level=ALL
java.util.logging.FileHandler.pattern=%h/java%u.log
java.util.logging.FileHandler.limit=50000
java.util.logging.FileHandler.count=1
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
单击该按钮会导致错误。
另见:
答案 0 :(得分:1)
虽然你是对的,大多数开发人员都避免使用java.util.logging来支持更好的事情,但你的问题根本不与日志记录有关:
title = null; <---- null
Date date = new Date();
Timestamp timestamp = new Timestamp(date.getTime());
log.info(title.toString()); <---- STILL null
title.setTitle("modified from GUI");