簡體   English   中英

android中的java.lang NullPointerException

[英]java.lang NullPointerException in android

我正在制作一個可下載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);

請幫我! 提前致謝!

我想說的是,在您的try語句之前嘗試實例化一個對象,然后“清空它”,或者確保curStackSite = new StackSite();。 可以到達您將其放在交換機內的外殼中,因此可能並不總是可以使用它。 這意味着如果未實例化對象,則引用可能會失敗。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM