问题是 Logger 没有使用 Logger.getLogger(class_name.class.getName())
方法初始化。你可以看到下面的代码。我标记了导致异常的行。我必须注意,我也尝试过不使用静态setLevel
块。
// File that contains logger
public class Experience {
private final static Experience INSTANCE = new Experience();
private final static Logger LOGGER = Logger.getLogger(Experience.class.getName());
private MainFrame main_frame;
static {
LOGGER.setLevel(Level.SEVERE);
}
public static Experience getInstance()
{
return INSTANCE;
}
private Experience()
{
try
{
FileHandler fh = new FileHandler(System.getProperty("user.dir") + "/log.txt");
fh.setFormatter(new XPLogFormatter());
LOGGER.addHandler(fh); // NULLPOINTEREXCEPION HERE
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
// Just a loader
public class Loader
{
public static void main(String args[])
{
JFrame.setDefaultLookAndFeelDecorated(true);
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
MainFrame main_frame = new MainFrame();
}
});
}
}
// Stripped down version of main frame
public class MainFrame extends JFrame
{
private Experience XP;
public MainFrame()
{
super();
XP = Experience.getInstance(this);
}
}
// Modified formatter
public class XPLogFormatter extends Formatter
{
@Override
public String format(LogRecord record)
{
StringBuffer buf = new StringBuffer(1000);
buf.append(formatMessage(record));
return buf.toString();
}
}
你能告诉我究竟是什么导致了这个错误吗?