null pointer exception while running doInBackground method

a 夏天 提交于 2019-12-13 03:38:20

问题


i am trying to upload a file to dropbox, i have a also create a progress bar to check its status but while executing it throws exception . i created a object of UploadFile class and call the UploadFile function and the argument which are pass are UploadData.this, mApi,FILE_DIR ,file.

public class UploadFile extends AsyncTask<Void, Long, Boolean> {
private DropboxAPI<?> mApi;
private String mPath;
private File mFile;
private long mFileLen;
private UploadRequest mRequest;
private Context mContext;
private final ProgressDialog mDialog;
private String mErrorMsg;
public UploadFile(Context context, DropboxAPI<?> api, String dropboxPath,
        File file) 
{
    mContext = context.getApplicationContext();
    mFileLen = file.length();
    mApi = api;
    mPath = dropboxPath;
    mFile = file;

    mDialog = new ProgressDialog(context);
    mDialog.setMax(100);
    mDialog.setMessage("Uploading " + file.getName());
    mDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    mDialog.setProgress(0);
    mDialog.setButton("Cancel", new OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // This will cancel the putFile operation
            mRequest.abort();
        }
    });
    mDialog.show();
}

@Override
protected Boolean doInBackground(Void... params) {
    try {
        FileInputStream fis = new FileInputStream(mFile);
        String path = mPath + mFile.getName();
        mRequest = mApi.putFileOverwriteRequest(path, fis, mFile.length(),
                new ProgressListener() {
            @Override
            public long progressInterval() {
                return 500;
            }
            public void onProgress(long bytes, long total) {
                publishProgress(bytes);
            }
        });

        if (mRequest != null) {
            mRequest.upload();
            return true;
        }

    } catch (DropboxUnlinkedException e) {
        mErrorMsg = "This app wasn't authenticated properly.";
    } catch (DropboxFileSizeException e) {
        mErrorMsg = "This file is too big to upload";
    } catch (DropboxPartialFileException e) {
        mErrorMsg = "Upload canceled";
    } catch (DropboxServerException e) {
        if (e.error == DropboxServerException._401_UNAUTHORIZED) {
        } else if (e.error == DropboxServerException._403_FORBIDDEN) {
        } else if (e.error == DropboxServerException._404_NOT_FOUND) {

        } else if (e.error == DropboxServerException._507_INSUFFICIENT_STORAGE) {
            // user is over quota
        } else {
            // Something else
        }
        // This gets the Dropbox error, translated into the user's language
        mErrorMsg = e.body.userError;
        if (mErrorMsg == null) {
            mErrorMsg = e.body.error;
        }
    } catch (DropboxIOException e) {

        mErrorMsg = "Network error.  Try again.";
    } catch (DropboxParseException e) {
        mErrorMsg = "Dropbox error.  Try again.";
    } catch (DropboxException e) {
        mErrorMsg = "Unknown error.  Try again.";
    } catch (FileNotFoundException e) {
    }
    return false;
}

@Override
protected void onProgressUpdate(Long... progress) {
    int percent = (int)(100.0*(double)progress[0]/mFileLen + 0.5);
    mDialog.setProgress(percent);
}

@Override
protected void onPostExecute(Boolean result) {
    mDialog.dismiss();
    if (result) {
        showToast("Image successfully uploaded");
    } else {
        showToast(mErrorMsg);
    }
}

private void showToast(String msg) {
    Toast error = Toast.makeText(mContext, msg, Toast.LENGTH_LONG);
    error.show();
}
}

here is the log (i am new to this and i don't understand the log properly)

06-30 06:18:34.400: W/dalvikvm(422): threadid=7: thread exiting with uncaught exception (group=0x4001d800)
06-30 06:18:34.410: E/AndroidRuntime(422): FATAL EXCEPTION: AsyncTask #1
06-30 06:18:34.410: E/AndroidRuntime(422): java.lang.RuntimeException: An error occured while executing doInBackground()
06-30 06:18:34.410: E/AndroidRuntime(422):  at android.os.AsyncTask$3.done(AsyncTask.java:200)
06-30 06:18:34.410: E/AndroidRuntime(422):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
06-30 06:18:34.410: E/AndroidRuntime(422):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
06-30 06:18:34.410: E/AndroidRuntime(422):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
06-30 06:18:34.410: E/AndroidRuntime(422):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-30 06:18:34.410: E/AndroidRuntime(422):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
06-30 06:18:34.410: E/AndroidRuntime(422):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
06-30 06:18:34.410: E/AndroidRuntime(422):  at java.lang.Thread.run(Thread.java:1096)
06-30 06:18:34.410: E/AndroidRuntime(422): Caused by: java.lang.NullPointerException
06-30 06:18:34.410: E/AndroidRuntime(422):  at com.sachin.UploadFile.doInBackground(UploadFile.java:59)
06-30 06:18:34.410: E/AndroidRuntime(422):  at com.sachin.UploadFile.doInBackground(UploadFile.java:1)
06-30 06:18:34.410: E/AndroidRuntime(422):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
06-30 06:18:34.410: E/AndroidRuntime(422):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)

回答1:


Make sure you properly initialize

UploadRequest mApi;

as not doing so will cause a NullPointerException when the onClick method is called.




回答2:


Put a break point on the line where you have the exception and debug your application. When it stops there, check all the variable values you are using on that line. If one or more is null, there is your error. Those are the variables that you forget to initialize before using in that method.



来源:https://stackoverflow.com/questions/11270531/null-pointer-exception-while-running-doinbackground-method

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