android中的java.lang NullPointerException

时间:2014-04-24 12:25:18

标签: java android xml nullpointerexception

我正在创建一个下载xml文件的Android应用程序,并创建一个列表视图,显示下载的数据。该应用程序运行正常。突然屏幕显示为空。我没有做任何改变。我有一个变量,显示arrayadapter的长度,它似乎是0. logcat在下面的文件中显示NullPointerException:

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:

curStackSite.setKompania(curText);

请帮帮我!提前谢谢!

1 个答案:

答案 0 :(得分:1)

我要么试着尝试对一个对象进行实例化,然后在你的try语句之前将它归零#34;或者确保         curStackSite = new StackSite(); 可以到达 你有一个在开关内的情况下,所以它可能并不总是来。这意味着如果对象没有实例化,引用可能会失败。