org.testng.TestNGException: Cannot instantiate class issue

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

org.testng.TestNGException: Cannot instantiate class issue

sreedevivasudevan91
my program contains 4 class :
import is added but not included below
class1 

package testngsa;

public class NewTest { 
                              String UserName;
public String getUserName(){
return UserName;
}
public void  setUserName( String UserName){
this.UserName=UserName;
}
}
class2:
public class NewTest1 {

public NewTest1() throws Exception {
super();
}
int rows_total;
NewTest dataSheet =null;
List<NewTest> dataSheetList=  new ArrayList<NewTest>();
Row row = null;
XSSFCell cell = null;
public List<NewTest> readDataFromExcel(String filePath, String sheetName)throws Exception{
FileInputStream inputStream= new FileInputStream(filePath);
XSSFWorkbook workbook = new XSSFWorkbook(inputStream); 
XSSFSheet sheet = workbook.getSheet(sheetName);
rows_total= sheet.getLastRowNum();
for(int i=1;i<3;i++){
row = sheet.getRow(i);
dataSheet= new NewTest();
cell=(XSSFCell) row.getCell(1);
dataSheet.setUserName(cell.getStringCellValue());
//cell=(XSSFCell) row.getCell(1);
//dataSheet.setPassword(cell.getStringCellValue());
dataSheetList.add(dataSheet);
}
return dataSheetList;
}
}
class3:
public class NewTest2   extends NewTest1 {
WebDriver driver;
//Actions action = new Actions(driver);
public String baseUrl = " ";
public NewTest2 ()  throws Exception {
super();
}

protected List<NewTest> dataSheetList = readDataFromExcel("C:/Users/name/Desktop/data2.xlsx","data");//name gave my own name
@BeforeTest
//@Test
//@Before
// @BeforeMethod
public void launchbr() {
System.setProperty("webdriver.chrome.driver", "D:\\chrome/chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-extensions");
       WebDriver driver = new ChromeDriver(options);
        driver.get(baseUrl);
   driver.manage().timeouts().implicitlyWait(300, TimeUnit.SECONDS);
 }
}


class4:
public class NewTest3   extends NewTest2 {
protected String GeUserName;
protected String GetPassword;
public NewTest3 () throws Exception{
super();
}
@Test
public void login() throws InterruptedException{
if (dataSheetList.size()==0){
System.out.println(" no data found");
}else{
for(NewTest gData: dataSheetList){
GeUserName = gData.getUserName();
//GetPassword = gData.getPassword();
driver.findElement(By.xpath(" ")).click();
driver.findElement(By.xpath("  ")).sendKeys(GeUserName);
driver.findElement(By.xpath(" ")).click();
driver.quit();
}
}
}
}

tried with driver.quit and driver.close ,@before ,@beforetest,@beforemethod,@test
to verify  testng installed correctly, checked for sample testng code and that was passed successfully.

and my error is :
org.testng.TestNGException: 
Cannot instantiate class testngsa.NewTest3
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:31)
at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:410)
at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:323)
at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:126)
at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:191)
at org.testng.TestClass.getInstances(TestClass.java:99)
at org.testng.TestClass.initTestClassesAndInstances(TestClass.java:85)
at org.testng.TestClass.init(TestClass.java:77)
at org.testng.TestClass.<init>(TestClass.java:42)
at org.testng.TestRunner.initMethods(TestRunner.java:423)
at org.testng.TestRunner.init(TestRunner.java:250)
at org.testng.TestRunner.init(TestRunner.java:220)
at org.testng.TestRunner.<init>(TestRunner.java:169)
at org.testng.remote.support.RemoteTestNG6_10$1.newTestRunner(RemoteTestNG6_10.java:28)
at org.testng.remote.support.RemoteTestNG6_10$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG6_10.java:61)
at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:616)
at org.testng.SuiteRunner.init(SuiteRunner.java:185)
at org.testng.SuiteRunner.<init>(SuiteRunner.java:131)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1383)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1363)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1217)
at org.testng.TestNG.runSuites(TestNG.java:1144)
at org.testng.TestNG.run(TestNG.java:1115)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:23)
... 25 more
Caused by: java.lang.NullPointerException
at testngsa.NewTest1.readDataFromExcel(NewTest1.java:50)
at testngsa.NewTest2.<init>(NewTest2.java:46)
at testngsa.NewTest3.<init>(NewTest3.java:35)
... 30 more

Please help



  
 













--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: org.testng.TestNGException: Cannot instantiate class issue

Krishnan Mahadevan

Looking at the stack trace, I would suggest that you setup a break point in NewTest1.java at line 50 and see what is causing the NullPointerException. Once you fix that you should be good.

