-4

I'm trying to use blog id to get JSON object from server. At the moment I'm getting this error

java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator()

on a null object reference`.How do I use the post Id to get the full content what am I doing wrong. Please help!!!

MyBlog Adapter:

public class MyBlogAdapter extends RecyclerView.Adapter<BlogViewHolder> {
    List<BlogResponse> postsList;
    Context context;
    public static String blog_Id, blogID;
    private LayoutInflater inflater;

    public MyBlogAdapter(Context context, List<BlogResponse> postsList){
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.postsList = postsList;
    }

    @Override
    public BlogViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.custom_view,parent, false);
        return new BlogViewHolder(view);
    }

    @Override
    public void onBindViewHolder(BlogViewHolder holder, int position) {
        final BlogResponse posts= postsList.get(position);
        holder.summary.setText(posts.getBlogExcerpt().trim().toString());
        holder.title.setText(posts.getBlogTitle().trim().toString());
        //  Glide.with(context).load(posts.getBlogThumbnail()).into(holder.cover);

        holder.blogHolder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, AnotherSingleView.class);
                blog_Id = posts.getBlogId();
                intent.putExtra(blogID,blog_Id);
                Log.d("MyblogAdapter","Please check blog Id: "+blog_Id);

                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        Log.d("MyBlogAdapter,","getItemCount"+postsList.size());

        return postsList == null ? (0) : postsList.size();
    }
}

SecondActivity:

public class AnotherSingleView extends AppCompatActivity {
    String postID;
    int position;
    public TextView blogTitle,blogSub,blogContent;
    public ImageView blogPic;
    List<SingleBlogPost> singleBlogPosts;
    SingleBlogPost singleBlogPost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_another_single_view);


        Intent intent  = getIntent();
        Bundle showBlogId = intent.getExtras();
        postID = showBlogId.getString(blogID);
        Log.d("AnotherSingleView","Please check blog Id: "+postID);

        singleBlogPosts = new ArrayList<>();

        blogContent = (TextView)findViewById(R.id.blog_content);
        blogSub = (TextView) findViewById(R.id.blog_subtitle);
        blogTitle =(TextView) findViewById(R.id.blog_title);
        blogPic =(ImageView) findViewById(R.id.blog_pix);
        singlePostDisplay();

    }

private void singlePostDisplay() {
    BlogaPI api = ApiClient.getBlogInterface();
    Call<List<SingleBlogPost>> call = api.postResponse(postID);
    call.enqueue(new Callback<List<SingleBlogPost>>() {
        @Override
        public void onResponse(Call<List<SingleBlogPost>> call, Response<List<SingleBlogPost>> response) {
            singleBlogPosts = response.body();
            if (singleBlogPosts != null && !singleBlogPosts.isEmpty() ){
                for (SingleBlogPost posts : singleBlogPosts){
                    Log.d("AnotherSingleView","Please check RESPONSE: "+response.body().toString());
                    blogTitle.setText(posts.getBlogTitle());
                    blogSub.setText(posts.getBlogSubtitle());
                    blogContent.setText(posts.getBlogContent());
                    // Glide.with(AnotherSingleView.this).load(singlepost.getBlogMedimg()).into(blogPic);
                }
            }
            else{
                Toast.makeText(AnotherSingleView.this, "Something is empty", Toast.LENGTH_SHORT).show();
            }            }

            @Override
            public void onFailure(Call<List<SingleBlogPost>> call, Throwable t) {
                Toast.makeText(AnotherSingleView.this, "check again: "+t.toString(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}

Interface:

public interface BlogaPI {
    @GET("blog")
    Call<BlogList> response();

    @GET("post/{blog_id}")
    Call<List<SingleBlogPost>> postResponse(@Path("blog_id") String blog_id);
    //Call<List<SingleBlogPost>> postResponse();
}

SingleBlogPost:

public class SingleBlogPost {

@SerializedName("blog_id")
@Expose
private String blogId;
@SerializedName("blog_title")
@Expose
private String blogTitle;
@SerializedName("blog_subtitle")
@Expose
private String blogSubtitle;
@SerializedName("blog_excerpt")
@Expose
private String blogExcerpt;
@SerializedName("blog_content")
@Expose
private String blogContent;
@SerializedName("blog_thumbnail")
@Expose
private String blogThumbnail;
@SerializedName("blog_medimg")
@Expose
private String blogMedimg;
@SerializedName("category_title")
@Expose
private String categoryTitle;
public SingleBlogPost(String blogId,String blogTitle, String blogSubtitle, String blogExcerpt,
                      String blogContent, String blogThumbnail, String blogMedimg, String categoryTitle){
    this.blogId = blogId;
    this.blogTitle = blogTitle;
    this.blogSubtitle = blogSubtitle;
    this.blogExcerpt = blogExcerpt;
    this.blogContent = blogContent;
    this.blogThumbnail = blogThumbnail;
    this.blogMedimg = blogMedimg;
    this.categoryTitle = categoryTitle;
}

public String getBlogId() {
    return blogId;
}

public void setBlogId(String blogId) {
    this.blogId = blogId;
}

public String getBlogTitle() {
    return blogTitle;
}

public void setBlogTitle(String blogTitle) {
    this.blogTitle = blogTitle;
}

public String getBlogSubtitle() {
    return blogSubtitle;
}

public void setBlogSubtitle(String blogSubtitle) {
    this.blogSubtitle = blogSubtitle;
}

public String getBlogExcerpt() {
    return blogExcerpt;
}

public void setBlogExcerpt(String blogExcerpt) {
    this.blogExcerpt = blogExcerpt;
}

public String getBlogContent() {
    return blogContent;
}

public void setBlogContent(String blogContent) {
    this.blogContent = blogContent;
}

public String getBlogThumbnail() {
    return blogThumbnail;
}

public void setBlogThumbnail(String blogThumbnail) {
    this.blogThumbnail = blogThumbnail;
}

public String getBlogMedimg() {
    return blogMedimg;
}

public void setBlogMedimg(String blogMedimg) {
    this.blogMedimg = blogMedimg;
}

public String getCategoryTitle() {
    return categoryTitle;
}

public void setCategoryTitle(String categoryTitle) {
    this.categoryTitle = categoryTitle;
}

}
HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
Ekenne Chilex
  • 45
  • 2
  • 8
  • 2
    Can't say for sure without a full Logcat, but given that the only iterator I can find in your code is `for (SingleBlogPost posts : singleBlogPosts)`, I'd say that `singleBlogPosts` is `null`. Check that `response.body()` is not empty. – Michael Dodd Mar 31 '17 at 15:12
  • 3
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Michael Dodd Mar 31 '17 at 15:13

2 Answers2

1

Check if the server response is different then null before you do a enhanced for like this:

if(singleBlogPosts!= null) {
    for (SingleBlogPosts posts : singleBlogPosts){
        Log.d("AnotherSingleView","Please check RESPONSE: "+response.body().toString());
        blogTitle.setText(posts.getBlogTitle());
        blogSub.setText(posts.getBlogSubtitle());
        blogContent.setText(posts.getBlogContent());
        // Glide.with(AnotherSingleView.this).load(singlepost.getBlogMedimg()).into(blogPic);
    }
}
Khaled H
  • 150
  • 7
Luiz Fernando Salvaterra
  • 4,192
  • 2
  • 24
  • 42
  • Change your `if` statements to use `singleBlogPost` (lowercase s). You want to reference the instance, not the class. – Michael Dodd Mar 31 '17 at 15:16
  • Still not quite right. Can't suggest an edit at the moment as the queue is full, but it should be `if (singleBlogPosts != null && !singleBlogPosts.isEmpty()) {` followed by `for (SingleBlogPost posts : singleBlogPosts)` – Michael Dodd Mar 31 '17 at 15:20
  • !singleBlogPosts.isEmpty() is not required, a redundant check – Khaled H May 16 '17 at 07:27
-2

you question was list iterator,but we can`t see you list use on anywhere,i guess you may be use recyclerView on you data has not get,because get data was asynchronous.try to use data if you can sure it is existing.

yufa
  • 1
  • 1
  • `iterator` in this case refers to looping through a `List` object, not a `ListView`. See Luiz's answer. – Michael Dodd Mar 31 '17 at 15:25
  • I added ' if (singleBlogPosts != null && !singleBlogPosts.isEmpty() ){ for (SingleBlogPost posts : singleBlogPosts){ } } else{ Toast.makeText(AnotherSingleView.this, "Something is empty", Toast.LENGTH_SHORT).show(); }' and am getting the else statement. what could be wrong – Ekenne Chilex Mar 31 '17 at 15:31
  • @ekennechilex Better to post this as a comment on your original question. It means that you're receiving a `null` or empty response i.e. your request is failing. – Michael Dodd Mar 31 '17 at 15:35
  • but I don't know how to fix it.. please could you help? – Ekenne Chilex Mar 31 '17 at 15:45
  • You would need to debug that yourself. There's too much that could be going wrong, maybe you're sending an invalid request, maybe the blog's API is not working, maybe the URL is wrong. We can only deal with specific problems, and your original question has been answered by @Luiz. Narrow down the issue as best as you can, then open a new question. – Michael Dodd Mar 31 '17 at 17:52