0

Am getting null NullPointerException when running below code, i am just trying read excel path from properties file but getting error, please help me as I'd tried hard to resolve this problem

login script

public class Login1 {
    public static Properties props;
    public static WebDriver driver;
    public InputStream propertiesFilePath = this.getClass().getClassLoader()
    .getResourceAsStream("Config.properties");

    public static void main(String [] arg){
    }

    public  void Readproperties() throws IOException {
        props = new Properties();
        props.load(propertiesFilePath);
    }

    public static void  getDriver(){
        System.setProperty("webdriver.chrome.driver",               "C:\\Users\\aroy\\Desktop\\Sele\\chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver();
    }
    public static void logintoapp(String acc, String passw){
        driver.findElement(By.xpath("//input[@id='userName']")).sendKeys(acc);
        driver.findElement(By.xpath("//input[@id='password']")).sendKeys(passw);
        driver.findElement(By.xpath("//button[@id='loginBtn']")).click();
        System.out.println("Login SuccessFully");
    }
}

Tesstcase:

package AtExecution;

import java.io.IOException;
import org.testng.annotations.Test;

import BeoreExecution.Login1;

public class Testcase extends Login1 {

    public static ExcelLib exel1;

    @Test
    public void login() throws IOException{
        Login1 x = new Login1();
        x.Readproperties();

        exel1 = new ExcelLib(Login1.props.getProperty("TestDataPath"));
        String siteurl = exel1.getCellData("Login_Credentials",    "TestUrl", 2);
        String acc = exel1.getCellData("Login_Credentials", "account", 2);
        String passw=exel1.getCellData("Login_Credentials", "Password", 2);
        Login1.getDriver();
        driver.get(siteurl);
        driver.manage().window().maximize();
        Login1.logintoapp(acc, passw);
    }
}

here is my log

    [RemoteTestNG] detected TestNG version 6.12.0
     FAILED: login
     java.lang.NullPointerException
     at java.util.Properties$LineReader.readLine(Unknown Source)
     at java.util.Properties.load0(Unknown Source)
     at java.util.Properties.load(Unknown Source)
        at BeoreExecution.Login1.Readproperties(Login1.java:30)
     at AtExecution.Testcase.login(Testcase.java:18)
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
     at java.lang.reflect.Method.invoke(Unknown Source)
at            org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:669)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:877)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1201)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:776)
at org.testng.TestRunner.run(TestRunner.java:634)

Please help me to solve this

my config file data is

TestDataPath=C:/Users/Testdata.xlsx

and below is my exelsheet data

link to excel sheet

Manmohan_singh
  • 1,776
  • 3
  • 20
  • 29
Abhijith
  • 41
  • 6
  • Why initialize your WebDriver in your other class? Why not just initialize it in your test case class, then pass it as a parameter to a constructor in your other class? – Reez0 Nov 23 '17 at 18:51
  • Possible duplicate of [NullPointerException in my code. How to deal with it](https://stackoverflow.com/questions/45474353/nullpointerexception-in-my-code-how-to-deal-with-it) – undetected Selenium Nov 23 '17 at 19:35
  • 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) – JeffC Nov 23 '17 at 22:30

2 Answers2

0

You did't instantiate WebDriver instance in your test case class. You can follow the below way:

Return WebDriver from the method -

public static WebDriver  getDriver(){
    System.setProperty("webdriver.chrome.driver",               "C:\\Users\\aroy\\Desktop\\Sele\\chromedriver_win32\\chromedriver.exe");
    driver = new ChromeDriver();
    return driver;
}

Then call this method inside login test method like

WebDriver driver = Login1.getDriver();

Finally continue your next code.

Ali Azam
  • 2,047
  • 1
  • 16
  • 25
  • @Abhijith how did you call the getDriver() method? Can you send me your implemented code in [chat](https://chat.stackoverflow.com/) box. – Ali Azam Nov 25 '17 at 07:14
  • Hi Ali, i cant able to create box – Abhijith Nov 25 '17 at 07:31
  • Login1 x = new Login1(); x.Readproperties(); exel1 = new ExcelLib(Login1.props.getProperty("TestDataPath")); String siteurl = exel1.getCellData("Login_Credentials", "TestUrl", 2); String acc = exel1.getCellData("Login_Credentials", "account", 2); String passw=exel1.getCellData("Login_Credentials", "Password", 2); Login1.getDriver(); driver.get(siteurl); driver.manage().window().maximize(); Login1.logintoapp(acc, passw); – Abhijith Nov 25 '17 at 07:34
  • @Abhijith Can you please send you getDriver method definition also? – Ali Azam Nov 25 '17 at 07:42
  • pls find getdrivermethod – Abhijith Nov 25 '17 at 07:46
  • public static WebDriver getDriver(){ System.setProperty("webdriver.chrome.driver", "C:\\Users\\aroy\\Desktop\\Sele\\chromedriver_win32\\chromedriver.exe"); driver = new ChromeDriver(); return driver; } – Abhijith Nov 25 '17 at 07:46
  • @Abhijith please come on [chat](https://chat.stackoverflow.com/) and upload both the class file. – Ali Azam Nov 25 '17 at 07:57
  • @Abhijith when you call the method, use the line `WebDriver driver = Login1.getDriver();` instead of `Login1.getDriver();` – Ali Azam Nov 25 '17 at 08:04
  • Hi Ali, i cant come to chat as it is saying i need 20 reputation but i have only 1 – Abhijith Nov 25 '17 at 08:18
  • i call the method as suggested by you only – Abhijith Nov 25 '17 at 08:18
  • Hi Ali, my Skype id is +91 98863 47751 – Abhijith Nov 25 '17 at 08:37
  • Ali, Pls come to skype or provide me your mail id i will send my class files – Abhijith Nov 25 '17 at 08:49
  • @I am in a hurry. so attach the file and I will give you the solution later. – Ali Azam Nov 25 '17 at 08:54
  • Sure ali, i sent aatachments – Abhijith Nov 25 '17 at 08:56
0

The problem is in the below snippet of your code

public InputStream propertiesFilePath = this.getClass().getClassLoader()
    .getResourceAsStream("Config.properties");

    public  void Readproperties() throws IOException {
        props = new Properties();
        props.load(propertiesFilePath);
    }

Java is not finding a resource by the name Config.properties in your CLASSPATH. You would need to check and ensure that this file is available in the CLASSPATH and is readable by Java. Once this is fixed, your NullPointerException should go away.

Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66