异常:引起:java.lang.NullPointerException:asset

时间:2013-09-12 07:32:25

标签: java android

我正在读取资产文件夹中的.OBJ文件。但是我在阅读文件时遇到异常。即使我在Eclipse上调试项目,但我可以找到原因。

请帮帮我

提前致谢。

/**
 * Load Object Asynchronous.
 * @author Ajay
 */
private class ObjLoaderAsync extends AsyncTask<Void, Void, Void> {

    private ProgressDialog progressDialog;

    @Override
    protected void onPreExecute() {
        try {
            progressDialog = new ProgressDialog(localContext);
            progressDialog.setTitle(localContext
                    .getString(R.string.app_name));
            progressDialog.setMessage(localContext
                    .getString(R.string.please_wait));
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progressDialog.setCancelable(true);
            progressDialog.show();
        } catch (Exception e) {
        }
    }

    @Override
    protected Void doInBackground(Void... arg0) {
        try {
            mr[getCurrentPosition()] = new ModelRenderer(localContext,
                    localContext.getAssets().open(RendererView.objName));
        } catch (java.io.IOException e) {
            Log.v("DemoRendererView", "loading model: " + e);
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void param) {
        try {
            progressDialog.cancel();
        } catch (Exception e) {

        }
    }
}

public ModelRenderer(Context paramContext, InputStream localFileInputStream) throws FileNotFoundException {
    ModelStaticClassTransfer.value = -777.0F;
    while (true) {
        try {
            i = localFileInputStream.read();
            if (i != -1)
                continue;
            localFileInputStream.close();
            if ((char) i == 'v') {
                i = localFileInputStream.read();
                if ((char) i != ' ')
                    continue;
                this.verticeCounter = (1 + this.verticeCounter);
                continue;
            }
            if ((char) i == 'f') {
                i = localFileInputStream.read();
                if ((char) i != ' ')
                    continue;
                this.indexCounter = (1 + this.indexCounter);
                continue;
            }
            int j = localFileInputStream.read();
            i = j;
        } catch (IOException localIOException) {
            localIOException.printStackTrace();
            return;
        }
    }
}

错误跟踪

09-12 12:54:57.516: E/AndroidRuntime(29949): FATAL EXCEPTION: AsyncTask #2
09-12 12:54:57.516: E/AndroidRuntime(29949): java.lang.RuntimeException: An error occured while executing doInBackground()
09-12 12:54:57.516: E/AndroidRuntime(29949):    at android.os.AsyncTask$3.done(AsyncTask.java:278)
09-12 12:54:57.516: E/AndroidRuntime(29949):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
09-12 12:54:57.516: E/AndroidRuntime(29949):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
09-12 12:54:57.516: E/AndroidRuntime(29949):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
09-12 12:54:57.516: E/AndroidRuntime(29949):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-12 12:54:57.516: E/AndroidRuntime(29949):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
09-12 12:54:57.516: E/AndroidRuntime(29949):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-12 12:54:57.516: E/AndroidRuntime(29949):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-12 12:54:57.516: E/AndroidRuntime(29949):    at java.lang.Thread.run(Thread.java:856)
09-12 12:54:57.516: E/AndroidRuntime(29949): Caused by: java.lang.NullPointerException: asset
09-12 12:54:57.516: E/AndroidRuntime(29949):    at android.content.res.AssetManager.readAssetChar(Native Method)
09-12 12:54:57.516: E/AndroidRuntime(29949):    at android.content.res.AssetManager.access$200(AssetManager.java:35)
09-12 12:54:57.516: E/AndroidRuntime(29949):    at android.content.res.AssetManager$AssetInputStream.read(AssetManager.java:548)
09-12 12:54:57.516: E/AndroidRuntime(29949):    at com.amplimesh.models.ModelRenderer.<init>(ModelRenderer.java:64)
09-12 12:54:57.516: E/AndroidRuntime(29949):    at com.amplimesh.models.ModelGLRenderer$ObjLoaderAsync.doInBackground(ModelGLRenderer.java:138)
09-12 12:54:57.516: E/AndroidRuntime(29949):    at com.amplimesh.models.ModelGLRenderer$ObjLoaderAsync.doInBackground(ModelGLRenderer.java:1)
09-12 12:54:57.516: E/AndroidRuntime(29949):    at android.os.AsyncTask$2.call(AsyncTask.java:264)
09-12 12:54:57.516: E/AndroidRuntime(29949):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-12 12:54:57.516: E/AndroidRuntime(29949):    ... 5 more

3 个答案:

答案 0 :(得分:2)

我认为你误解了continue关键字的含义。这个关键字意味着循环立即跳转到下一次迭代而不执行循环块中的其余代码。

在您的情况下,这意味着此代码

i = localFileInputStream.read();
if (i != -1)
    continue;
localFileInputStream.close();
if ((char) i == 'v') {
    i = localFileInputStream.read();

关闭inputStream,然后尝试从中读取。由于所有的继续,你的算法不是很清楚,所以我不能真正告诉你如何解决这个问题。

答案 1 :(得分:0)

 @Override
    protected void onPostExecute(Void param) {
        try {
            mr[getCurrentPosition()] = new ModelRenderer(localContext,
                    localContext.getAssets().open(RendererView.objName));
             dialog.cancle();
        } catch (java.io.IOException e) {
            Log.v("DemoRendererView", "loading model: " + e);
        }
    }

请检查一下......

答案 2 :(得分:0)

你从

开始
try {
        i = localFileInputStream.read();
        if (i != -1)
            continue;
        localFileInputStream.close();

所以,您正在关闭流,然后您尝试再次从中读取。此外,您在无限循环内运行此操作,并仅在catch部分退出。完成阅读后,请考虑关闭流。