java - NullPointerException when trying to convert list back into an array -
i'm getting npe when trying convert list array. debugged through , found list getting value null.
why happening , more importantly how prepare issue?
list<string> attrlist = new linkedlist<string>(arrays.aslist(attrarray)) //i loop through , remove unnecessary elements attrarray = attrlist.toarray(attrarray); //next line uses attrarray , throwing npe. here's found through debugging, attrlist = [1, 2, 3] attrarray = [1, 2, 3, null]
try replacing
attrarray = attrlist.toarray(attrarray);
with
attrarray = attrlist.toarray(new string[attrlist.size()]);
i think work, because have right is
list<string> attrlist = new linkedlist<string>(arrays.aslist(attrarray)); // loop through , remove unnecessary elements attrarray = attrlist.toarray(attrarray);
and javadoc of list#toarray(t[] a)
states (highlights me):
if list fits in specified array room spare (i.e., array has more elements list), element in array next end of list set null
. (this useful in determining length of list if caller knows list not contain null elements.)
java arrays list nullpointerexception
No comments:
Post a Comment