android - Handling Null Pointers from JsonReader -


i using jsonreader fetch lots of data website , saving db. incidentally, whenever reader doesnt find value object item, fails , stops executing.

this error have;

system.err﹕ java.lang.illegalstateexception: expected string null @ line 1 column 359337 path $[19].date 

even if specified value not available, seems objects loaded before error encountered lost , not saved db. there way handle these errors , keep objects had been loaded far? have set jsonreader lenient.

this part of method parses data:

private post read(jsonreader reader) throws exception {             reader.beginobject();              while (reader.hasnext()) {                 string name = reader.nextname();                  switch (name) {                     case title:                         mtitle = new stringbuilder();                         mtitle.append(reader.nextstring());                         break;                     case author:                         if (reader.hasnext()) {                             jsontoken peek = reader.peek();                              if (peek == jsontoken.null) {                                 reader.skipvalue();                             } else {                                 reader.beginobject();                                  while (reader.hasnext()) {                                     string enclosedname = reader.nextname();                                      switch (enclosedname) {                                         case name:                                             mauthor = new stringbuilder();                                             mauthor.append(reader.nextstring());                                             break;                                         case avatar:                                             mavatar = new stringbuilder();                                             mavatar.append(reader.nextstring());                                             break;                                         default:                                             reader.skipvalue();                                             break;                                     }                                 }                                  reader.endobject();                             }                         }                         break;                     case content:                         mdescription = new stringbuilder();                         mdescription.append(reader.nextstring());                         break;                     case url:                         mentrylink = new stringbuilder();                         mentrylink.append(reader.nextstring());                         break;                     case date:                         mdatestringbuilder = new stringbuilder();                         mdatestringbuilder.append(reader.nextstring());                         break;                     case guid:                         mguid = new stringbuilder();                         mguid.append(reader.nextstring());                         break;                     case featured_image:                         if (reader.hasnext()) {                             jsontoken = reader.peek();                              if (look == jsontoken.null) {                                 reader.skipvalue();                             } else {                                 mfeaturedimage = new stringbuilder();                                 reader.beginobject();                                  while (reader.hasnext()) {                                     string itemtolookfor = reader.nextname();                                      switch (itemtolookfor) {                                         case source:                                             mfeaturedimage.append(reader.nextstring());                                             break;                                         default:                                             reader.skipvalue();                                             break;                                     }                                 }                                  reader.endobject();                             }                         }                         break;                     case terms:                         if (reader.hasnext()) {                             jsontoken check = reader.peek();                              if (check == jsontoken.null) {                                 reader.skipvalue();                             } else {                                 reader.beginobject();                                  while (reader.hasnext()) {                                     string stuff = reader.nextname();                                      switch (stuff) {                                         case categories:                                             reader.beginarray();                                              itemcategory = new stringbuilder();                                              while (reader.hasnext()) {                                                 reader.beginobject();                                                  while (reader.hasnext()) {                                                     string item = reader.nextname();                                                      switch (item) {                                                         case name:                                                             itemcategory.append(reader.nextstring()).append(",");                                                             break;                                                         default:                                                             reader.skipvalue();                                                             break;                                                     }                                                 }                                                  reader.endobject();                                             }                                             reader.endarray();                                             break;                                         case tags:                                             reader.beginarray();                                              itemtags = new stringbuilder();                                              while (reader.hasnext()) {                                                 reader.beginobject();                                                  while (reader.hasnext()) {                                                     string item = reader.nextname();                                                      switch (item) {                                                         case name:                                                             itemtags.append(reader.nextstring()).append(",");                                                             break;                                                         default:                                                             reader.skipvalue();                                                             break;                                                     }                                                 }                                                  reader.endobject();                                             }                                             reader.endarray();                                             break;                                         default:                                             reader.skipvalue();                                             break;                                     }                                 }                                  reader.endobject();                             }                         }                         break;                     default:                         reader.skipvalue();                         break;                 }             }              reader.endobject();              return new post(mauthor.tostring(), mavatar.tostring(), mdatestringbuilder.tostring(), mtitle.tostring(), mentrylink.tostring(), mdescription.tostring(), mguid.tostring(), mfeaturedimage.tostring(), itemtags.tostring(), itemcategory.tostring());         } 

i thought may weren't checking null elements, seems case.

at 1 point use:

jsontoken check = reader.peek();  if (check == jsontoken.null) { 

that correct. however, in other places string:

mdescription.append(reader.nextstring()); 

in places getting string, need check whether there unexpected type (such null). in cases this, need apply appropriate error containment code.

incidentally, don't need creating of stringbuilders. since putting 1 string after each initialization. save lot of time using string. if intend write contents stringbuilder each type, need initialize them before start looping.


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -