-1

I have been googling and googling how to get this to work but everything i try gets me the same error. here is my code

package com.gwiddle.airsoftcreations.airsoftapp;


import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import retrofit2.Call;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText; 
import retrofit2.Callback;
import retrofit2.Response;

public class SignUpFragment extends Fragment {

private EditText Name, Username, UserPassword;

private Button BnSignup;


public SignUpFragment() {


}

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup 
container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_sign_up, container, 
false);
    Name = view.findViewById(R.id.name);
    Username = view.findViewById(R.id.username);
    UserPassword = view.findViewById(R.id.password);
    BnSignup = view.findViewById(R.id.btnSignup);

    BnSignup.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
            performRegistration();
        }
    });

    return view;

}

public void performRegistration(){
    String name = Name.getText().toString();
    String username = Username.getText().toString();
    String password = UserPassword.getText().toString();
    Call<User> call = 
MainActivity.apiInterface.performRegistration(name,username,password);





            call.enqueue(new Callback<User>() {
                @Override
                public void onResponse(@NonNull Call<User> call, @NonNull 
 Response<User> response) {

                    if (response.body().getResponse().equals("ok"))
                    {
                        MainActivity.prefConfig.displayToast("Ready to 
  Deploy.....");
                    }
                    else if (response.body().getResponse().equals("exist"))
                    {
                        MainActivity.prefConfig.displayToast("User Already 
 In Squad, Try again.");

                    }
                    else if (response.body().getResponse().equals("error"))
                    {
                        MainActivity.prefConfig.displayToast("KIA.. 
Something Went Wrong.");

                    }

                }
                }

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

                }
            });

    Name.setText("");
    Username.setText("");
    UserPassword.setText("");
  }
  }

I and my mate are both new to java and have spent all day trying to fix this

this is the error i get

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.gwiddle.airsoftcreations.airsoftapp, PID: 20606 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.gwiddle.airsoftcreations.airsoftapp.User.getResponse()' on a null object reference at com.gwiddle.airsoftcreations.airsoftapp.SignUpFragment$2.onResponse(SignUpFragment.java:64) at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70) at android.os.Handler.handleCallback(Handler.java:809) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:166) at android.app.ActivityThread.main(ActivityThread.java:7377) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:469) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:963)

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 1
    I'm not very familiar with Retrofit, but by the looks of it the call to response.body() is returning null. not all HTTP requests will contain a response body. This can happen in the case of error codes or redirect codes, for example. I'd suggest attaching a debugger on line 64 and inspecting the response via the debugging tools, specifically looking for the response code. Also check the logcat output, retrofit may be outputting something there. – Ian Newson Sep 05 '18 at 15:56
  • `SignUpFragment.java:64` What is line 64? – Code-Apprentice Sep 05 '18 at 17:03
  • if (response.body().getResponse().equals("ok")) { – Callum Jackson Sep 06 '18 at 10:29
  • @CallumJackson hi got solution? – Arnold Brown Apr 19 '19 at 21:08

2 Answers2

1

Make a new if, at the very beginning of those checks, which looks like this:

if(response.body() == null || response.body().getResponse() == null ){
//show message or whatever
}
Maksym V.
  • 2,877
  • 17
  • 27
0

Read through the error, you are facing a NullPointerException :

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.gwiddle.airsoftcreations.airsoftapp, PID: 20606 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.gwiddle.airsoftcreations.airsoftapp.User.getResponse()' on a null object reference at com.gwiddle.airsoftcreations.airsoftapp.SignUpFragment$2.onResponse(SignUpFragment.java:64)

It says that you have called the getResponse() method on something which is null. So, you have to add null checks to ensure that you don't call methods on null references.

For example if a string can be null then ensure its not null before calling its methods :

String s = null;
...
if(s != null){
    s.trim();
} 

Use similar reasoning in your code.

Jitendra A
  • 1,568
  • 10
  • 18