testng - Java: how to pass and use this array in another class -


i've got csv reader class , i've got user creator class. want user creator class take array, generated csv reader , assign data variables, im getting nullpointerexception

here csv reader class:

public class csvdata {      private static final string file_path="c:\\250.csv";      @test     public static void main() throws exception {          csvreader reader = new csvreader(new filereader(file_path));         arraylist<arraylist<string>> array = new arraylist<arraylist<string>>();         string[] nextline;         while ((nextline = reader.readnext()) != null) {             arraylist<string> list = new arraylist<string>();             (int i=0;i<5;i++) { //5 number of sheets                list.add(nextline[i]);             }             array.add(list);          }          /*for(int x=0;x<array.size();x++) {             for(int y=0;y<array.get(x).size();y++) {                       }         }*/         apptest3 instance = new apptest3();         instance.settingvariables(array);         reader.close();         }     } 

and here user creator class

public class apptest3 extends appdata (which extends csvdata) {      private string[] firstname;     private string[] lastname;      public void settingvariables(arraylist<arraylist<string>> array) {         int randomuser1 = randomizer (1, 250);         int randomuser2 = randomizer (1, 250);         string firstname1 = array.get(randomuser1).get(0);         string firstname2 = array.get(randomuser2).get(0);         string lastname1 = array.get(randomuser1).get(1);         string lastname2 = array.get(randomuser2).get(1) ;         //firstname = { firstname1, firstname2 }; //this doesnt work, dunno why         //lastname = { lastname1, lastname2 };         firstname[0] = firstname1.replace(" ", "");         firstname[1] = firstname2.replace(" ", "");         lastname[0] = lastname1.replace(" ", "");         lastname[1] = lastname2.replace(" ", "");     } @parameters({ "driver", "wait" }) @test(dataprovider = "dataprovider") public void oneusertwouser(webdriver driver, webdriverwait wait)         throws exception {         // user signs up, signs out, second user signs         (int y = 0; y < 2; y++) {             string email = firstname[y].tolowercase() + randomnumber + "@"                     + lastname[y].tolowercase() + emailsuffix;     //test here     } } 

this error message

failed: oneusertwouser(org.openqa.selenium.support.events.eventfiringwebdriver@5efed246, org.openqa.selenium.support.ui.webdriverwait@2b9f2263) java.lang.nullpointerexception     @ com.pragmaticqa.tests.apptest3.oneusertwouser(apptest3.java:57)     @ sun.reflect.nativemethodaccessorimpl.invoke0(native method)     @ sun.reflect.nativemethodaccessorimpl.invoke(unknown source)     @ sun.reflect.delegatingmethodaccessorimpl.invoke(unknown source)     @ java.lang.reflect.method.invoke(unknown source) 

ps: system.out.println(firstname[0]); doesnt display in console. system.out.println(array); displays list of arrays.

edit: found out problem was: first of changed way initialize string[] this

string[] firstname = new string[2]; string[] lastname = new string[2]; 

now firstname[0] returns value.

however, when try system.out.println firstname[0] in next method contains test case, returns null.

so have find way pass strings method.

in main method instance local variable lost after method execution ends.:

 apptest3 instance = new apptest3();  // local variable lost after method execution ends.  instance.settingvariables(array); 

thus, oneusertwouser invoked on instance has no parameters set. can see debugger.

you can put initialization method before below apptest3 class:

public class apptest3 {    apptest3 instance; // field used tests    @beforesuite(alwaysrun = true)    public void before() throws exception {      csvreader reader = new csvreader(new filereader(file_path));     arraylist<arraylist<string>> array = new arraylist<arraylist<string>>();     string[] nextline;     while ((nextline = reader.readnext()) != null) {         arraylist<string> list = new arraylist<string>();         (int i=0;i<5;i++) { //5 number of sheets            list.add(nextline[i]);         }         array.add(list);      }      instance = new apptest3();  /// init instance here     instance.settingvariables(array);     reader.close();     }   } 

Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -