GitHub Repo app building for learning purposes I’ve become stuck in this world of NullPointerException.
So it says on line 119 the ArrayList is null it’s a simple Callback via Retrofit where I try to get the data and then set they data further on. I’m unsure of where exactly in the process it fails as it just points to the initialization of the array on line 119 but I’m iterating over that array and then adding each item to a collection.
Someone with more knowhow would be able to spot something that I can’t.
Any advice welcome.
Call<GitHubRepo> call = gitHubClient.getRepos(searchTerm, pageNumber, resultsPerPage); call.enqueue(new Callback<GitHubRepo>() { @Override public void onResponse(Call<GitHubRepo> call, Response<GitHubRepo> response) { ArrayList<Item> itemsList = response.body().getItems(); for(int i = 0; i < itemsList.size(); i++) { ModelCachedGitHubProject currentProject = new ModelCachedGitHubProject(); Item responseItem = itemsList.get(i); currentProject.setOwnerName(responseItem.getOwner().getLogin()); currentProject.setRepoName(responseItem.getName()); currentProject.setRepoSize(responseItem.getSize()); currentProject.setHasWiki(responseItem.isHas_wiki()); currentProject.setCreatedAt(responseItem.getCreated_at()); currentProject.setPushedAt(responseItem.getPushed_at()); currentProject.setUpdatedAt(responseItem.getUpdated_at()); currentProject.setHtmlUrl(responseItem.getHtml_url()); currentProject.setAvatarUrl(responseItem.getOwner().getAvatar_url()); currentProject.setLanguage(responseItem.getLanguage()); currentProject.setForksCount(responseItem.getForks_count()); currentProject.setScore(responseItem.getScore()); currentProject.setDescription(responseItem.getDescription()); gitHubProjectsList.add(currentProject); }
Any other classes that are needed just ask. Thanks.
StackTrace :
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.benmohammad.repoapp, PID: 2325 java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.ArrayList com.benmohammad.repoapp.data.webservice.apiresponse.GitHubRepo.getItems()' on a null object reference at com.benmohammad.repoapp.data.DataRepository$1.onResponse(DataRepository.java:119) at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1.lambda
GitHub Repo Repo
Thanks
public void updateFromWebservice(final String searchTerm, final int pageNumber, int resultsPerPage) { webServiceMessageCallStatus.setValue(WebServiceMessage.UPDATING_STATUS); final List<ModelCachedGitHubProject> gitHubProjectsList = new ArrayList<>(); if(retrofit == null || gitHubClient == null) { retrofit = new Retrofit.Builder().baseUrl(GitHubClientService.BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .build(); gitHubClient = retrofit.create(GitHubClientService.class); } Call<GitHubRepo> call = gitHubClient.getRepos(searchTerm, pageNumber, resultsPerPage); call.enqueue(new Callback<GitHubRepo>() { @Override public void onResponse(Call<GitHubRepo> call, Response<GitHubRepo> response) { ArrayList<Item> itemsList = response.body().getItems(); for(int i = 0; i < itemsList.size(); i++) { ModelCachedGitHubProject currentProject = new ModelCachedGitHubProject(); Item responseItem = itemsList.get(i); currentProject.setOwnerName(responseItem.getOwner().getLogin()); currentProject.setRepoName(responseItem.getName()); currentProject.setRepoSize(responseItem.getSize()); currentProject.setHasWiki(responseItem.isHas_wiki()); currentProject.setCreatedAt(responseItem.getCreated_at()); currentProject.setPushedAt(responseItem.getPushed_at()); currentProject.setUpdatedAt(responseItem.getUpdated_at()); currentProject.setHtmlUrl(responseItem.getHtml_url()); currentProject.setAvatarUrl(responseItem.getOwner().getAvatar_url()); currentProject.setLanguage(responseItem.getLanguage()); currentProject.setForksCount(responseItem.getForks_count()); currentProject.setScore(responseItem.getScore()); currentProject.setDescription(responseItem.getDescription()); gitHubProjectsList.add(currentProject); } if(!gitHubProjectsList.isEmpty()) { boolean clearPreviousCache; if(pageNumber == 1) { clearPreviousCache = true; saveLastSearchTerm(searchTerm); } else clearPreviousCache = false; cacheProjectsList(gitHubProjectsList, clearPreviousCache); setLastRefreshDate(new Date()); webServiceMessageCallStatus.postValue(WebServiceMessage.ON_RESPONSE_SUCCESS); } else { if(pageNumber == 1) webServiceMessageCallStatus.postValue(WebServiceMessage.ON_RESPONSE_NOTHING_FOUND); else { webServiceMessageCallStatus.postValue(WebServiceMessage.ON_RESPONSE_NO_MORE_RESULTS); } } } @Override public void onFailure(Call<GitHubRepo> call, Throwable t) { webServiceMessageCallStatus.postValue(WebServiceMessage.ON_FAILURE); } }); }
Advertisement
Answer
Response is null, assuming you are talking about ArrayList itemsList line.. We can’t see line numbers. If you look at the stack trace, it says .getItems()' on a null object reference.
That means response.getBody()
is null. So you need to check where that response is being initialized (being sent) and make sure there isn’t a problem in that class/service.