Null pointer exception for log4j

504 views
Skip to first unread message

reidm...@gmail.com

unread,
Jun 26, 2017, 10:50:25 PM6/26/17
to Selenium Users
Hello,

1. I added log4j dependency in maven.
2. I created log4j.properties.
3. In my BaseTest.class, I have this.

package com.diceBase;

import org.apache.log4j.BasicConfigurator;

import org.apache.log4j.Logger;

import org.apache.log4j.PropertyConfigurator;

import org.junit.BeforeClass;

import org.openqa.selenium.WebDriver;

import org.testng.ITestContext;

import org.testng.annotations.AfterMethod;

import org.testng.annotations.BeforeMethod;

import org.testng.annotations.Parameters;

 

public class BaseTest {

 

 
protected WebDriver driver;

 
protected Logger log;

 

 
@BeforeClass

 
protected void setup(ITestContext ctx){

 
BasicConfigurator.configure();

 
String testName = ctx.getCurrentXmlTest().getName();

 log
= Logger.getLogger(testName);

 
PropertyConfigurator.configure("log4j.properties");

 
}

 

 

 
@Parameters ({"browser"})

 
@BeforeMethod

 
protected void Methodstart(String browser){

 log
.info("start method");

 driver
= BrowsersgetDriver(browser);

 
}

 

 
@AfterMethod

 
protected void TearDown(){

log
.info("end method");

 

 
try {

 
Thread.sleep(5000);

 driver
.quit();

 
} catch (Exception e) {

 
}

 
}

}



When I run my xml suite file, I get error "null pointer exception" at line


log.info("start method");


Can you please advise why I get this error?

Krishnan Mahadevan

unread,
Jun 26, 2017, 11:04:25 PM6/26/17
to seleniu...@googlegroups.com

Its because of the fact that you are trying to create a logger instance based on the current <test> name

 

String testName = ctx.getCurrentXmlTest().getName();

You would need to alter this logic such that you strip out any spaces in the value of “testName” else the line log = Logger.getLogger(testName); is going to cause null to be assigned to log.

 

Refer to this SO post : https://stackoverflow.com/a/27833960

 

Thanks & Regards

Krishnan Mahadevan

 

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

My Scribblings @ http://wakened-cognition.blogspot.com/

My Technical Scribbings @ http://rationaleemotions.wordpress.com/

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/d801c372-fa5c-4f70-b507-98880e25edf1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages