I was trying to parse a Json
like the following:
{
"status": "ok",
"feed": {
"title": "title1",
"link": "http://www.link/",
"author": "",
"description": "desc",
"image": ""
},
"items": [
{
"title": "something",
"link": "something",
"guid": "something",
"pubDate": "date",
"categories": [],
"author": "",
"thumbnail": "",
"description": null,
"content": null
},
...
...
And i'm getting the expected results from response
but, that's not the point.Here is what i did:
try {
JSONObject response = new JSONObject(result);
JSONObject feed_object = response.getJSONObject("feed");
PostItems item = new PostItems();
item.setAuthor(feed_object.getString("title"));
JSONArray items_arr = response.getJSONArray("items");
for (int i = 0; i < items_arr.length(); i++) {
JSONObject post = items_arr.getJSONObject(i);
item.setTitle(post.getString("title"));
item.setDate(post.getString("pubDate")); // As expected results
feedItem.add(item);
}
}
The point is, if i store the ( first object
) like what i did so far as above codes , the RecyclerView
is showing the items but without setting the correct title
and date
inside the items.
And if i store it inside the array and after that, setting one inside the array(for array
) and outside the array
, it's just showing an error like this:
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
at java.io.StringReader.<init>(StringReader.java:47)
at android.text.HtmlToSpannedConverter.convert(Html.java:449)
at android.text.Html.fromHtml(Html.java:136)
at android.text.Html.fromHtml(Html.java:99)
at client.com.news.RecyclerViews.MainSanjeshRecyclerView.MyRecyclerAdapter.onBindViewHolder(MyRecyclerAdapter.java:34)
at client.com.news.RecyclerViews.MainSanjeshRecyclerView.MyRecyclerAdapter.onBindViewHolder(MyRecyclerAdapter.java:14)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5465)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5498)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4735)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4611)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1988)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1384)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1347)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3026)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2903)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3277)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:596)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1684)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:122)
at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1139)
at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:810)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171)
at android.vi
The second try:
try {
JSONObject response = new JSONObject(result);
JSONObject feed_object = response.getJSONObject("feed");
PostItems item = new PostItems();
item.setAuthor(feed_object.getString("title"));
JSONArray items_arr = response.getJSONArray("items");
for (int i = 0; i < items_arr.length(); i++) {
JSONObject post = items_arr.getJSONObject(i);
PostItems item2 = new PostItems();
item2.setTitle(post.getString("title"));
item2.setDate(post.getString("pubDate"));
feedItem.add(item2);
}
feedItem.add(item);
}
Actually, i've tried anything you can even imagine it and saw any questions on stackoverflow and i couldn't find something like this questiion, but one time, just showing one item without correct data from object and one time without showing the correct datas from that Array
.
Any helps would be great.
You are trying to use getAuthor()
in every item when clearly it was set for only one (first) item in the ArrayList
. When it doesn't have any value it return null
, resulting NullPointerException
. Also new object should be created at every iteration.
Just move
PostItems item = new PostItems();
item.setAuthor(feed_object.getString("title"));
inside for
loop.
Edit
try {
JSONObject response = new JSONObject(result);
JSONObject feed_object = response.getJSONObject("feed");
JSONArray items_arr = response.getJSONArray("items");
for (int i = 0; i < items_arr.length(); i++) {
JSONObject post = items_arr.getJSONObject(i);
PostItems item = new PostItems();
item.setAuthor(feed_object.getString("title")); // being fetched from top node
item.setTitle(post.getString("title")); // being fetched from array
item.setDate(post.getString("pubDate")); // being fetched from array
feedItem.add(item);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.