我正在使用改造功能来解析android中的数据,但始终收到空指针异常

时间:2018-09-05 11:39:03

标签: android retrofit

代码如下:

请求接口类

import retrofit2.Call;
import retrofit2.http.GET;

public interface RequestInterface {

    String BASE_URL = "https://newsapi.org/v2/";

    @GET("top-headlines?sources=google-news&apiKey=3709c816cdcb4eb38b7e45c9829a37d7\n")
    Call<NewsList> getJSON();
}

对象类

public class News {
    private Source source;
    private String author;
    private String title;
    private String description;
    private String url;
    private String urlToImage;
    private String publishedAt;

    public class Source{
        private String id;
        private String name;
        public Source(String id, String name) {
            this.id = id;
            this.name = name;
        }

        public String getId() {
            return id;
        }

        public String getName() {
            return name;
        }

        public void setId(String id) {
            this.id = id;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

    public News(Source source, String author, String title, String description, String url, String urlToImage, String publishedAt) {
        this.source = source;
        this.author = author;
        this.title = title;
        this.description = description;
        this.url = url;
        this.urlToImage = urlToImage;
        this.publishedAt = publishedAt;
    }

    public Source getSource() {
        return source;
    }

    public String getAuthor() {
        return author;
    }

    public String getTitle() {
        return title;
    }

    public String getDescription() {
        return description;
    }

    public String getUrl() {
        return url;
    }

    public String getUrlToImage() {
        return urlToImage;
    }

    public String getPublishedAt() {
        return publishedAt;
    }

    public void setSource(Source source) {
        this.source = source;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public void setUrlToImage(String urlToImage) {
        this.urlToImage = urlToImage;
    }

    public void setPublishedAt(String publishedAt) {
        this.publishedAt = publishedAt;
    }
}

对象列表类

import java.util.ArrayList;

public class NewsList {
    private ArrayList<News> news = new ArrayList<>();

    public ArrayList<News> getNews() {
        return news;
    }

    public void setNews(ArrayList<News> news) {
        this.news = news;
    }
}

适配器类

    import android.content.Context;
    import android.support.annotation.NonNull;
    import android.support.v7.widget.CardView;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import android.widget.TextView;

    import com.squareup.picasso.Picasso;

    import java.util.ArrayList;

    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{
        private ArrayList<News> news;
        private Context context;

        public MyAdapter(Context context, ArrayList<News> news){
            this.news = news;
            this.context  = context;
        }

        public News getItem(int i){
            return news.get(i);
        }
        @NonNull
        @Override
        public MyAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.news_card, viewGroup, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull MyAdapter.ViewHolder viewHolder, int i) {
     //       viewHolder.article_source.setText(news.get(i).getSource());
            viewHolder.article_author.setText(news.get(i).getAuthor());
            viewHolder.article_title.setText(news.get(i).getTitle());
            viewHolder.article_description.setText(news.get(i).getDescription());
            viewHolder.article_url.setText(news.get(i).getUrl());
            viewHolder.article_urlToImage.setText(news.get(i).getUrlToImage());
            viewHolder.article_publishedAt.setText(news.get(i).getPublishedAt());

            Picasso.with(context).load(news.get(i).getUrlToImage()).resize(120,60).into(viewHolder.article_image);
        }

        @Override
        public int getItemCount() {
            return news.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder{

             ImageView article_image;
             TextView article_source, article_author, article_title,
                    article_description, article_url, article_urlToImage,
                    article_publishedAt;

            public ViewHolder (@NonNull View itemView) {
                super(itemView);
                article_image = (ImageView) itemView.findViewById(R.id.article_image);
                article_source = (TextView) itemView.findViewById(R.id.article_source);
                article_author = (TextView) itemView.findViewById(R.id.article_author);
                article_title = (TextView) itemView.findViewById(R.id.article_title);
                article_description = (TextView) itemView.findViewById(R.id.article_description);
                article_url = (TextView) itemView.findViewById(R.id.article_url);
                article_urlToImage = (TextView) itemView.findViewById(R.id.article_urlToImage);
                article_publishedAt = (TextView) itemView.findViewById(R.id.article_publishedAt);
            }
        }
}

主要活动

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private ArrayList<News> newsArrayList;
    private  MyAdapter myAdapter;
    private Context context;
    NewsList newsList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initviews();
    }
    private void initviews(){
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(layoutManager);
        loadJSON();
    }

    private void loadJSON(){
        newsArrayList = new ArrayList<>();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(RequestInterface.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RequestInterface requestInterface =retrofit.create(RequestInterface.class);
        Call<NewsList> call = requestInterface.getJSON();
        call.enqueue(new Callback<NewsList>() {
            @Override
            public void onResponse(Call<NewsList> call, Response<NewsList> response) {
           //     newsArrayList = response.body().getNews();

       //         Log.i(MainActivity.class.getSimpleName().toString(),"@@@@@@@@@ArrayList: "+newsArrayList);
               newsArrayList = new ArrayList<>((newsList.getNews()));

                myAdapter = new MyAdapter(context, newsArrayList);
                recyclerView.setAdapter(myAdapter);
            }

            @Override
            public void onFailure(Call<NewsList> call, Throwable t) {
                Log.d("Error", t.getMessage());
            }
        });
    }
}

我在做什么错?我一直在这行中得到一个空指针异常:

newsArrayList = new ArrayList<>((newsList.getNews()))

错误消息:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.ArrayList com.example.dell.myapi.NewsList.getNews()' on a null object reference
        at com.example.dell.myapi.MainActivity$1.onResponse(MainActivity.java:57)

3 个答案:

答案 0 :(得分:0)

尝试此操作,您在末尾(\ n)传递了错误的网址,请删除并尝试如下操作

  @GET("top-headlines?sources=google-news&apiKey=3709c816cdcb4eb38b7e45c9829a37d7")

更改也能听到

   call.enqueue(new Callback<NewsList>() {
        @Override
        public void onResponse(Call<NewsList> call, Response<NewsList> response) {



           newsArrayList = new ArrayList<>();
           newsArrayList.add(response.body().getNews());

            myAdapter = new MyAdapter(context, newsArrayList);
            recyclerView.setAdapter(myAdapter);
        }

        @Override
        public void onFailure(Call<NewsList> call, Throwable t) {
            Log.d("Error", t.getMessage());
        }
    });
}

答案 1 :(得分:0)

您尚未初始化newsList并尝试获取数据。

尝试将结果分配给newsList,然后尝试。

@Override
public void onResponse(Call<NewsList> call, Response<NewsList> response) {
    Log.i(MainActivity.class.getSimpleName().toString(),"Response : "+response.body().toString());
    newsList = response.body().getNews();// assign the value from response
    newsArrayList = new ArrayList<>((newsList.getNews()));
    myAdapter = new MyAdapter(context, newsArrayList);
    recyclerView.setAdapter(myAdapter);
}

答案 2 :(得分:0)

首先,您的网址不正确。删除网址末尾的\n。 其次,您的模型不正确。例如,NewsList.java没有类似articles的字段。使用this link创建模型。您的NewsList.java必须是这样

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class NewsList {

@SerializedName("status")
@Expose
private String status;
@SerializedName("totalResults")
@Expose
private Integer totalResults;
@SerializedName("articles")
@Expose
private List<News> articles = null;

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public Integer getTotalResults() {
return totalResults;
}

public void setTotalResults(Integer totalResults) {
this.totalResults = totalResults;
}

public List<News> getArticles() {
return articles;
}

public void setArticles(List<News> articles) {
this.articles = articles;
}

}