How to call a method from one class to another class when both are TestNG classes up vote

44 views
Skip to first unread message

sachin patil

unread,
Jun 10, 2018, 9:12:32 AM6/10/18
to Selenium Users
Guys, I am facing above problem which i mentioned in the subject line.
Can you guys please me on this?

Thanks
Sachin

Cassian Raja Thomas

unread,
Jun 10, 2018, 1:05:44 PM6/10/18
to seleniu...@googlegroups.com
What do you mean by calling method?? Are you trying to call one @Test method from another @Test method??

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/7f19523f-724f-4040-954c-dfcf3b4a3554%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

sachin patil

unread,
Jun 11, 2018, 1:52:29 AM6/11/18
to seleniu...@googlegroups.com
I have two classes as mentioned below.


public class ClassA {
public void method1()
    {
        System.out.println("Selenium_1");
    }
   public void method2()
    {
        System.out.println("Selenium_2");
    }
    
}
=============================
public class ClassB {

public static void main(String[] args) {
       
 
 System.out.println("executing class A");
 ClassA rt1=new ClassA();
rt1.method1();
rt1.method1();
   
}
 
}

After running the ClassB it's executed properly but when i execute my orginal script which has more code(from real project) i am getting NullPointer exception.

Why this is happening?

Please help me on this.


Best Regard
Sachin Patil
+91-9552619077


On Sun, Jun 10, 2018 at 10:35 PM, Cassian Raja Thomas <rajac...@gmail.com> wrote:
What do you mean by calling method?? Are you trying to call one @Test method from another @Test method??
On Sun, Jun 10, 2018, 6:42 PM sachin patil <sachin....@gmail.com> wrote:
Guys, I am facing above problem which i mentioned in the subject line.
Can you guys please me on this?

Thanks
Sachin

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/CABh6KKsYnxsJGLtc8bv7vLxaZh_Es-%3DoLEB-Q8KBLB4VPJJPsA%40mail.gmail.com.

Cassian Raja Thomas

unread,
Jun 11, 2018, 2:47:02 AM6/11/18
to seleniu...@googlegroups.com
These classes looks good to be executed. But unless you show the actual code, I will not be able to provide any help. Also, try to debug the code using your IDE to find out the line where there is a null reference.

Thanks & Regards,
Cassian Raja .T

To post to this group, send email to seleniu...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.

Sowjanya Karumuri

unread,
Jun 11, 2018, 1:17:27 PM6/11/18
to seleniu...@googlegroups.com

You need to have driver in ur constructor in both classes. Then it will work. 

To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/CAA_T2rGFjCmEZEgKXSXOoOsvridMvc%2BOmA74tzMYgKRsgPxmsg%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.
--
Sowjanya
---------------------
Be the change you wish to see in the world

sachin patil

unread,
Jun 15, 2018, 7:20:27 AM6/15/18
to seleniu...@googlegroups.com
Hello,

I am sharing real code with you as i am getting error called "Exception in thread "main" java.lang.NullPointerException" while method from one class to another class.
Please look into ClassA and ClassB.
Help me with this.

public class ClassA {
public static AndroidDriver driver;
public void launch() throws MalformedURLException
{
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("deviceName", "Honor");
caps.setCapability("platformName", "Android");
caps.setCapability("platformVersion", "7.0");
     caps.setCapability("udid", "192.168.157.101:5555");
caps.setCapability("appPackage", "com.torenzo.torenzocafe");
caps.setCapability("appActivity", "com.torenzo.torenzopos.StartScreenActivity");
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), caps);
}
public void login()
{
driver.findElement(By.id("com.torenzo.torenzocafe:id/submit_login")).click();
driver.findElement(By.id("com.torenzo.torenzocafe:id/clock_in")).click();
driver.findElement(By.id("com.torenzo.torenzocafe:id/role_name")).click();
  System.out.println("on home page");
}


}

==================
public class ClassB {
public static AndroidDriver driver;
public static void main(String[] args) throws MalformedURLException {
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
ClassA call = new ClassA();
call.launch();
call.login();
ClassB call1 = new ClassB();
call1.TakeoutOrder();
}

public void TakeoutOrder(){

System.out.println("Order creation process is started");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.id("com.torenzo.torenzocafe:id/new_order_create_btn_layout")).click();
String order_no=driver.findElement(By.id("com.torenzo.torenzocafe:id/order_no")).getText();
System.out.println("Order number is =>" + order_no);
driver.findElement(By.xpath("//android.widget.TextView[@text='All Items']")).click();
for (int i=3; i<5; i++)
driver.findElement(By.xpath("//android.widget.LinearLayout[contains(@resource-id,'grid_menu_layout') and @index="+i+"]")).click();
}
driver.findElement(By.id("com.torenzo.torenzocafe:id/add_guest_btn")).click();
driver.findElement(By.id("com.torenzo.torenzocafe:id/add_guest_two")).click();
driver.findElement(By.id("com.torenzo.torenzocafe:id/add_guest_done")).click();
for (int g=1; g<3; g++)
{
driver.findElement(By.xpath("//android.widget.LinearLayout[contains(@resource-id,'card_view') and @index="+g+"]")).click();
for (int i=1; i<3; i++)
{
driver.findElement(By.xpath("//android.widget.LinearLayout[contains(@resource-id,'grid_menu_layout') and @index="+i+"]")).click();
}
}
}
}







Best Regard
Sachin Patil
+91-9552619077


To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.
--
Sowjanya
---------------------
Be the change you wish to see in the world

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/CAM%2BNpKBCJGQMCiH5ir4BvsCfxUdMgbHnR8BNeeD6HbztBvm2Xw%40mail.gmail.com.

sachin patil

unread,
Jun 15, 2018, 8:00:28 AM6/15/18
to seleniu...@googlegroups.com
Can you please send modify the code like how it's work for calling a method from one class to another class for TestNG framework and simple java class file as well as i made above class file(automation). 

Best Regard
Sachin Patil
+91-9552619077


To post to this group, send email to seleniu...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
--
Sowjanya
---------------------
Be the change you wish to see in the world

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.

bharathi sriram

unread,
Jun 15, 2018, 2:26:32 PM6/15/18
to seleniu...@googlegroups.com
By importing classes and instantiating.




--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.



--
Thanks and Best Regards,
Bharathi

sachin patil

unread,
Jun 17, 2018, 3:22:05 AM6/17/18
to seleniu...@googlegroups.com
Can you please give example?

On Fri, 15 Jun 2018, 11:56 pm bharathi sriram, <bsh...@gmail.com> wrote:
By importing classes and instantiating.



On Sun, Jun 10, 2018 at 6:09 AM, sachin patil <sachin....@gmail.com> wrote:
Guys, I am facing above problem which i mentioned in the subject line.
Can you guys please me on this?

Thanks
Sachin

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
--
Thanks and Best Regards,
Bharathi

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/CAEwmRzhz%3D_QcYeBy%3DJKPwQ3Cvcu2yxs3NmkoyurbsO%3DEUERmRg%40mail.gmail.com.

Scott Babcock

unread,
Jun 17, 2018, 8:44:53 PM6/17/18
to Selenium Users
In class B, I don't see any code that initializes the [driver] field. You just launch straight into trying to use whatever might be stored there. Since this field is uninitialized, you get NullPointerException.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
--
Sowjanya
---------------------
Be the change you wish to see in the world

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.

sachin patil

unread,
Jun 18, 2018, 12:35:20 AM6/18/18
to seleniu...@googlegroups.com
Here you can see...
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/61f75afb-6d8c-472d-95b4-6d5ce1f89c4f%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages