android - Java inner class new instance not being created -
i have java class going have number of inner classes. done organization , keep things in separate file.
public class pucobjects { public static class pucnewsitem { public string title; public string summary; public string body; public string url; public string imageurl; } } i trying create new instance of inner class (doing in class parses remote xml), reason doesn't seem created:
public static arraylist<pucobjects.pucnewsitem> getpucnews() throws ioexception { string url = "http://api.puc.edu/news/list?key="+api_key+"&count=30"; inputstream = downloadurl(url); xmlpullparserfactory pullparserfactory; try { pullparserfactory = xmlpullparserfactory.newinstance(); xmlpullparser parser = pullparserfactory.newpullparser(); parser.setinput(is, null); arraylist<pucobjects.pucnewsitem> items = null; int eventtype = parser.geteventtype(); pucobjects.pucnewsitem item = null; log.d("debug: ", "start: "+url); while (eventtype != xmlpullparser.end_document){ string name = null; switch (eventtype){ case xmlpullparser.start_document: items = new arraylist<pucobjects.pucnewsitem>(); break; case xmlpullparser.start_tag: name = parser.getname(); //log.d("start tag name: ", parser.getname()+" === "+name); if (name == "item"){ log.d("debug: ", "item"); item = new pucobjects.pucnewsitem(); } else if (item != null){ log.d("debug: ", "item not null 2"); if (name == "title"){ log.d("title: ", parser.nexttext()); item.title = parser.nexttext(); } else if (name == "summary"){ item.summary = parser.nexttext(); } else if (name == "body_text"){ item.body = parser.nexttext(); } } break; case xmlpullparser.end_tag: name = parser.getname(); if (name.equalsignorecase("item") && item != null) { log.d("debug: ", "add item"); items.add(item); } break; }//end switch eventtype = parser.next(); }//end while log.d("debug: ", "done"); return items; } catch (xmlpullparserexception e) { e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } return null; }//end i trying create object item = new pucobjects.pucnewsitem(); seems null.
is there reason why object isn't getting created?
problem string comparison. if statement not resulting true due == check.
if (name == "item"){ you need use equals() method instead of == when comparing objects/strings. read thread more information on eqauals() vs ==
Comments
Post a Comment