Caused by: java.lang.NullPointerException at testngsa.NewTest1.readDataFromExcel(NewTest1.java:50)
at testngsa.NewTest2.<init>(NewTest2.java:46)
at testngsa.NewTest3.<init>(NewTest3.java:35)


On Tue, May 16, 2017, 18:01 <[hidden email]> wrote:
my program contains 4 class :
import is added but not included below
class1 

package testngsa;

public class NewTest { 
                              String UserName;
public String getUserName(){
return UserName;
}
public void  setUserName( String UserName){
this.UserName=UserName;
}
}
class2:
public class NewTest1 {

public NewTest1() throws Exception {
super();
}
int rows_total;
NewTest dataSheet =null;
List<NewTest> dataSheetList=  new ArrayList<NewTest>();
Row row = null;
XSSFCell cell = null;
public List<NewTest> readDataFromExcel(String filePath, String sheetName)throws Exception{
FileInputStream inputStream= new FileInputStream(filePath);
XSSFWorkbook workbook = new XSSFWorkbook(inputStream); 
XSSFSheet sheet = workbook.getSheet(sheetName);
rows_total= sheet.getLastRowNum();
for(int i=1;i<3;i++){
row = sheet.getRow(i);
dataSheet= new NewTest();
cell=(XSSFCell) row.getCell(1);
dataSheet.setUserName(cell.getStringCellValue());
//cell=(XSSFCell) row.getCell(1);
//dataSheet.setPassword(cell.getStringCellValue());
dataSheetList.add(dataSheet);
}
return dataSheetList;
}
}
class3:
public class NewTest2   extends NewTest1 {
WebDriver driver;
//Actions action = new Actions(driver);
public String baseUrl = " ";
public NewTest2 ()  throws Exception {
super();
}

protected List<NewTest> dataSheetList = readDataFromExcel("C:/Users/name/Desktop/data2.xlsx","data");//name gave my own name
@BeforeTest
//@Test
//@Before
// @BeforeMethod
public void launchbr() {
System.setProperty("webdriver.chrome.driver", "D:\\chrome/chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-extensions");
       WebDriver driver = new ChromeDriver(options);
        driver.get(baseUrl);
   driver.manage().timeouts().implicitlyWait(300, TimeUnit.SECONDS);
 }
}


class4:
public class NewTest3   extends NewTest2 {
protected String GeUserName;
protected String GetPassword;
public NewTest3 () throws Exception{
super();
}
@Test
public void login() throws InterruptedException{
if (dataSheetList.size()==0){
System.out.println(" no data found");
}else{
for(NewTest gData: dataSheetList){
GeUserName = gData.getUserName();
//GetPassword = gData.getPassword();
driver.findElement(By.xpath(" ")).click();
driver.findElement(By.xpath("  ")).sendKeys(GeUserName);
driver.findElement(By.xpath(" ")).click();
driver.quit();
}
}
}
}

tried with driver.quit and driver.close ,@before ,@beforetest,@beforemethod,@test
to verify  testng installed correctly, checked for sample testng code and that was passed successfully.

and my error is :
org.testng.TestNGException: 
Cannot instantiate class testngsa.NewTest3
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:31)
at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:410)
at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:323)
at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:126)
at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:191)
at org.testng.TestClass.getInstances(TestClass.java:99)
at org.testng.TestClass.initTestClassesAndInstances(TestClass.java:85)
at org.testng.TestClass.init(TestClass.java:77)
at org.testng.TestClass.<init>(TestClass.java:42)
at org.testng.TestRunner.initMethods(TestRunner.java:423)
at org.testng.TestRunner.init(TestRunner.java:250)
at org.testng.TestRunner.init(TestRunner.java:220)
at org.testng.TestRunner.<init>(TestRunner.java:169)
at org.testng.remote.support.RemoteTestNG6_10$1.newTestRunner(RemoteTestNG6_10.java:28)
at org.testng.remote.support.RemoteTestNG6_10$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG6_10.java:61)
at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:616)
at org.testng.SuiteRunner.init(SuiteRunner.java:185)
at org.testng.SuiteRunner.<init>(SuiteRunner.java:131)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1383)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1363)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1217)
at org.testng.TestNG.runSuites(TestNG.java:1144)
at org.testng.TestNG.run(TestNG.java:1115)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:23)
... 25 more
Caused by: java.lang.NullPointerException
at testngsa.NewTest1.readDataFromExcel(NewTest1.java:50)
at testngsa.NewTest2.<init>(NewTest2.java:46)
at testngsa.NewTest3.<init>(NewTest3.java:35)
... 30 more

Please help



  
 













--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
--

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/

My Technical Scribbings @ http://rationaleemotions.wordpress.com/

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: org.testng.TestNGException: Cannot instantiate class issue

Vimal Raj
In reply to this post by sreedevivasudevan91
Hi,

I see you are getting a NullPointerException which could cause the problem. Refer the following stacktrace (trimmed version of your entire stacktrace)

