I have created a common class to access all POJO classes. when i try to parse and acceess POJO variable i am getting null pointer exception. i really do not know where i was wrong . can anyone help me to resolve this issue?
CommonReponse.java
public class CommonResponse {
private Github github;
public Github getGithub() {
return github;
}
public void setGithub(Github github) {
this.github = github;
}}
GithubService.java
public interface GithubService {
@GET("users/{username}")
Call<CommonResponse> getGithubUserInfo(@Path("username") String username);}
Github.java // POJO
public class Github {
@SerializedName("name")
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}}
GetUserInfo() // Method
private void getUserInfo() {
Retrofit retrofit = new Retrofit.Builder()
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.baseUrl("https://api.github.com/")
.build();
GithubService githubService = retrofit.create(GithubService.class);
Call<CommonResponse> call = githubService.getGithubUserInfo(mGitUserName);
call.enqueue(new retrofit2.Callback<CommonResponse>() {
@Override
public void onResponse(Call<CommonResponse> call, Response<CommonResponse> response) {
System.out.println("Response:+"+response.body().getGithub().getName());
}
@Override
public void onFailure(Call<CommonResponse> call, Throwable t) {
System.out.println("Response error:+"+t.getMessage());
}
});}
log
02-19 15:38:53.926 32293-32293/com.ananth.rxandroidwithretrofit E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ananth.rxandroidwithretrofit, PID: 32293
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.ananth.rxandroidwithretrofit.model.Github.getName()' on a null object reference
at com.ananth.rxandroidwithretrofit.ProfileActivity$7.onResponse(ProfileActivity.java:243)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)