2

I have class LoggerInitializer with method:

public static void initLogger() {
    try {
        LogManager
                .getLogManager()
                .readConfiguration(
                Service.class.getResourceAsStream("/logging.properties"));
    } catch (IOException e) {
        System.err.println("Could not setup logger configuration: " + e.toString());
    }
}

When I start test in classes which have method

@BeforeClass
public static void initEnvironment() {
    LoggerInitializer.initLogger();
}

Getting an error

java.lang.NullPointerException
    at java.util.Properties$LineReader.readLine(Properties.java:434)
    at java.util.Properties.load0(Properties.java:353)
    at java.util.Properties.load(Properties.java:341)
    at java.util.logging.LogManager.readConfiguration(LogManager.java:1409)
    at com.netcracker.edu.inventory.LoggerInitializer.initLogger(LoggerInitializer.java:23)
    at com.netcracker.edu.inventory.model.impl.BatteryTest.initEnvironment(BatteryTest.java:18)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

This is my project structure

Project structure

What can be the cause of these errors? How to solve this problem?

RubioRic
  • 2,442
  • 4
  • 28
  • 35
Mentha
  • 73
  • 5
  • `Service.class.getResourceAsStream("/logging.properties")` that gives you `null`. Show mi your project structure where `logging.props` are – Antoniossss Oct 05 '18 at 19:27
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Antoniossss Oct 05 '18 at 19:27
  • `handlers= java.util.logging.FileHandler java.util.logging.FileHandler.pattern = application_log.txt java.util.logging.FileHandler.limit = 1000000 java.util.logging.FileHandler.count = 5 java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter java.util.logging.SimpleFormatter.format="%1$tc %2$s%n%4$s: %5$s%6$s%n" java.util.logging.FileHandler.level = INFO` – Mentha Oct 05 '18 at 19:37
  • and thats the **directory structure** ?? – Antoniossss Oct 05 '18 at 19:44
  • Sorry. Read wrong. https://ibb.co/hOrZie – Mentha Oct 05 '18 at 20:17

2 Answers2

1

Your file is in com package so it should be

Service.class.getResourceAsStream("/com/logging.properties")

either change that, or move that file 1 level higher. Moreover, it should (its not a must but a good practice) be placed in resources directory so it will not be mixed with sources (but the effect would be the same in resulting jar).

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
0

make file with name Log4j.properties in src/resource folder and paste below code.

# DEFAULT FILE OUTPUT IS IN USER'S HOME DIRECTORY.
log4j.rootLogger=DEBUG, Appender1,Appender2
log4j.appender.Appender1=org.apache.log4j.ConsoleAppender
log4j.appender.Appender1.layout=org.apache.log4j.PatternLayout
log4j.appender.Appender1.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n
log4j.appender.Appender2=org.apache.log4j.FileAppender
log4j.appender.Appender2.File=log.txt
log4j.appender.Appender2.layout=org.apache.log4j.PatternLayout
log4j.appender.Appender2.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n

add dependency for log4j

<dependency>
  <groupId>log4j</groupId> 
  <artifactId>log4j</artifactId> 
  <version>1.2.17</version> 
</dependency>

import it in your class

import org.apache.log4j.Logger;

Create logger in your class

Logger logger = Logger.getLogger(className.class);
logger.info("Http Image is converted into Byte Array");
Umang Soni
  • 137
  • 1
  • 2
  • 14