0

I have page objects classes which I used for test scripts. I usually assign these classes sessions to variables as I need to use them in multiple places. I'm getting null exception in some test scripts while running the code. The same script sometime getting passed without showing an error too. Why does session variable throwing a null exception?

TestCommonLandingPage.java

public class TestCommonLandingPage extends PageObject{
   public TestCommonLandingPage(Session session) {
        super(session);        
    }

   public TestCommonLandingPage stepOpenWindowByShortcut() throws FrameworkException {
        this.test_step_initiation();
        this.TestCommonLandingPagePanel.action_perform_shortcuts_window();
        log.info("Step: OpenWindowByShortcut");
        return this;
    }
}

TestScript.java

public class TC1614 extends VerificationsWidget {
    public void viewingWidgetParameters() throws FrameworkException, InterruptedException {
        final TestCommonLandingPage testCommonLandingPage = new TestCommonLandingPage (getSession()); 

testCommonLandingPage
                .stepOpenWindowByShortcut());

The code("testCommonLandingPage.stepOpenWindowByShortcut());") most of the time throws a null exception, why is that? When I'm using session without assign to a variable this is working properly as well

Laurel
  • 5,965
  • 14
  • 31
  • 57
user3806999
  • 39
  • 4
  • 14
  • Your are not showing enough of your code to understand what is happening, i think. Could you add some more? – Pao Sep 21 '22 at 14:29
  • can you share that NullPointerException? That line itself could only throw if `testCommonLandingPage` was null which in the code you posted can't happen. It can happen inside `stepOpenWindowByShortcut` if something is not right with the Page – zapl Sep 21 '22 at 21:08
  • java.lang.NullPointerException this is the error throwing from testCommonLandingPage.stepOpenWindowByShortcut()); if im using same code snippet without assign it to a variable then no null exception throws; new TestCommonLandingPage (getSession()).stepOpenWindowByShortcut()); - This works fine I wanted to know why this is happening – user3806999 Sep 22 '22 at 03:32
  • Does this answer your question? [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) – knittl Sep 30 '22 at 06:16

1 Answers1

0

I was declared all the variables at the beginning of the class. This happened when user switch between windows(according to the script steps). When directing session was not set to the variable as there was some delay all the time. If you face same problem as this, check whether there is any loading delay between system windows and declare the variables once it is fully loaded.

user3806999
  • 39
  • 4
  • 14
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 04 '22 at 07:19