我创建了一个公共类来访问所有POJO类。当我试图解析和访问POJO变量时,我得到的是空指针异常。我真的不知道我哪里错了。有人能帮我解决这个问题吗?
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());
}
});}日志
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)发布于 2018-02-19 10:13:53
尝尝这个
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<Github> call = githubService.getGithubUserInfo(mGitUserName);
call.enqueue(new retrofit2.Callback<Github>() {
@Override
public void onResponse(Call<Github> call, Response<Github> response) {
System.out.println("Response:+"+response.body().getName());
}
@Override
public void onFailure(Call<Github> call, Throwable t) {
System.out.println("Response error:+"+t.getMessage());
}
});}和
public interface GithubService {
@GET("users/{username}")
Call<Github> getGithubUserInfo(@Path("username") String username);}https://stackoverflow.com/questions/48863594
复制相似问题