XMLPullParser导致空对象引用错误

时间:2015-11-10 13:35:27

标签: equals xmlpullparser

我有两个课程,创作者和亨特。 Hunt是一个只有String名称的简单类,如下所示:

public class Hunt {

    private String name;

    public Hunt() {

    }

    public Hunt(String name) {
        this.name = name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

}

Creator类由String名称标识,并存储Hunt对象的ArrayList。我已经覆盖了.equals方法,以便我可以通过String名称来比较Creator对象,因为我不希望有任何重复的名称。

import java.util.ArrayList;

public class Creator {

    private String name;
    private ArrayList<Hunt> hunts = new ArrayList<>();

    public Creator() {

    }

    public Creator(String name) {
        this.name = name;
    }

    public void addHunt(Hunt hunt) {
        hunts.add(hunt);
    }

    public ArrayList<Hunt> getHunts() {
        return hunts;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public boolean equals(Object object) {
        return object instanceof Creator && name.equalsIgnoreCase(((Creator) object).getName());
    }

}

最后,我有一个函数是我从Web服务读取XML文件并使用它来创建Creator和Hunt对象的主要活动。

    private class HuntListReaderTask extends AsyncTask<URL, Void, ArrayList<Creator>> {

        private String statusMessage;
        private Hunt hunt;
        private Creator creator;
        private String text;
        ArrayList<Creator> creators = new ArrayList<>();

        @Override
        protected ArrayList<Creator> doInBackground(URL... params) {
            statusMessage = "AsyncTask is working";
            statusMessage = "AsyncTask is working";

            try {
                XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
                factory.setNamespaceAware(true);
                XmlPullParser parser = factory.newPullParser();
                parser.setInput(params[0].openStream(), null);

                int eventType = parser.getEventType();
                while(eventType != XmlPullParser.END_DOCUMENT) {
                    String tagname = parser.getName();

                    switch(eventType) {
                        case XmlPullParser.START_TAG:
                            if(tagname.equalsIgnoreCase("hunt")) {
                                creator = new Creator();
                                hunt = new Hunt();
                            }
                            break;

                        case XmlPullParser.TEXT:
                            text = parser.getText();
                            break;

                        case XmlPullParser.END_TAG:
                            if(tagname.equalsIgnoreCase("name")) {
                                hunt.setName(text);
                            }
                            else if(tagname.equalsIgnoreCase("creator")) {
                                if(!creators.contains(new Creator(text))) {
                                    creator.setName(text);
                                    creator.addHunt(hunt);
                                    creators.add(creator);
                                }
                                else {
                                    creators.get(creators.indexOf(creator)).addHunt(hunt);
                                }
                            }
                            break;

                        default:
                            break;
                    }
                    eventType = parser.next();
                }
            }
            catch(XmlPullParserException e) {
                e.printStackTrace();
            }
            catch(IOException e) {
                e.printStackTrace();
            }
            return creators;
        }

        @Override
        protected void onPostExecute(ArrayList<Creator> creators) {
            Log.d(TAG, statusMessage);
            for(Creator creator : creators) {
                System.out.println("Creator Name: " + creator.getName());
                for(Hunt hunt : creator.getHunts()) {
                    System.out.println("Hunt Name: " + hunt.getName());
                }
            }
        }
    }

}

XML文件将按以下格式排列(例如):

<hunts>
 <hunt>
<name>hunt 1</name>
<creator>John</creator>
 </hunt>
 <hunt>
<name>hunt 2</name>
<creator>Bill</creator>
 </hunt>
 <hunt>
<name>hunt 3</name>
<creator>James</creator>
 </hunt>
 <hunt>
<name>school hunt</name>
<creator>Fred</creator>
 </hunt>
</hunts>

尝试运行我的应用程序时出现以下错误:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equalsIgnoreCase(java.lang.String)' on a null object reference
at brookes.ac.uk.coletreasurehunt.Creator.equals(Creator.java:35)

这指向了这一行:

 creators.get(creators.indexOf(creator)).addHunt(hunt); 

这是函数HuntListReaderTask的一半,以及指向Creator中的.equals方法。任何人都可以建议这是为什么?

0 个答案:

没有答案