Getting java.lang.NullPointerException when creating the result Excel file using Java in Webdriver -
getting java.lang.nullpointerexception when creating result excel file using java in webdriver.
first row in created results excel file created , 2nd row giving "java.lang.nullpointerexception" @ workbook.write() ie. wwb.write() in code.
please find code have written , error received.
public class accprofile_zipfield_validations1 extends sitesecurity_fieldvalidations { string msg = null; // creating new account profile @test public void accountprofile_city_validations() throws ioexception, biffexception, interruptedexception, rowsexceededexception, writeexception //public static void main(string args[]) throws ioexception, biffexception, interruptedexception, rowsexceededexception, writeexception { // creating properties files storing id's , xpaths application_logs.debug("starting test suite"); application_logs.debug("loading config files"); config = new properties(); //fileinputstream fp = new fileinputstream("./config.properties"); fileinputstream fp = new fileinputstream(system.getproperty("user.dir")+"\\src\\com\\ode\\utility\\config.properties"); config.load(fp); application_logs.debug("loading object xpaths"); or = new properties(); //fileinputstream fp1 = new fileinputstream("./or.properties"); fileinputstream fp1 = new fileinputstream(system.getproperty("user.dir")+"\\src\\com\\ode\\utility\\or.properties"); or.load(fp1); //sleep times long admin_edit = long.valueof(config.getproperty("config_edit")); long admin_zipcode = long.valueof(config.getproperty("config_zipcode")); long admin_cityfield = long.valueof(config.getproperty("config_cityfield")); long admin_screenshot = long.valueof(config.getproperty("config_screenshot")); long admin_checkbox = long.valueof(config.getproperty("config_checkbox")); long admin_save = long.valueof(config.getproperty("config_save")); //take data input xls file //fileinputstream fi =new fileinputstream("./credit_admin_login.xls"); fileinputstream fi =new fileinputstream(system.getproperty("user.dir")+"\\src\\com\\ode\\resources\\credit_adminuser_login.xls"); workbook wb = workbook.getworkbook(fi); sheet sh = wb.getsheet("credit_adminuserlogin"); //take data input xls file //fileinputstream fi =new fileinputstream("./credit_accprofile_fv.xls"); fileinputstream fi1 =new fileinputstream(system.getproperty("user.dir")+"\\src\\com\\ode\\resources\\credit_accprofile_fv.xls"); workbook wb1 = workbook.getworkbook(fi1); sheet sh1 = wb1.getsheet("700credit_fieldvalidations_city"); //create result xls file fileoutputstream fo = new fileoutputstream("./700credit_accprofile_fv_"+cdate+".xls"); writableworkbook wwb = workbook.createworkbook(fo); writablesheet ws = wwb.createsheet("700credit_fv_city_results", 0); system.out.println(ws.getname()); thread.sleep(admin_screenshot); system.out.println(" ******** city field validations ******** "); system.out.println(); system.out.println("random number generated: "+randnum); system.out.println("name account creation: "+accname+"_city_"+randnum); system.out.println("current date generated: "+cdate); application_logs.debug("starting driver"); driver = new internetexplorerdriver(); driver.get(config.getproperty("testwebsite")); driver.manage().window().maximize(); driver.manage().timeouts().implicitlywait(30, timeunit.seconds); (int = 1; < sh.getrows(); i++) { driver.findelement(by.id(or.getproperty("signin_username_id"))).clear(); driver.findelement(by.id(or.getproperty("signin_username_id"))).sendkeys(sh.getcell(0, i).getcontents()); driver.findelement(by.id(or.getproperty("signin_password_id"))).clear(); driver.findelement(by.id(or.getproperty("signin_password_id"))).sendkeys(sh.getcell(1, i).getcontents()); driver.findelement(by.id(or.getproperty("signin_login_id"))).click(); driver.manage().timeouts().implicitlywait(40, timeunit.seconds); system.out.println("logged in site admin user"); //click on add new button driver.findelement(by.name(or.getproperty("addnew_accprof_id"))).click(); thread.sleep(admin_edit); system.out.println("clicked on add button creating new account"); //entering data in account information section driver.findelement(by.name(or.getproperty("accname_id"))).clear(); driver.findelement(by.name(or.getproperty("accname_id"))).sendkeys(accname+"_city_"+randnum); driver.findelement(by.name(or.getproperty("accdba_id"))).sendkeys("kumar_dba"); string acctaxid = or.getproperty("acctax_id"); webelement acctaxinput = driver.findelement(by.id(acctaxid)); acctaxinput.clear(); acctaxinput.sendkeys(string.valueof(12345)); acctaxinput = driver.findelement(by.id(acctaxid)); driver.findelement(by.name(or.getproperty("accaddress_id"))).sendkeys("124 - city cross roads"); string acczipid = or.getproperty("acczip_id"); webelement acczipinput = driver.findelement(by.id(acczipid)); acczipinput.clear(); acczipinput.sendkeys(string.valueof(79081)); acczipinput = driver.findelement(by.id(acczipid)); new actions(driver).sendkeys(driver.findelement(by.id(or.getproperty("acczip_id"))), "").perform(); thread.sleep(admin_zipcode); //entering data in main contact information section driver.findelement(by.name(or.getproperty("accfname_id"))).sendkeys("peter"); thread.sleep(admin_cityfield); (int j=1; j<sh1.getrows(); j++) { system.out.println(); system.out.println("test scenario - "+sh1.getcell(3, j).getcontents()); if(j==1) { //sending numeric values city field. driver.findelement(by.id(or.getproperty("acccity_id"))).clear(); driver.findelement(by.id(or.getproperty("acccity_id"))).sendkeys(sh1.getcell(1, j).getcontents()); thread.sleep(admin_zipcode); //switch driver context alert alert alertdialog = driver.switchto().alert(); //get alert text string alerttext = alertdialog.gettext(); system.out.println("message displayed on popup window when entered "+sh1.getcell(5, j).getcontents()+" in city field: "+alerttext); //click ok button on alert. alertdialog.accept(); msg = alerttext; //taking screen shot of application file scrfile = ((takesscreenshot)driver).getscreenshotas(outputtype.file); fileutils.copyfile(scrfile, new file("./screenshots\\admin_accprofile_field_level_validations_"+cdate+"\\cityfield\\admin_accountprofile_"+sh1.getcell(5, j).getcontents()+" in city field.jpg")); thread.sleep(admin_checkbox); } else if (j==2) { driver.findelement(by.name(or.getproperty("accfname_id"))).sendkeys("peter"); driver.findelement(by.name(or.getproperty("accmname_id"))).sendkeys("s"); driver.findelement(by.name(or.getproperty("acclname_id"))).sendkeys("mark"); driver.findelement(by.id(or.getproperty("save_id"))).click(); thread.sleep(admin_cityfield); alert alertdialog = driver.switchto().alert(); //get alert text string alerttext = alertdialog.gettext(); system.out.println("message displayed on popup window when city field left blank: "+alerttext); //click ok button on alert. alertdialog.accept(); msg = alerttext; //taking screen shot of application file scrfile = ((takesscreenshot)driver).getscreenshotas(outputtype.file); fileutils.copyfile(scrfile, new file("./screenshots\\admin_accprofile_field_level_validations_"+cdate+"\\cityfield\\admin_accountprofile_with_cityfield_blank.jpg")); thread.sleep(admin_zipcode); driver.findelement(by.id(or.getproperty("save_id"))).click(); thread.sleep(admin_zipcode); alertdialog = driver.switchto().alert(); //get alert text alerttext = alertdialog.gettext(); //click ok button on alert. alertdialog.accept(); //driver.findelement(by.id(or.getproperty("acccity_id"))).sendkeys("general electric"); driver.findelement(by.id(or.getproperty("acccity_id"))).sendkeys(sh1.getcell(1, j).getcontents()); //taking screen shot of application scrfile = ((takesscreenshot)driver).getscreenshotas(outputtype.file); fileutils.copyfile(scrfile, new file("./screenshots\\admin_accprofile_field_level_validations_"+cdate+"\\cityfield\\admin_accountprofile_with_cityfield_other city name.jpg")); thread.sleep(admin_screenshot); string accoffnoid = or.getproperty("accoffno_id"); webelement accoffnoinput = driver.findelement(by.id(accoffnoid)); accoffnoinput.clear(); accoffnoinput.sendkeys(string.valueof(9874651230l)); accoffnoinput = driver.findelement(by.id(accoffnoid)); string acccellnoid = or.getproperty("acccellno_id"); webelement acccellnoinput = driver.findelement(by.id(acccellnoid)); acccellnoinput.clear(); acccellnoinput.sendkeys(string.valueof(9632587110l)); acccellnoinput = driver.findelement(by.id(acccellnoid)); string accfaxnoid = or.getproperty("accfaxno_id"); webelement accfaxnoinput = driver.findelement(by.id(accfaxnoid)); accfaxnoinput.clear(); accfaxnoinput.sendkeys(string.valueof(9512368741l)); accfaxnoinput = driver.findelement(by.id(accfaxnoid)); driver.findelement(by.name(or.getproperty("accemail_id"))).sendkeys("abc@abc.com"); driver.findelement(by.id(or.getproperty("txtnad_id"))).sendkeys("le0472"); system.out.println("entered required information creating account"); driver.findelement(by.id(or.getproperty("save_id"))).click(); thread.sleep(admin_save); system.out.println("successfully created account: "+accname+"_city_"+randnum); //taking screen shot of application scrfile = ((takesscreenshot)driver).getscreenshotas(outputtype.file); fileutils.copyfile(scrfile, new file("./screenshots\\admin_accprofile_field_level_validations_"+cdate+"\\cityfield\\admin_newaccountcreated.jpg")); thread.sleep(admin_screenshot); system.out.println(); driver.close(); } (int k = 0; k < sh1.getcolumns(); k++) { label lab=new label(k,j,sh1.getcell(k, j).getcontents()); ws.addcell(lab); label result = new label(2,j, msg); ws.addcell(result); string c2string = sh1.getcell(2, j).getcontents().tostring(); if(c2string.equalsignorecase(msg)) { label status=new label(5,j,"pass"); ws.addcell(status); } else { label status=new label(5,j,"fail"); ws.addcell(status); } } //add 4 labels label un= new label(0,0,"test data"); label pw= new label(1,0,"city code"); label desc= new label(2,0,"error message"); label st= new label(3,0,"scenario#"); label tsd = new label(4,0,"test scenario description"); label rs = new label(5,0,"results"); ws.addcell(un); ws.addcell(pw); ws.addcell(desc); ws.addcell(st); ws.addcell(tsd); ws.addcell(rs); wwb.write(); wwb.close(); } } } }
error message received:
failed: accountprofile_city_validations java.lang.nullpointerexception @ jxl.write.biff.file.write(file.java:149) @ jxl.write.biff.writableworkbookimpl.write(writableworkbookimpl.java:697) @ com.ode.admin.accprofile_zipfield_validations1.accountprofile_city_validations(accprofile_zipfield_validations1.java:258) @ 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) @ org.testng.internal.methodinvocationhelper.invokemethod(methodinvocationhelper.java:80) @ org.testng.internal.invoker.invokemethod(invoker.java:714) @ org.testng.internal.invoker.invoketestmethod(invoker.java:901) @ org.testng.internal.invoker.invoketestmethods(invoker.java:1231) @ org.testng.internal.testmethodworker.invoketestmethods(testmethodworker.java:128) @ org.testng.internal.testmethodworker.run(testmethodworker.java:111) @ org.testng.testrunner.privaterun(testrunner.java:767) @ org.testng.testrunner.run(testrunner.java:617) @ org.testng.suiterunner.runtest(suiterunner.java:334) @ org.testng.suiterunner.runsequentially(suiterunner.java:329) @ org.testng.suiterunner.privaterun(suiterunner.java:291) @ org.testng.suiterunner.run(suiterunner.java:240) @ org.testng.suiterunnerworker.runsuite(suiterunnerworker.java:52) @ org.testng.suiterunnerworker.run(suiterunnerworker.java:86) @ org.testng.testng.runsuitessequentially(testng.java:1203) @ org.testng.testng.runsuiteslocally(testng.java:1128) @ org.testng.testng.run(testng.java:1036) @ org.testng.remote.remotetestng.run(remotetestng.java:111) @ org.testng.remote.remotetestng.initandrun(remotetestng.java:204) @ org.testng.remote.remotetestng.main(remotetestng.java:175)
please me out issue. appreciated.
i have idea, maybe helps:
the driver.manage().timeouts().implicitlywait(30, timeunit.seconds);
, other wait functions because multithreaded environment.
in cases wait, sleep good, easy fast solution can be, until strange behavior doesn't happen few times, checking wrong.
i suggest spend bit more time on code review , remove sleep , wait functions , synchronized blocks, functions instead, , thread join how needed in multithreaded environment.
probably statement1 need write , say: wait 30 sec, workbook try write file , after 30 sec elapsed executed statement1 , should set field value. @ moment, when workbook write file isn't set field1, statement1.
other idea:
place breakpoint @ line : wwb.write();
there investigate field has no value.
i hope helps
Comments
Post a Comment