1

We have implemented the Selenium Grid (Distributed Test) Concept with our existing framework, while implemented its produce an Null Pointer Exception as of now i have an single package, with three class file (Baseclass, Loginclass, testcase)

Baseclass - getting my desired driver and navigate to application

class Baseclass {
    public WebDriver myDriver;
    public static String baseUrl;

    // Explicit Constructors
    public Baseclass() {
        baseUrl = "https://example.com/";
    }
    public void Navigate(String url) {
        String navigateToThisUrl = baseUrl + url;       
        myDriver.navigate().to(navigateToThisUrl);
    }
    public void GetDriver() throws MalformedURLException {
        threadDriver = new ThreadLocal<RemoteWebDriver>();
        DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
        FirefoxProfile fp = new FirefoxProfile();
        desiredCapabilities.setCapability(FirefoxDriver.PROFILE, fp);
        desiredCapabilities.setBrowserName(DesiredCapabilities.firefox()
                .getBrowserName());
        myDriver = new RemoteWebDriver(new URL(
                "http://localhost:5555/wd/hub"), desiredCapabilities);
    }
    }

and in Loginclass navigate to the location (either QA or UAT etc.,)

public class Loginclass extends Baseclass {

    public Loginclass navigateToLogin() {
        Navigate("qa");
        return new Loginclass();
    }
    }

Testcase we have an test case and executed the same

public class TestcaseSearch extends Loginclass {
    @BeforeTest
    public final void Startup() throws MalformedURLException {
        Baseclass baseClass = new Baseclass();
        baseClass.GetDriver;        
        }       
    @Test
    public void fieldsSearch(String username, String password)
            throws Exception {      
        Loginclass loginClass = new Loginclass();
        navigateToLogin();
        }
        }

While Execute the above its produce an Null Pointer Exception

Baseclass: idsDriver.navigate().to(navigateToThisUrl);

Loginclass :Navigate("qa");

Let me know how can i rectify this

Exception output

java.lang.NullPointerException
    at com.Baseclass.Navigate(Baseclass.java:11)
    at com.Loginclass.Navigate(Loginclass.java:1)
    at com.TestcaseSearch.Navigate(TestcaseSearch.java:1)
    at com.Loginclass.navigateToLogin(Loginclass.java:4)
    at com.TestcaseSearch.fieldsSearch(TestcaseSearch.java:11)
Prabu
  • 3,550
  • 9
  • 44
  • 85
  • 1
    Can you show where the NPE occurs, and include a full stack trace? – Andy Turner Sep 29 '15 at 12:35
  • Don't set static fields in constructors: you are resetting `baseUrl` every time you create an instance. – Andy Turner Sep 29 '15 at 12:37
  • Why are you creating a `new Baseclass` inside `TestcaseSearch`, a subclass of `Baseclass`? – Andy Turner Sep 29 '15 at 12:40
  • @Andy stack trace java.lang.NullPointerException at com.Baseclass.Navigate(Baseclass.java:11) at com.Loginclass.Navigate(Loginclass.java:1) at com.TestcaseSearch.Navigate(TestcaseSearch.java:1) at com.Loginclass.navigateToLogin(Loginclass.java:4) at com.TestcaseSearch.FieldsSearch(TestcaseSearch.java:11) – Prabu Sep 29 '15 at 12:42
  • please put that in your question, where you can format it legibly. – Andy Turner Sep 29 '15 at 12:42
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Andy Turner Sep 29 '15 at 12:43

1 Answers1

1

Your myDriver instance is null because you create a new instance of Baseclass in Startup(), which sets myDriver on itself rather than the actual test class instance.

Similarly, you don't need to create a new Loginclass in fieldsSearch, because TestcaseSearch is a subclass of Loginclass:

public class TestcaseSearch extends Loginclass {
    @BeforeTest
    public final void Startup() throws MalformedURLException {
        GetDriver();        
    }       

    @Test
    public void fieldsSearch(String username, String password)
            throws Exception {      
        navigateToLogin();
    }
}
Andy Turner
  • 137,514
  • 11
  • 162
  • 243