i looking create list json content. here adapter code :
public class answersadapter extends arrayadapter<answer> { static context context; static int layoutresourceid; answer data[] = null; public answersadapter(context context, int layoutresourceid, answer[] data) { super(context, layoutresourceid, data); this.layoutresourceid = layoutresourceid; this.context = context; this.data = data; } public long getitemid(int position) { return position; } public view getview(int position, view convertview, viewgroup parent) { view row = convertview; answerholder holder = null; if(row == null) { layoutinflater inflater = ((activity)context).getlayoutinflater(); row = inflater.inflate(layoutresourceid, parent, false); holder = new answerholder(); holder.txttitle = (textview)row.findviewbyid(r.id.answertext); holder.txttitle2 = (textview)row.findviewbyid(r.id.answerauthor); holder.txttitle3 = (textview)row.findviewbyid(r.id.answervotes); row.settag(holder); } else { holder = (answerholder)row.gettag(); } answer hold = data[position]; holder.txttitle.settext(html.fromhtml(hold.text)); holder.txttitle2.settext(hold.author); holder.txttitle3.settext(hold.votes); return row; } static class answerholder { textview txttitle; textview txttitle2; textview txttitle3; } }
and facing error :
java.lang.nullpointerexception: attempt read field 'java.lang.string com.example.shivam.stackoverflow.answer.text' on null object reference
on line
holder.txttitle.settext(html.fromhtml(hold.text));
your data
array has null values. immediate cause of npe, because hold
ends null
positions. avoid npe if test that, (as point out in comment) adapter won't behave quite properly. cannot ignore rows have null
data values.
as aside, should eliminate data
member field. array reference being kept arrayadapter
superclass. use getitem(position)
whenever have used data[position]
. while won't address npe problem, simplify things bit, seen below.
you have several options:
- find out why entries of
data
null
begin , eliminate root problem. however, may normal (outside context of adapter)data
havenull
entries. - revise
getview
method set views inholder
specific values indicate "data missing"—perhaps blank, perhaps "no data" message. (in other words, don't skip processing. causing combination of blank , duplicate rows.) revise constructor eliminate
null
entriesdata
. result in shorter list. code accomplish might be:public answersadapter(context context, int layoutresourceid, answer[] data) { super(context, layoutresourceid, trimmeddata(data)); this.layoutresourceid = layoutresourceid; this.context = context; } /** * copy data new array has no nulls. */ private static answer[] trimmeddata(answer[] data) { answer[] result = new answer[data.length]; int n = 0; (answer : data) { if (a != null) { result[n++] = a; } } if (n < data.length) { result = arrays.copyof(result, n, answer.class); } return result; }
however, might cause problems elsewhere in code if need maintain correspondence between
listview
position , originaldata
array.
another aside: can eliminate getitemid()
method; default implementation in arrayadapter
does same thing.