从Parse下载文件抛出NullPointerException

时间:2015-09-12 19:25:02

标签: java android parse-platform

在我的应用程序中,我可以成功将文件上传到parse.com。但是当我尝试下载它时,它会给出空指针异常。这是我下载文件的代码。

ParseObject downloadData = new ParseObject("DownloadData");
        ParseFile downloadFile = (ParseFile) downloadData.get("File");
        downloadFile.getDataInBackground(new GetDataCallback() {
            @Override
            public void done(byte[] bytes, ParseException e) {
                if (e == null) {
                    String x= new String(bytes);
                    new AlertDialog.Builder(MainActivity2.this)
                            .setTitle("Downloaded File")
                            .setMessage(x)
                            .setPositiveButton("Ok", null)
                            .show();
                } else {
                    new AlertDialog.Builder(MainActivity2.this)
                            .setTitle("Download File")
                            .setMessage("An Error Occurred")
                            .setPositiveButton("Ok", null)
                            .show();
                }
            }
        });

官方文档令人困惑。谁能告诉我解决这个问题的方法。

1 个答案:

答案 0 :(得分:1)

你不能在像这样创建的ParseObject上调用get()。 首先,您需要在Parse类上调用parseQuery并从此查询结果中获取此ParseObject。现在在此ParseObject上调用get()。

ParseQuery<ParseObject> query = ParseQuery.getQuery("DownloadData");
query.getInBackground("parse_object_id", new GetCallback<ParseObject>() {
public void done(ParseObject downloadData, ParseException e) {
if (e == null) {
  // This object will contain your file
   ParseFile downloadFile = (ParseFile) downloadData.get("File");
    downloadFile.getDataInBackground(new GetDataCallback() {
        @Override
        public void done(byte[] bytes, ParseException e) {
            if (e == null) {
                String x= new String(bytes);
                new AlertDialog.Builder(MainActivity2.this)
                        .setTitle("Downloaded File")
                        .setMessage(x)
                        .setPositiveButton("Ok", null)
                        .show();
            } else {
                new AlertDialog.Builder(MainActivity2.this)
                        .setTitle("Download File")
                        .setMessage("An Error Occurred")
                        .setPositiveButton("Ok", null)
                        .show();
            }
        }
    });
 } else {
  // something went wrong
  }

});