Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid getAssets() method throws NullPointerException
    primarykey
    data
    text
    <p>I'm trying to get assets of my app from Service and i get nullpointexception. I found few articles about it.but i don't get where my mistake.</p> <p>Where is the code:</p> <pre><code>public class OCRService extends IntentService{ private static final String TAG = "OCRService"; public static final String DATA_PATH = Environment.getExternalStorageDirectory().toString() + "/SimpleAndroidOCR/"; public OCRService() { super("OCRService"); String path = new String(DATA_PATH + "tessdata/"); File dir = new File(path); if (!new File(path).exists() &amp;&amp; !dir.mkdirs()) { Log.e(TAG, "ERROR: Creation of directory " + path + " on sdcard failed"); return; } else { Log.v(TAG, "Created directory " + path + " on sdcard"); if (!(new File(DATA_PATH + "tessdata/" + "eng.traineddata")).exists()) { try { AssetManager assetManager = getAssets(); for (String file : assetManager.list("tessdata")) { InputStream in = assetManager.open("tessdata/" + file); OutputStream out = new FileOutputStream(DATA_PATH + "tessdata/" + file); byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) &gt; 0) { out.write(buf, 0, len); } in.close(); out.close(); } Log.v(TAG, "Copying ocr traineddata sacceded"); } catch (IOException e) { Log.e(TAG, "Was unable to copy osr traineddata "+ e.toString()); } } } } </code></pre> <p>Where is the exception:</p> <pre><code>FATAL EXCEPTION: main java.lang.RuntimeException: Unable to instantiate service com.pc.droidhelper.services.OCRService: java.lang.NullPointerException at android.app.ActivityThread.handleCreateService(ActivityThread.java:2241) at android.app.ActivityThread.access$1600(ActivityThread.java:127) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4476) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:816) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:583) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101) at com.pc.droidhelper.services.OCRService.&lt;init&gt;(OCRService.java:41) at java.lang.Class.newInstanceImpl(Native Method) at java.lang.Class.newInstance(Class.java:1319) at android.app.ActivityThread.handleCreateService(ActivityThread.java:2238) ... 10 more </code></pre> <p>as you can see getAssets is called from Service. and i have context there. I have read that I chould use getAssets from main activity. please tel my why, what is the difference.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload