This question already has an answer here:
- Retrofit 2 - null response body 1 answer
- What is a NullPointerException, and how do I fix it? 12 answers
I'm trying to get data from web API, but something is wrong in my onResponse()
method and I can't figure it out.
My JSON looks like this
[
"partner1",
"partner2",
"partner3",
... ,
"partner150"
]
I have Table partners (ActiveAndroid) in which I would like to save all partners from the API.
@Table(name = "Partners")
public class Partners extends Model {
@Column(name = "Name")
String name;
public Partners() {}
public Partners(String name) {
this.name = name;
}
}
Here is my POJO model class:
public class Partners extends ArrayList<String> {
@SerializedName("partners")
@Expose
public List<String> partners = new ArrayList<String>();
public List<String> getPartners() {
return partners;
}
public void setName(List<String> partners) {
this.partners = partners;
}
}
This is my interface
public interface APIService {
@GET("Partners")
Call<Partners> getPartners();
}
This is my APIHelper class
public class APIHelper {
public static final String PARTNERS_URL = "https://part-of-link.com/partners.json/";
public static APIService apiService;
public static APIService getApiService() {
if (apiService == null) {
Retrofit retrofit = new Retrofit.Builder().baseUrl(PARTNERS_URL)
.addConverterFactory(GsonConverterFactory.create()).build();
apiService = retrofit.create(APIService.class);
}
return apiService;
}
}
And this is Fragment where I have a Button on which I would like to implement thw onClick()
method to get the data from the API and save it into the Partners table.
public class DownloadMain extends Fragment implements Callback<Partners> {
private Button dloadPartners;
private Call<Partners> callPartners;
public static APIService apiService;
public DownloadMain() {}
public DownloadMain newInstance() { return new DownloadMain(); }
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.download_main, container, false);
dloadPartners = (Button) view.findViewById(R.id.downloadPartners);
dloadPartners.setOnClickListener(btnListener);
callPartners = APIHelper.getApiService().getPartners();
callPartners.enqueue(this);
return view;
}
Button.OnClickListener btnListener = (new View.OnClickListener() {
@Override
public void onClick(View v) {
APIHelper helper = new APIHelper();
apiService = helper.getApiService();
Call<Partners> call = apiService.getPartners();
call.enqueue(new Callback<Partners>() {
@Override
public void onResponse(Call<Partners> call, Response<Partners> response) {
Call<Partners> call = apiService.getPartners();
call.enqueue(new Callback<Partners>() {
@Override
public void onResponse(Call<Partners> call, Response<Partners> response) {
List<String> partners = response.body().getPartners();
Log.d(TAG, "Number of partners received: " + partners.size());
}
@Override
public void onFailure(Call<Partners> call, Throwable t) {
Toast.makeText(getActivity(), "FAIL!!!", Toast.LENGTH_SHORT).show();
}
});
}
});
@Override
public void onResponse(Call<Partners> call, Response<Partners> response) {
}
@Override
public void onFailure(Call<Partners> call, Throwable t) {
}
}
Here the onResponse()
method throws me an error: Attempt to invoke virtual method 'java.util.List com.example...pojo.Partners.getPartners()' on a null object reference
What I want is to get all partners from web and save it into my Partners table on button click.
I can't find solution to this error so if someone could help me I've would be grateful.
Question: Could anybody guide me and tell me what is wrong and help me to fix this?