java - Read android assets files throw NullPointerException -
i working on android automation test, create test project in eclipse. (in automation, packed apk , deploy onto emulator) , in project, want read xml file in assets folder. put xml file "mytest.xml" directly in folder assets. , want load , parse. seems nullpointerexception. below code
1.the function defined in class osncommonlib.
public class osncommonlib extends activity { public string readtranslationfile(string filename,string transunitid) { if(doc == null) { try { inputstream in = getassets().open(filename); documentbuilderfactory factory = documentbuilderfactory.newinstance(); documentbuilder builder = factory.newdocumentbuilder(); doc = builder.parse(in); } catch ( exception e ) { system.out.println("mytest catch" + e.tostring() ); e.printstacktrace(); } } org.w3c.dom.element root = doc.getdocumentelement(); nodelist nodes = root.getelementsbytagname( "trans-unit" ); ....... } .... }
and function above called in java class
public class tc01_osndemo extends activityinstrumentationtestcase2 { osncommonlib ocl = new osncommonlib(); ... public void testsortbybutton(){ .... string getstr = ocl.readtranslationfile("mytest","osn_menu_sortby"); assert.asserttrue(solo.searchtext(getstr)); ... }
and in catch, java.lang.nullpointerexception. means can't load file? tried many times , still same errors.
can give suggestions issue?
besides: here full log
java.lang.nullpointerexception @ com.stg.osn.lib.osncommonlib.readtranslationfile(osncommonlib.java:107) --> **point org.w3c.dom.element root = doc.getdocumentelement();** @ com.sgt.osn.junit.test.tc01_osndemo.testsortbybutton(tc01_osndemo.java:142) @ java.lang.reflect.method.invokenative(native method) @ android.test.instrumentationtestcase.runmethod(instrumentationtestcase.java:214) @ android.test.instrumentationtestcase.runtest(instrumentationtestcase.java:199) @ android.test.activityinstrumentationtestcase2.runtest(activityinstrumentationtestcase2.java:192) @ android.test.androidtestrunner.runtest(androidtestrunner.java:190) @ android.test.androidtestrunner.runtest(androidtestrunner.java:175) @ android.test.instrumentationtestrunner.onstart(instrumentationtestrunner.java:555) @ android.app.instrumentation$instrumentationthread.run(instrumentation.java:1661)
at last, problem sovled. extend test case on instruments, should use below method call: inputstream in = getinstrumentation().getcontext().getassets().open("mytest.xml"); useful link http://developer.android.com/reference/android/test/instrumentationtestcase.html http://www.mail-archive.com/android-developers@googlegroups.com/msg62671.html
thanks all!!!
you can try this..
public string readdatafromassets(string file) { try { inputstream = getassets().open(file); bufferedreader reader = new bufferedreader(new inputstreamreader(is)); stringbuilder builder = new stringbuilder(); string line = null; try { while ((line = reader.readline()) != null) { builder.append(line); // builder.append("\n"); // append new line } } catch (ioexception e) { e.printstacktrace(); } { try { is.close(); } catch (ioexception e) { e.printstacktrace(); } } return builder.tostring(); } catch (ioexception e) { e.printstacktrace(); } return null; }
Comments
Post a Comment