Caused by: java.lang.NullPointerException
at testngsa.NewTest1.readDataFromExcel(NewTest1.java:50)
at testngsa.NewTest2.<init>(NewTest2.java:46)
at testngsa.NewTest3.<init>(NewTest3.java:35)
... 30 more


I would recommend you to put a break point and debug. Also, it is recommended to initialize the objects in your @Before hooks.

Thanks
Vimalraj


On 16 May 2017 at 11:28, <[hidden email]> wrote:
my program contains 4 class :
import is added but not included below
class1 

package testngsa;

public class NewTest { 
                              String UserName;
public String getUserName(){
return UserName;
}
public void  setUserName( String UserName){
this.UserName=UserName;
}
}
class2:
public class NewTest1 {

public NewTest1() throws Exception {
super();
}
int rows_total;
NewTest dataSheet =null;
List<NewTest> dataSheetList=  new ArrayList<NewTest>();
Row row = null;
XSSFCell cell = null;
public List<NewTest> readDataFromExcel(String filePath, String sheetName)throws Exception{
FileInputStream inputStream= new FileInputStream(filePath);
XSSFWorkbook workbook = new XSSFWorkbook(inputStream); 
XSSFSheet sheet = workbook.getSheet(sheetName);
rows_total= sheet.getLastRowNum();
for(int i=1;i<3;i++){
row = sheet.getRow(i);
dataSheet= new NewTest();
cell=(XSSFCell) row.getCell(1);
dataSheet.setUserName(cell.getStringCellValue());
//cell=(XSSFCell) row.getCell(1);
//dataSheet.setPassword(cell.getStringCellValue());
dataSheetList.add(dataSheet);
}
return dataSheetList;
}
}
class3:
public class NewTest2   extends NewTest1 {
WebDriver driver;
//Actions action = new Actions(driver);
public String baseUrl = " ";
public NewTest2 ()  throws Exception {
super();
}

protected List<NewTest> dataSheetList = readDataFromExcel("C:/Users/name/Desktop/data2.xlsx","data");//name gave my own name
@BeforeTest
//@Test
//@Before
// @BeforeMethod
public void launchbr() {
System.setProperty("webdriver.chrome.driver", "D:\\chrome/chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-extensions");
       WebDriver driver = new ChromeDriver(options);
        driver.get(baseUrl);
   driver.manage().timeouts().implicitlyWait(300, TimeUnit.SECONDS);
 }
}


class4:
public class NewTest3   extends NewTest2 {
protected String GeUserName;
protected String GetPassword;
public NewTest3 () throws Exception{
super();
}
@Test
public void login() throws InterruptedException{
if (dataSheetList.size()==0){
System.out.println(" no data found");
}else{
for(NewTest gData: dataSheetList){
GeUserName = gData.getUserName();
//GetPassword = gData.getPassword();
driver.findElement(By.xpath(" ")).click();
driver.findElement(By.xpath("  ")).sendKeys(GeUserName);
driver.findElement(By.xpath(" ")).click();
driver.quit();
}
}
}
}

tried with driver.quit and driver.close ,@before ,@beforetest,@beforemethod,@test
to verify  testng installed correctly, checked for sample testng code and that was passed successfully.

and my error is :
org.testng.TestNGException: 
Cannot instantiate class testngsa.NewTest3
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:31)
at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:410)
at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:323)
at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:126)
at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:191)
at org.testng.TestClass.getInstances(TestClass.java:99)
at org.testng.TestClass.initTestClassesAndInstances(TestClass.java:85)
at org.testng.TestClass.init(TestClass.java:77)
at org.testng.TestClass.<init>(TestClass.java:42)
at org.testng.TestRunner.initMethods(TestRunner.java:423)
at org.testng.TestRunner.init(TestRunner.java:250)
at org.testng.TestRunner.init(TestRunner.java:220)
at org.testng.TestRunner.<init>(TestRunner.java:169)
at org.testng.remote.support.RemoteTestNG6_10$1.newTestRunner(RemoteTestNG6_10.java:28)
at org.testng.remote.support.RemoteTestNG6_10$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG6_10.java:61)
at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:616)
at org.testng.SuiteRunner.init(SuiteRunner.java:185)
at org.testng.SuiteRunner.<init>(SuiteRunner.java:131)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1383)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1363)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1217)
at org.testng.TestNG.runSuites(TestNG.java:1144)
at org.testng.TestNG.run(TestNG.java:1115)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:23)
... 25 more
Caused by: java.lang.NullPointerException
at testngsa.NewTest1.readDataFromExcel(NewTest1.java:50)
at testngsa.NewTest2.<init>(NewTest2.java:46)
at testngsa.NewTest3.<init>(NewTest3.java:35)
... 30 more

Please help



  
 













--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Loading...