Reputation: 23
I am making an android app that downloads an xml file and creates a list view where the downloaded data are shown. The app was working fine. Suddenly the screen appears empty. I did not make any change. I have a variable that shows the length of the arrayadapter and it appears to be 0. The logcat shows a NullPointerException in the file below:
package com.makemyandroidapp.example.stacksites;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import android.content.Context;
public class SitesXmlPullParser {
/*static final String KEY_SITE = "site";
static final String KEY_NAME = "name";
static final String KEY_LINK = "link";
static final String KEY_ABOUT = "about"; */
static final String KEY_SITE = "pozicioni";
static final String KEY_KOMPANIA = "kompania";
static final String KEY_POZICIONI = "pozicioni";
static final String KEY_KATEGORIA = "kategoria";
static final String KEY_QYTETI = "qyteti";
static final String KEY_IMAGE_URL = "image";
public static List<StackSite> getStackSitesFromFile(Context ctx) {
// List of StackSites that we will return
List<StackSite> stackSites;
stackSites = new ArrayList<StackSite>();
// temp holder for current StackSite while parsing
StackSite curStackSite = null;
// temp holder for current text value while parsing
String curText = "";
try {
// Get our factory and PullParser
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser xpp = factory.newPullParser();
// Open up InputStream and Reader of our file.
FileInputStream fis = ctx.openFileInput("StackSites.xml");
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
// point the parser to our file.
xpp.setInput(reader);
// get initial eventType
int eventType = xpp.getEventType();
boolean done = false;
int count=0;
// Loop through pull events until we reach END_DOCUMENT
while (eventType != XmlPullParser.END_DOCUMENT) {
// Get the current tag
String tagname = xpp.getName();
// React to different event types appropriately
switch (eventType) {
case XmlPullParser.START_TAG:
if (tagname.equalsIgnoreCase(KEY_SITE)&&count==0) {
// If we are starting a new <site> block we need
//a new StackSite object to represent it
curStackSite = new StackSite();
count=1;
System.out.println(count);
}
break;
case XmlPullParser.TEXT:
{String s="";
String href="";
s=xpp.getText(); //in case of cdsect this is null otherwise you get the text right here already
if (s==null) { //what would happen if it encounters in fact a cdsect
int event=xpp.nextToken(); //this is the main technical important line
if (event==XmlPullParser.CDSECT) {
s=xpp.getText();
}
}
curText=s;
break;}
case XmlPullParser.END_TAG:
if (tagname.equalsIgnoreCase(KEY_SITE)&&count==1) {
// if </site> then we are done with current Site
// add it to the list.
count=0;
System.out.println(curText);
curStackSite.setPozicioni(curText);
}
else if (tagname.equalsIgnoreCase(KEY_SITE)&&count==0){
stackSites.add(curStackSite);
} else if (tagname.equalsIgnoreCase(KEY_KOMPANIA)) {
// if </name> use setName() on curSite
//curStackSite.setKompania(curText);
System.out.println(curText+"kooooooooooooooooooooooooooooooooooooooot");
curStackSite.setKompania(curText);
System.out.println(curText+"kooooooooooooooooooooooooooooooooooooooot");
} else if (tagname.equalsIgnoreCase(KEY_KATEGORIA)) {
// if </about> use setAbout() on curSite
curStackSite.setKategoria(curText);
} else if (tagname.equalsIgnoreCase(KEY_QYTETI)){
curStackSite.setQyteti(curText);
}
break;
default:
break;
}
//move on to next iteration
eventType = xpp.next();
}
}
catch (Exception e) {
e.printStackTrace();
}
// return the populated list.
return stackSites;
}
}
NullPointerException occurs in this line:
curStackSite.setKompania(curText);
Please help me! Thanks in advance!
Upvotes: 0
Views: 248
Reputation: 1925
I'd say either to try and instancing an object and then "nulling it", before your try statement or making sure that curStackSite = new StackSite(); Is reachable You have it within a case within switch, so it may not always come to it. Which means the reference may fail if the object wasn't instanced.
Upvotes: 1