进行事务时尝试在空对象引用上调用虚拟方法'int java.lang.String.length()'

时间:2018-11-09 02:41:17

标签: java android json

我开发了一个应用程序,用户可以在其中使用Paypal付款。交易正常。它在企业帐户中反映金额。另外,从个人帐户中扣除金额。但是在进行下一个活动时,我正在尝试在空对象引用错误时调用虚拟方法'int java.lang.String.length()'。

下面是我的代码-

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == PAYPAL_REQUEST_CODE){
            if(resultCode == RESULT_OK){
                PaymentConfirmation confirmation = data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
                if(confirmation != null){
                    try {
                        String paymentDetails = confirmation.toJSONObject().toString(4);
                        Log.d("paymentDetails", paymentDetails);
                        Log.d("fnf", fnf);
                        startActivity(new Intent(this, PaymentDetailsActivity.class).putExtra("PaymentDetails", paymentDetails).putExtra("PaymentAmount", fnf));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            } else if(resultCode == Activity.RESULT_CANCELED){
                Toast.makeText(this, "Cancel", Toast.LENGTH_SHORT).show();
            }
        } else if(requestCode == PaymentActivity.RESULT_EXTRAS_INVALID){
            Toast.makeText(this, "Invalid", Toast.LENGTH_SHORT).show();
        }
    }

下面是下一个活动代码,我遇到了错误-

try {
            JSONObject jsonObject = new JSONObject(i.getStringExtra("PaymentDetails"));
            //JSONObject jsonObject = new JSONObject(response);
            showDetails(jsonObject.getJSONObject("response"), i.getStringExtra("PaymentAmount"));
        } catch (JSONException e) {
            e.printStackTrace();
        }

此showDetails方法-

private void showDetails(JSONObject response, String paymentAmount) {
        try {
            txtId.setText(response.getString("id"));
            txtStatus.setText(response.getString("state"));
            txtAmount.setText(response.getString(String.format("$%s", paymentAmount)));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

这是stacktrace-

11-09 15:11:31.797 11358-11358/com.example.user.boat E/AndroidRuntime: FATAL EXCEPTION: main    Process: com.example.user.boat, PID: 11358    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.boat/com.example.user.boat.PaymentDetailsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2659)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6123)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
        at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
        at org.json.JSONTokener.nextValue(JSONTokener.java:94)
        at org.json.JSONObject.<init>(JSONObject.java:156)
        at org.json.JSONObject.<init>(JSONObject.java:173)
        at com.example.user.boat.PaymentDetailsActivity.onCreate(PaymentDetailsActivity.java:28)
        at android.app.Activity.performCreate(Activity.java:6672)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2612)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724) 
        at android.app.ActivityThread.-wrap12(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6123) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757) 

请帮助我。

0 个答案:

没有答案