[slf4j-user] Logger in subclass in NULL
Robert Elliot
rob at lidalia.org.uk
Thu Jun 6 18:33:14 CEST 2013
Loggers do not need to be static, and there's no particular reason why they would be beyond normal reasons why you might make a variable static. Though in this case making them static would fix the problem.
Ulrich - inherently your problem has nothing to do with SLF4J, it's a pure Java bug in your code. The first thing the constructor of a sub class does is call the constructor on the super class. It will do this before initialising any fields defined in the subclass. If you call an over-ridden method in the constructor of the super class which uses a field on the sub-class, it will be uninitialised.
Try this following application which does not use a logger - it will fail in exactly the same way:
public class TestSuperClass {
public TestSuperClass() {
display();
}
public void display() {
}
}
public class TestSubClass extends TestSuperClass {
private final Integer value = 5;
public TestSubClass() {
super();
}
@Override
public void display() {
System.out.println(value.toString());
}
public static void main(String[] args) {
new TestSubClass();
}
}
----- Original Message -----
> From: "Chris Pratt" <thechrispratt at gmail.com>
> To: "User list for the slf4j project" <slf4j-user at qos.ch>
> Sent: Thursday, 6 June, 2013 5:17:08 PM
> Subject: Re: [slf4j-user] Logger in subclass in NULL
> Loggers should be static.
> (*Chris*)
>
>
>
> On Thu, Jun 6, 2013 at 8:17 AM, Ulrich < Forums at gombers.de > wrote:
>
>
> When calling override-method in a subclass from the constructor of the
> superclass the LOGGER in the subclass is not initialized; the program
> fails with
> NullPointerException.
> E.g:
> Subclass is:
> import org.slf4j.Logger;
> import org.slf4j.LoggerFactory;
>
> public class TestLoggerSubClass extends TestLoggerSuperClass {
>
> private final Logger LOGGER =
> LoggerFactory.getLogger(this.getClass());
> public TestLoggerSubClass() {
> super("TestLoggerSubClass");
> }
>
> @Override
> public void display() {
> int summe = 15+5;
> LOGGER.info("Summe={}",summe);
> }
> }
>
>
> Superclass is:
> import org.slf4j.Logger;
> import org.slf4j.LoggerFactory;
>
> public class TestLoggerSuperClass {
>
> private final Logger LOGGER =
> LoggerFactory.getLogger(this.getClass());
> public TestLoggerSuperClass(String name) {
> LOGGER.info("wurde mit Namen {} gerufen", name);
> display();
> }
>
> public void display() {
> int summe = 5+5;
> LOGGER.info("Summe={}",summe);
> }
> }
>
> The program fails, as already said, with NullPointerException in the
> LOGGER-Statement of the Override-Method in the subclass.
>
> Can I anything do to have the LOGGER initialized before invoking the
> constructor
> of the superclass?
>
> Thanks in advance,
> Ulrich
> _______________________________________________
> slf4j-user mailing list
> slf4j-user at qos.ch
> http://mailman.qos.ch/mailman/listinfo/slf4j-user
>
>
> _______________________________________________
> slf4j-user mailing list
> slf4j-user at qos.ch
> http://mailman.qos.ch/mailman/listinfo/slf4j-user
More information about the slf4j-user
mailing list