[slf4j-user] Logger in subclass in NULL

Ulrich Forums at gombers.de
Thu Jun 6 17:17:55 CEST 2013


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


More information about the slf4j-user mailing list