-1

I have the following problem:

I have app. which I want to log information to JTextArea with Log4J. I know that I must create a class which extends AppenderSkeleton and to make a logic to log message and append it to the text area component when it click on run button. This is the class I made:

public class TextAreaAppender extends AppenderSkeleton
{
    public static JTextArea logTextArea = new JTextArea();

    @Override
    public void append(LoggingEvent logEvent)
    {
        String logMessage = this.layout.format(logEvent);
        logTextArea.append(logMessage);
    }

    @Override
    public void close()
    {

    }

    @Override
    public boolean requiresLayout()
    {
        return false;
    }
}

In the class the UI and logic for buttons there is the following code for the text area component:

logTextArea = new JTextArea();

logTextArea.setEnabled(false);
scrollTextArea = new JScrollPane(logTextArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollTextArea.setBounds(50, 300, 300, 140);
TextAreaAppender.logTextArea = logTextArea;

Here is my config in log4j properties file:

log4j.rootLogger = INFO, textarea
log4j.appender.textarea = bis.debug.mode.ui.TextAreaAppender
log4j.appender.textarea.layout = org.apache.log4j.PatternLayout
log4j.appender.textarea.layout.ConversionPattern= %d{HH:mm:ss} -%m%n

And now when I click the run button it must log the errors which are happened to the text area component but it throws an Exception :

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException.. 

Which means that no log message is appended and I do not know why it happens. Can you please tell me how?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
George
  • 75
  • 1
  • 10
  • Can you put the console data when hitting the run button for more clarification. – Nayan Patel Aug 22 '18 at 08:29
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). .. – Andrew Thompson Aug 22 '18 at 08:35
  • .. 3) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) – Andrew Thompson Aug 22 '18 at 08:36
  • BTW `public static JTextArea logTextArea = new JTextArea();` This variable declaration seems to shadow the other `logTextArea` which is actually added to the GUI. It doesn't (will **never**) work like that. `TextAreaAppender` might have a constructor which accepts the `JTextArea` to which the logger must append. – Andrew Thompson Aug 22 '18 at 08:41

1 Answers1

0

You are using the inherited layout attribute in your append method, which is null as you have not initialized it yet. You can either set up your own layout in the append method, initialize the attribute before using append or just not use it in the append method at all as it is optional.

@Override
    public void append(LoggingEvent logEvent)
    {
        String logMessage = this.layout.format(logEvent);
        logTextArea.append(logMessage);
    }
Cristian G.
  • 135
  • 7