Android getAssets() method throws NullPointerException

时间秒杀一切 提交于 2019-12-11 14:37:59

问题


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.

Where is the 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() && !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)) > 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());
            }
        }
    }
}

Where is the exception:

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.<init>(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

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.


回答1:


The next time, please indicate which is line number 41 here. IMHO this was only partial code making the stack trace unusable

The problem i think is here:

AssetManager assetManager = getAssets();
            for (String file : assetManager.list("tessdata")) {
                InputStream in = assetManager.open("tessdata/" + file);

if assetManager.list("tessdata") returns a null (that there is no tessdata), that null goes into String file as one entry, then you're calling

InputStream in = assetManager.open("tessdata/" + file);

"tessdata/" + file will not exist if "tessdata" didn't exist

So, the solution might be:

Solution

for (String file : assetManager.list("tessdata")) {
if(file!=null){
                InputStream in = assetManager.open("tessdata/" + file);
...
    }
}


来源:https://stackoverflow.com/questions/14430692/android-getassets-method-throws-nullpointerexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!