您好,我想使用000webhost使我的应用程序联机,但即时通讯出错。下面是我的代码

时间:2018-09-15 10:37:02

标签: android nullpointerexception

 public static final String BASE_URL = "http://kanzen.000webhostapp.com/";

 public static final String API_TOKEN_URL = "http://kanzen.000webhostapp.com/braintree/main.php";

这是我现在在Common.java上使用的链接

在使用它正常工作之前。

public static final String BASE_URL = "http://localhost/";

 public static final String API_TOKEN_URL = "http://localhost/braintree/main.php";

Here's where i'm getting the error which is my MainActivity.java

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE)
        {
            AccountKitLoginResult  result = data.getParcelableExtra(AccountKitLoginResult.RESULT_KEY);

            if (result.getError() != null)
            {
                Toast.makeText(this, ""+result.getError().getErrorType().getMessage(), Toast.LENGTH_SHORT).show();
            }
            else if (result.wasCancelled())
            {
                Toast.makeText(this, "Cancelled", Toast.LENGTH_SHORT).show();
            }
            else
                {
                    if (result.getAccessToken() != null) {
                        final android.app.AlertDialog alertDialog = new SpotsDialog(MainActivity.this);
                        alertDialog.show();
                        alertDialog.setMessage("Processing... Please Wait....");

                        AccountKit.getCurrentAccount(new AccountKitCallback<Account>() {
                            @Override
                            public void onSuccess(final Account account) {

                                mService.checkUserExists(account.getPhoneNumber().toString())
                                        .enqueue(new Callback<CheckUserResponse>() {
                                            @Override
                                            public void onResponse(Call<CheckUserResponse> call, Response<CheckUserResponse> response) {
                                                CheckUserResponse userResponse = response.body();
                                                if (userResponse.isExists())
                                                {
                                                    mService.getUserInformation(account.getPhoneNumber().toString())
                                                            .enqueue(new Callback<User>() {
                                                                @Override
                                                                public void onResponse(Call<User> call, Response<User> response) {

                                                                    alertDialog.dismiss();

                                                                    Common.currentUser = response.body();

                                                                    startActivity(new Intent(MainActivity.this,MainActivity.class));
                                                                    finish();
                                                                }

                                                                @Override
                                                                public void onFailure(Call<User> call, Throwable t) {
                                                                    Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
                                                                }
                                                            });
                                                }
                                                else
                                                    {
                                                        alertDialog.dismiss();

                                                        showRegisterDialog(account.getPhoneNumber().toString());
                                                    }
                                            }

                                            @Override
                                            public void onFailure(Call<CheckUserResponse> call, Throwable t) {

                                                Toast.makeText(MainActivity.this, "ERROR", Toast.LENGTH_SHORT).show();
                                                alertDialog.dismiss();
                                            }
                                        });
                            }

                            @Override
                            public void onError(AccountKitError accountKitError) {
                                Log.d("ERROR",accountKitError.getErrorType().getMessage());
                            }
                        });
                    }
                }
        }
    }

这是我在运行应用程序时遇到的错误

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.motasemx.itsproject, PID: 17303
                  java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.example.motasemx.itsproject.Model.CheckUserResponse.isExists()' on a null object reference
                      at com.example.motasemx.itsproject.MainActivity$4$1.onResponse(MainActivity.java:201)
                      at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)
                      at android.os.Handler.handleCallback(Handler.java:836)
                      at android.os.Handler.dispatchMessage(Handler.java:103)
                      at android.os.Looper.loop(Looper.java:232)
                      at android.app.ActivityThread.main(ActivityThread.java:6802)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1103)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)

1 个答案:

答案 0 :(得分:0)

实际上,您会得到空指针异常,因为服务器返回的响应为空,并且您正在尝试访问空对象

 mService.checkUserExists(account.getPhoneNumber().toString())
                                    .enqueue(new Callback<CheckUserResponse>() {
                                        @Override
                                        public void onResponse(Call<CheckUserResponse> call, Response<CheckUserResponse> response) {
                                            CheckUserResponse userResponse = response.body();
                                            //userResponse is null
                                            if (userResponse.isExists())
                                            {

                                            }
                                        }
                                        @Override
                                        public void onFailure(Call<CheckUserResponse> call, Throwable t) {
                                        }
                                    });