title: Passing the single instance of the driver object to all other classes (Testng framework)
body: <p>I have a driver object initialized in class sample.I want to pass the driver object to other classes also but i get a null pointer exception. My code is</p>
  
  <p><strong>sample class</strong></p>
  
  <pre><code>    public class sample {
  
      WebDriver driver ;
  
  
      @Test(priority=1)
  
      public void openbrowser(){
  
  
          System.setProperty("webdriver.chrome.driver",
                  "/home/ss4u/Desktop/Vignesh/jars/chromedriver");
  
          driver = new ChromeDriver();
  
          driver.get("http://www.google.com");
  
          System.out.println(driver instanceof WebDriver);
  
  
      }
     @Test(priority=2)
     public void maximize(){
  
        driver.manage().window().maximize();
  
     }
     @Test(priority=3)
     public void transfer_instance(){
  
         sampleone obj=new sampleone(driver);
  
  
     }
  
  }
  </code></pre>
  
  <p><strong>sampleclassone</strong></p>
  
  <pre><code>public class sampleone {
  
      WebDriver driver;
  
      public sampleone(WebDriver driver){
  
          this.driver=driver;
  
          System.out.println(driver instanceof WebDriver);
  
          System.out.println(this.driver instanceof WebDriver);
  
          System.out.println("constructor2");
  
  
      }
  
    public sampleone(){
  
          System.out.println("Default constructor called");
  
  
      }
  
  
      @Test(priority=1)
  
       public void gettitle(){
  
            System.out.println(this.driver instanceof WebDriver);
  
            System.out.println(driver instanceof WebDriver);
  
            String title=this.driver.getTitle();
  
            System.out.println(this.driver instanceof WebDriver);
  
            System.out.println(title);
  
            Assert.assertEquals(title, "Google");
  
          }
  
      @Test(priority=2)
  
      public void navigate(){
  
          this.driver.get("https:in.yahoo.com");
  
      }
  
  }
  </code></pre>
  
  <p><strong>Testng xml file</strong> </p>
  
  <pre><code>&lt;!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" &gt;
  
  &lt;suite name="TestNG" verbose="1" &gt;
  
      &lt;test name="sample test"&gt;
      &lt;classes&gt;
        &lt;class name="testsample.sample" /&gt;
      &lt;/classes&gt;
      &lt;/test&gt;
  
      &lt;test name="sample testone"&gt;
      &lt;classes&gt;
        &lt;class name="testsample.sampleone" /&gt;
      &lt;/classes&gt;
      &lt;/test&gt;
  
  &lt;/suite&gt;
  </code></pre>
  
  <p>This issue occurs as iam calling the class not using the object created but using the testng.xml file is there any possible way to create a new java instance(common for all classes) or use the existing instance in all classes</p>