-1

my problem is, that I always get a nullpointerexception in my retrofit request, but I have no idea why...

Feedis a own created class which should contain the response from the retrofit request.

ServiceAPI:

@GET("top-headlines?country=de&apiKey=xxx")
Call<Feed> getNewsFeed();

Retrofit-Call in onCreateView:

Retrofit retrofitNewsData = new Retrofit.Builder()
            .baseUrl(NetworkService.NEWS_DATA_BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    Log.d(TAG, "network service connected to: " + retrofitNewsData);

    ServiceRepository serviceRepositoryNewsData = retrofitNewsData.create(ServiceRepository.class);

    Call<Feed> callNews = serviceRepositoryNewsData.getNewsFeed();
    callNews.enqueue(new Callback<Feed>() {
        @Override
        public void onResponse(Call<Feed> call, Response<Feed> response) {
             newsFeed = response.body();
            Log.d(TAG, "Habe den feed: " + newsFeed + " erhalten." + response.code());
        }

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

        }
    });`

Base-URL: public static final String NEWS_DATA_BASE_URL = "https://newsapi.org/v2/";

Alec Sanger
  • 4,442
  • 1
  • 33
  • 53
Andreas Teich
  • 349
  • 1
  • 2
  • 11
  • 1
    Which line is throwing the NPE? `response.body()`? – em_ Jul 25 '18 at 14:09
  • No the problem occures a few lines after I initialize the newFeed object. Sry for the bad format, but: HSNewsAdapter hsNewsAdapter = new HSNewsAdapter(newsFeed.getArticles()); allNews.setAdapter(hsNewsAdapter); (allNews is a object of type recyclerview) – Andreas Teich Jul 25 '18 at 14:19

1 Answers1

-1

You need to use a another type. If your body can be empty you should to use Response, and parse response body.