1

I have created 2 classes in 2 packages under one project and calling one method of class A into class B by creating object of class but it is giving me null pointer exception below is the code can any one help on this:


package Login;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import Repository.Objects;

// import Repository.Objects;

public class Loginasa {


    public static  WebDriver driver;
     String baseUrl;

     @BeforeSuite
     public void setUp() throws Exception 
     {
        System.setProperty("webdriver.chrome.driver","E:\\chromedriver_win32\\chromedriver.exe");
        driver =new ChromeDriver();
        baseUrl= "http://10.7.30.135:9090/xGLinear/login.html";
        driver.get(baseUrl);
        driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
     }



    @Test 
     public void Newlogin() throws Exception 
     {
     // driver.get(baseUrl);

    driver.findElement(By.id(Objects.Username)).clear();
    driver.findElement(By.id(Objects.Username)).sendKeys("ASA");
    driver.findElement(By.id(Objects.Password)).clear();
    driver.findElement(By.id(Objects.Password)).sendKeys("Abcd1234");
    driver.findElement(By.id(Objects.loginbtn)).click();
    Thread.sleep(20000);
     }

    @AfterTest
      public void tearDown() throws Exception 
     {
        driver.quit();
        System.out.println("Browser Launch");
   }





}

Another is:

package Commonmethods;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import Login.Loginasa;

public class CreateOrderline  {

    public static  WebDriver driver;
    String baseUrl;

     @BeforeTest
     public void setUp() throws Exception 
     {
        System.setProperty("webdriver.chrome.driver","E:\\chromedriver_win32\\chromedriver.exe");
        driver =new ChromeDriver();
        baseUrl= "http://10.7.30.135:9090/xGLinear/login.html";
        driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);


     }

    @Test 
     public void Orderline () throws Exception 
     {
        driver.get(baseUrl);
        Loginasa ASA= new Loginasa();
        System.out.println("Browser Launch");
         ASA.Newlogin();

     }

         @AfterTest
          public void tearDown() throws Exception 
         {
            driver.quit();
            System.out.println("Browser Launch");
       }
     }

Giving Error: NULL Pointer Exception............

5
  • Paste your error also
    – QAMember
    Commented Feb 23, 2017 at 6:12
  • Please provide your error log or stacktrace so we can identify error line.
    – Sagar007
    Commented Feb 23, 2017 at 6:58
  • Unclear what you are asking, Please add some more details about your question and queries. Commented Feb 23, 2017 at 7:18
  • @Rashmi Could you please provide the code which you write in test-ng file. because as per my understanding your webdriver session get closed that why you get the null pointer execution
    – Ankit jain
    Commented Feb 23, 2017 at 21:50
  • What IDE are you using to build your tests? I would like to suggest better ways to debug nullpointer exceptions like this. Commented Feb 27, 2017 at 13:06

3 Answers 3

1

I'm not sure it's good design to call one test from another fixture. Had problems with that in the past. You might consider using Page Objects

Anyhow, in your case it looks like you have a NullPointerException because ASA.setUp() isn't called before calling ASA.NewLogin() when the setup method of the referring class is called.

Though it's C# you can find a working proposal of how to organize your test code here.

2
  • 2
    The problem is that the setup method from Loginasa won't be called automatically from the CreateOrderOnline instance as it's another test suite Maybe if CreateOrderOnline inherited from Loginasa the setup method would be visible to the test runner from a CreateOrderOnline instance.
    – user1130
    Commented Feb 23, 2017 at 7:21
  • 1
    @BharatMane user1130 is right, the setUp() is not called for instantiated class in your tests. So when you use the "new" keyword the setUp() is not called. Commented Feb 27, 2017 at 13:03
0

No need to use two instances of browser. Just specify one instance and you can use the same instance in a project. You can call the browser object by specifying the classname.objectname. ie,

CreateOrderline.driver.findElement(By.id(Objects.Username)).clear();
CreateOrderline.driver.findElement(By.id(Objects.Username)).sendKeys("ASA");
0

You could pass the driver when creating the Loginasa instance:

Loginasa ASA= new Loginasa(driver);

Then you also need to add a constructor to the Loginasa class to pass the driver:

public class Loginasa {
  public static  WebDriver driver;
  public Loginasa(passedDriver) {
    driver = passedDriver;
  }
}

Now can you remove the setUp() function from the Loginasa class, this one is useless anyways cause it is not called by TestNG for that test. TestNG only calls the setUp() of classes for which it runs the test, not classes used by the test. This is also the reason you get a NullPointer exception, the class has an empty driver property when you call ASA.Newlogin(). Also remove all the other annotations from Loginasa. You shouldn't be using a test-class as a class you are going to instantiate from within another test.

I would have a look at the Page Object Model for TestNG: http://www.ontestautomation.com/using-the-page-object-model-pattern-in-selenium-testng-tests/

Also have a look at how you can start debugging NullPointer exceptions yourself:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.