开发者

Android, Null Pointer Exception to Data that isn't null

I'm writing an app for android that needs to parse data from an XML file. I've never come across an error like this that is so impossibly hard to track down. Or maybe my brain just stopped working. That happens. XML file is of the form:

<?xml version="1.0" encoding="iso-8859-1"?>
<memberRoster>
    <agent>
        <agentInfo1>...</agentInfo1>
        <agentInfo2>...</agentInfo2>
        ...
    </agent>
    <agent>
        ...
    </agent>
    ...
</memberRoster>

So far it's working well, except for some random bits of fun!

Every now and then it will throw a NullPointerException. I did some more digging and found out that there are THREE "agents" (out of 800) with "supposedly" null data. I checked the XML file and the data is there, there are no illegal characters, etc. It is the same three "agents" every time. The program parses other entries before and after these "null" "agents". Also of note is that not all "agentInfo" fields in the ArrayList come up null; example, one of the entries has 7 of the 8 entries as null, with the 8th one non-null, another has only one null with the last 7 non-null.

I'm parsing the data in to an ArrayList from the XML file, and like I mentioned before, it works flawlessly until it comes to those three specific entries in the XML file.

I'm sorry I can't give much more info than that, the data is sensitive to our members.

EDIT:

Sorry! I knew I was forgetting something! :)

Some code from my XMLHandler.java class:

public void characters(char[] ch, int start, int length)

    if(this.in_mr_agentNrdsId) {
        agent[0] = ch.toString();
    }
    else开发者_开发技巧 if(this.in_mr_agentFirstName) {
        agent[1] = ch.toString();
    }
    else if(this.in_mr_agentLastName) {
        agent[2] = ch.toString();
    }
    else if(this.in_mr_agentPhone) {
        agent[3] = ch.toString();
    }
    else if(this.in_mr_agentEmail) {
        agent[4] = ch.toString();
    }
    else if(this.in_mr_agentOfficeName) {
        agent[5] = ch.toString();
    }
    else if(this.in_mr_agentOfficePhone) {
        agent[6] = ch.toString();
    }
    else if(this.in_mr_agentType) {
        agent[7] = ch.toString();
        pds.setMemberRoster(agent);
        agent = new String[8];
    }

PDS is an object of type ParsedDataSet, which is just a simple class containing the ArrayList objects and a few getter and setter methods:

public class ParsedDataSet { private ArrayList agentOpenHouses = new ArrayList(); private ArrayList calendarOfEvents = new ArrayList(); private ArrayList latestStatistics = new ArrayList(); private ArrayList memberRoster = new ArrayList();

public ArrayList<String[]> getAgentOpenHouses() {
    return agentOpenHouses;
}

public ArrayList<String[]> getCalendarOfEvents() {
    return calendarOfEvents;
}

public ArrayList<String[]> getLatestStatistics() {
    return latestStatistics;
}

public ArrayList<String[]> getMemberRoster() {
    return memberRoster;
}

public void setAgentOpenHouses(String[] agentOpenHousesItem) {
    this.agentOpenHouses.add(agentOpenHousesItem);
}

public void setCalendarOfEvents(String[] calendarOfEventsItem) {
    this.calendarOfEvents.add(calendarOfEventsItem);
}

public void setLatestStatistics(String[] latestStatisticsItem) {
    this.latestStatistics.add(latestStatisticsItem);
}

public void setMemberRoster(String[] memberRosterItem) {
    this.memberRoster.add(memberRosterItem);
}

} // end class ParsedDataSet


You could throw an if statement into your assignements and reassign any caught 'NULL' or empty strings into a zero value or just reassign as variable = "" in your code.

For example:

if (agentInfo1 == NULL) {
    agentInfo1 = "" || agentInfo1 = 0; //Depending on what your variables are
}


Try putting try catch loop in code to find where the error is happening, then, pinpoint the exact part of code that is giving this error, there do null checks before proceeding. This is based on best practices of software development, rather than a fix for you.

Alternatively, you can makes sure on server side that there are no "null" values, maybe by giving dummy value like "EMPTY_STRING". This is especially relevant if your app is already shipped and you cant make any client code changes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