Getting Null pointer expections " java.lang.NullPointerException "



  • [RemoteTestNG] detected TestNG version 6.14.3 1553664942228 mozrunner::runner INFO Running command: "C:\Program Files\Mozilla Firefox\firefox.exe" "-marionette" "-foreground" "-no-remote" "-profile" "C:\Users\Admin\AppData\Local\Temp\rust_mozprofile.itROC1fctTdz" 1553664942542 addons.webextension.screenshots@mozilla.org WARN Loading extension 'screenshots@mozilla.org': Reading manifest: Invalid extension permission: mozillaAddons 1553664942542 addons.webextension.screenshots@mozilla.org WARN Loading extension 'screenshots@mozilla.org': Reading manifest: Invalid extension permission: resource://pdf.js/ 1553664942542 addons.webextension.screenshots@mozilla.org WARN Loading extension 'screenshots@mozilla.org': Reading manifest: Invalid extension permission: about:reader* 1553664943661 Marionette INFO Listening on port 53094 1553664943965 Marionette WARN TLS certificate errors will be ignored for this session Mar 27, 2019 11:05:44 AM org.openqa.selenium.remote.ProtocolHandshake createSession INFO: Detected dialect: W3C CTS Logged in SELECT REPORTS Date entered successfully Successfully Execute the Reports Back to the Reports FAILED: Reports java.lang.NullPointerException at ctsreports.Timesheetbyday.dailytimesheet(Timesheetbyday.java:15) at ctsreports.Initial.Reports(Initial.java:31) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)


    First Class file

    package ctsreports;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.testng.annotations.Test;
    
    public class Timesheetbyweek {
    
        WebDriver driver;
    
    
        @Test (priority=1)
        public void Logincts() throws InterruptedException {
    
            //DRIVER INITIALIZATION
            System.setProperty("webdriver.gecko.driver", "E://Automation Backup file//Selenium//Firefox//geckodriver.exe");
            driver = new FirefoxDriver();
    
            // OPENED THE REQUIRED URL  
            driver.get("http://localhost:5740/#/Login/");
            driver.manage().window().maximize();
    
            // LOGIN WITH VALID CREDENTIALS
            driver.findElement(By.id("emailBox")).sendKeys("sathish@cybertree.com.au");
            driver.findElement(By.id("loginStaff")).click();
            driver.findElement(By.id("pwdBox")).sendKeys("Cyber12345");
            driver.findElement(By.id("loginStaff")).click();
            Thread.sleep(10000);
            System.out.println("CTS Logged in");
        }
    
            @Test (priority=2)
            public void reportsbyweek() throws InterruptedException {
    
    
                // LOGIN WITH VALID CREDENTIALS
                driver.findElement(By.xpath("//*[@id=\"navigation\"]/li[12]/a/span")).click();    
                Thread.sleep(1000);
                System.out.println("SELECT REPORTS");
                //driver.findElement(By.cssSelector("div.row:nth-child(2) > div:nth-child(1) > div:nth-child(2) > label:nth-child(1) > a:nth-child(2) > span:nth-child(1)")).click();
                driver.findElement(By.xpath("/html/body/div[3]/ng-view/div/div[2]/form/div[2]/div[1]/div[2]/label/a/span")).click();
                Thread.sleep(1000);
    
                  driver.findElement(By.id("searchPopup")).clear();
                  driver.findElement(By.id("searchPopup")).sendKeys("25-03-2019");  
    
                  driver.findElement(By.id("searchPopup1")).clear();
                  driver.findElement(By.id("searchPopup1")).sendKeys("31-03-2019");
    
                  System.out.println("Date entered successfully");
    
                  driver.findElement(By.xpath(" //*[@id=\"filterForm\"]/div[4]/button")).click();
                  System.out.println("Successfully Execute the Reports");
                  Thread.sleep(10000);
    
    
    
    
            }
    
        /*
         * @Test (priority=3) public void export() throws InterruptedException {
         * 
         * driver.findElement(By.xpath("//*[@id=\"filterForm\"]/div[5]/button[1]")).
         * click(); Thread.sleep(5000);
         * driver.findElement(By.xpath("//*[@id=\"filterForm\"]/div[5]/button[2]")).
         * click();
         * 
         * System.out.println("Export timesheet by week");
         * 
         * 
         * 
         * }
         */
    
    
    
    }
    

    Second Class file

    package ctsreports;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.testng.annotations.Test;
    
    public class Timesheetbyday {
    
        WebDriver driver;
    
        @Test (priority=4)
        public void dailytimesheet() throws InterruptedException {
                Thread.sleep(10000);
    
                System.out.println("Back to the Reports");
                driver.findElement(By.xpath("//*[@id=\"navigation\"]/li[12]/a/span")).click();
    
                System.out.println("Choose Timesheetbyday");
                driver.findElement(By.xpath("/html/body/div[3]/ng-view/div/div[2]/form/div[2]/div[1]/div[3]/label/a/span")).click();   
                Thread.sleep(1000);
    
                  driver.findElement(By.id("searchPopup")).clear();
                  driver.findElement(By.id("searchPopup")).sendKeys("25-03-2019");  
                  System.out.println("First date");
                  driver.findElement(By.id("searchPopup1")).clear();
                  driver.findElement(By.id("searchPopup1")).sendKeys("31-03-2019");
                  System.out.println("Second date");
    
                  driver.findElement(By.xpath(" //*[@id=\"filterForm\"]/div[4]/button")).click();
                  System.out.println("Successfully Execute the Reports");                     
    
            }
    
        /*
         * @Test (priority=5) public void exportdailytimesheet() throws
         * InterruptedException {
         * 
         * driver.findElement(By.xpath("//*[@id=\"filterForm\"]/div[5]/button[1]")).
         * click(); Thread.sleep(5000);
         * driver.findElement(By.xpath("//*[@id=\"filterForm\"]/div[5]/button[2]")).
         * click();
         * 
         * 
         * 
         * }
         */
    }
    

    Main File

    package ctsreports;
    import ctsreports.Timesheetbyweek;
    import ctsreports.Timesheetbyday;
    
    import org.testng.annotations.Test;
    
    public class Initial 
        {
    
        Timesheetbyweek timesheetweek = new Timesheetbyweek();
        Timesheetbyday timesheet = new Timesheetbyday();
    
        public static void mail (String args[]) throws InterruptedException
        {
            Timesheetbyweek timesheetweek = new Timesheetbyweek();
            Timesheetbyday timesheet = new Timesheetbyday();
            timesheetweek.Logincts();
            timesheetweek.reportsbyweek();
        //  timesheetweek.export();
            timesheet.dailytimesheet();
            //timesheet.exportdailytimesheet();
        }
    
    
    
    
      @Test
      public void Reports() throws InterruptedException {
    
          timesheetweek.Logincts();
          timesheetweek.reportsbyweek();
         // timesheetweek.export();
          timesheet.dailytimesheet();
        //  timesheet.exportdailytimesheet();
    
          System.out.println("Reports Executed successfully");
      }
    
    
    
    
    
    }
    


  • You are using Timesheetbyday which is in a different class, but I can't see that you are passing in the active Driver instance when calling the method.

    i.e Timesheetbyweek works as it initialises the Driver instance then uses it, but Timesheetbyday doesn't get passed the driver instance and instead will use the driver for it's own class definition (which is NULL). I'd also expect to see the Timesheetbyweek tests fail if you change the order so that Logincts() isn't first (as then the browser instance wouldn't exist for the other tests until that one runs).

    Also try not to use Thread.sleep(1000). It will seriously slow down the test running. Instead have a look at implicit / explicit waits e.g.

    wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.visibilityOf(webElement));
    wait.until(ExpectedConditions.elementToBeClickable(webElement));
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2