尝试在空对象引用上调用虚拟方法'java.lang.String java.lang.Integer.toString()'

时间:2019-08-12 07:30:36

标签: java android null

我试图解决此问题已经两天了,但是仍然无法弄清为什么我得到了NullPointException。

我尝试更改模型类的参数,但这无济于事。

//call the task api
    private void taskuploadArt(String name,String width,String height,String year, String technique, String price,String contact)
    {
        String currency_code = currency.getText().toString().trim();
        String tags = painting_tags.getText().toString().trim();
        String info = painting_story.getText().toString().trim();
        String wishtosell;
        if(wisht_to_sell)
        {
            wishtosell="1";
            //Toast.makeText(this, "Done here", Toast.LENGTH_SHORT).show();
        }
        else
        {
           wishtosell="0";
        }

        String sellprints;
        if(sell_prints)
        {
         sellprints="1";
        }
        else {
            sellprints="0";
        }

        String createdby_me;
        if(createdbyme)
        {
            createdby_me="1";
        }
        else
        {
            createdby_me="0";
        }


         UploadArtApiInterface uploadartapiInterface;
        //API call
        uploadartapiInterface = ApiClient.getApiClient().create(UploadArtApiInterface.class);
        Call<TaskNew> call = uploadartapiInterface.uploadtask(api_key,
                name,
                info,
                year,
                technique,
                createdby_me,
                wishtosell,
                price,
                contact,
                dimen_unit,
                width,
                height,
                currency_code,
                sellprints,
                tags);

        call.enqueue(new Callback<TaskNew>() {
            //show progress dialog
            final ProgressDialog progressDialog = ProgressDialogPopup.showProgressDialog(UploadArt.this,
                    getResources().getString(R.string.loading), "");
            @Override
            public void onResponse(@NonNull Call<TaskNew> call, @NonNull Response<TaskNew> response) {

                //on response successful
                if(response.isSuccessful())
                {
                    //Toast.makeText(UploadArt.this,"upload task successful",Toast.LENGTH_SHORT).show();
                    //call image upload api
                    try
                    {
                        //Toast.makeText(UploadArt.this, "here", Toast.LENGTH_SHORT).show();
                        assert response.body() != null;
                        if(uploadArtImage(response.body().getTaskId().toString().trim(),response.body().getUserId().toString().trim()))
                        {
                            Toast.makeText(UploadArt.this, "Uploaded", Toast.LENGTH_SHORT).show();
                            //successfully image uploaded
                            progressDialog.dismiss();
                            UploadArt.super.onBackPressed();
                            //Toast.makeText(UploadArt.this, "Success", Toast.LENGTH_SHORT).show();
                            //AlertPopup.alertDialogShow(UploadArt.this, response.body().getMessage().toString(), "");

                            //Log.d("ImageUploadq123",response.body().toString());
                        }
                        else {
                            Toast.makeText(UploadArt.this, "Not uploaded", Toast.LENGTH_SHORT).show();
                            //image upload unsuccessful
                            progressDialog.dismiss();
                            //Log.d("ImageUploadq123",response.body().toString());
                        }
                    }
                    catch (Exception e)
                    {
                        progressDialog.dismiss();
                        AlertPopup.alertDialogShow(UploadArt.this, e.getMessage(), "");
                    }
                }
                else
                {
                    //Toast.makeText(UploadArt.this,"Upload task failed",Toast.LENGTH_SHORT).show();
                    progressDialog.dismiss();
                    //AlertPopup.alertDialogShow(UploadArt.this, "Some error occurred 2", "");
                    Toast.makeText(UploadArt.this, "Something wrong with your internet connection. Please try again.", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(@NonNull Call<TaskNew> call, @NonNull Throwable t) {
                progressDialog.dismiss();
                //Toast.makeText(UploadArt.this,"upload task failed",Toast.LENGTH_SHORT).show();
                AlertPopup.alertDialogShow(UploadArt.this, "Some error occurred 3", "");
                Toast.makeText(UploadArt.this, "Something wrong with your internet connection. Please try again.", Toast.LENGTH_SHORT).show();

            }
        });

    }




 //image upload api
    private boolean uploadArtImage(String task_Id, String user_Id)
    {
        // Build and send an Event.
        mTracker.send(new HitBuilders.EventBuilder()
                .setCategory(getString(R.string.ga_upload_action))
                .setAction(getString(R.string.ga_upload_action))
                .build());

        //check if pic is properly set to the imageview
        final Boolean[] imageresonse = new Boolean[1];

        if(imageadded)
        {
            //get the bytes
            Bitmap bitmap = ((BitmapDrawable) previewImage.getDrawable()).getBitmap();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            byte[] imageInByte = baos.toByteArray();

            RequestBody requestFile = RequestBody.create(MediaType.parse("image/png"), imageInByte);
            MultipartBody.Part body = MultipartBody.Part.createFormData("image", "image"+"_"+user_Id+"_"+task_Id+".jpg", requestFile);
            RequestBody userId = RequestBody.create(MediaType.parse("text/plain"), user_Id);
            RequestBody taskId = RequestBody.create(MediaType.parse("text/plain"), task_Id);

            UploadArtApiInterface uploadArtApiInterface = ApiClient.getApiClient().create(UploadArtApiInterface.class);
            Call<ProfilePicUploadResponse> call = uploadArtApiInterface.uploadArt(body,userId,taskId);

            final ProgressDialog progressDialog = ProgressDialogPopup.showProgressDialog(UploadArt.this,
                    getResources().getString(R.string.loading), "");
            call.enqueue(new Callback<ProfilePicUploadResponse>() {
                @Override
                public void onResponse(@NonNull Call<ProfilePicUploadResponse> call, @NonNull Response<ProfilePicUploadResponse> response) {

                    progressDialog.dismiss();

//                    ProfilePicUploadResponse profilePicUploadResponse = response.body();
//                    if (profilePicUploadResponse.getError() != null){
//                        AlertPopup.alertDialogShow(UploadArt.this, response.body().getMessage(), "");
//                    }
//                    else {
//                        AlertPopup.alertDialogShow(UploadArt.this, profilePicUploadResponse.getMessage(), "");
//                    }
                    if(response.isSuccessful())
                    {
                        imageresonse[0] =true;
                        //Toast.makeText(UploadArt.this,response.body().getMessage(),Toast.LENGTH_SHORT).show();
                        //AlertPopup.alertDialogShow(UploadArt.this, response.body().getMessage(), "");
                        Intent intent = new Intent();
                        intent.putExtra("message","message");
                        setResult(12);
                        finish();
                        //UploadArt.super.onBackPressed();
                        //Log.d("ImageUpload",response.body().toString());
                    }
                    else
                    {
                        //AlertPopup.alertDialogShow(UploadArt.this, response.body().getMessage(), "");
                        //Log.d("ImageUpload","error occurred");
                        imageresonse[0] =false;
                       //Toast.makeText(UploadArt.this,response.body().toString(),Toast.LENGTH_SHORT).show();
                    }
                }
                @Override
                public void onFailure(@NonNull Call<ProfilePicUploadResponse> call, @NonNull Throwable t) {
                    imageresonse[0] =true;
                    //Toast.makeText(UploadArt.this,response.body().getMessage(),Toast.LENGTH_SHORT).show();
                    //AlertPopup.alertDialogShow(UploadArt.this, t.getMessage(), "");
                    Intent intent = new Intent();
                    intent.putExtra("message","message");
                    setResult(12);
                    finish();
//                    progressDialog.dismiss();
//                    Log.d("ImageUpload",t.getMessage());
//                    //AlertPopup.alertDialogShow(UploadArt.this, t.getMessage(), "");
//                    imageresonse[0] =false;
                }
            });
            return imageresonse[0];
        }
        else {
            return false;
        }

    }

我在UI中有一个开关,当它打开时要价。关闭时,它不会询问其他任何信息。

更新: 我已经解决了问题。 我发布的参数之一为空。

2 个答案:

答案 0 :(得分:0)

在将taskid和userid转换为字符串之前,请对它们进行空检查。

if(uploadArtImage(response.body().getTaskId().toString().trim(),response.body().getUserId().toString().trim()))

答案 1 :(得分:0)

response.body()只能使用一次。

if(uploadArtImage(response.body().getTaskId().toString().trim(),response.body().getUserId().toString().trim())) {...}

第二次调用response.body()将为null。您应该像这样存储它:

String body = response.body();
if(uploadArtImage(body.getTaskId().toString().trim(),body.getUserId().toString().trim())) {...}