Reputation: 8487
While I'm aware of Log4J, and I'm sure there are others, I'm using java.util.logging
to write to a file.
The error originates in nextTitleActionPerformed
which is near the bottom of the soure code.
I've looked at a few questions on SO, but must be misunderstanding the mechanism for logging to a file.
However, logging to a file results in a NullPointerException
as below:
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$
Apparently, I'm not attaching the handler correctly?
code:
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
clicking the button causes the error.
see also:
http://www.javapractices.com/topic/TopicAction.do?Id=143
Upvotes: 0
Views: 214
Reputation: 24192
while youre right that most devs shun java.util.logging in favor of something better, your issue isnt related to logging at all:
title = null; <---- null
Date date = new Date();
Timestamp timestamp = new Timestamp(date.getTime());
log.info(title.toString()); <---- STILL null
title.setTitle("modified from GUI");
Upvotes: 1