简体   繁体   中英

Android parsing of XML file Null Pointer Exception

I have a method that reads in an XML file, parses it and loads the individual elements into an array. The code itself works when it is included with an Activity class. It needs to be used from 2 different Activities, so I created a utility class so I can call the one copy from each Activity needed. However, when it runs from the utility class, I get a null point exception. This tells me that I'm breaking a rule of what I can do, and from where. I suspect it's related to the access to the resources, as I also tried to access a string resources (just as a test) and that also throws a NPE.

Here is the utility class. The first Log.i appears in LogCat, but the 2nd does not.

class Utils extends MainActivity
{
String debugTag          = DEBUG_TAG + "/Utils";

public void loadRankFile() throws XmlPullParserException, IOException
{       
    int     eventType      = -1;
    boolean boolFoundRanks = false;
    int     intCounter     = 0;

    // this line appears in LogCat      
    Log.i(debugTag, "Inside loadRankFile utility.");

    // this line causes the NPE     
    XmlResourceParser scoutRanks = getResources().getXml(R.xml.scoutranks);

    // this line does not appear in LogCat      
    Log.i(debugTag, "xml file loaded");
.
.
.
}

This is the code from the Activity that calls the method, above:

public class RankProgressActivity extends MainActivity
{
String debugTag          = DEBUG_TAG + "/RankProgressActivity";

Utils utilities = new Utils();

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.rank_progress);

    try
    {
        initSelectScout();
        utilities.loadRankFile();
        initSelectRank();
    }
    catch (IOException e)
    {
        Log.i(debugTag, "Failure initializing Scout Progress activity items", e);
    }       
    catch (XmlPullParserException e)
    {
        Log.e(debugTag, "Rank file parse error.", e);
    }
.
.
.
}

I'm sure I'm making a classic mistake, but I don't see it, and I haven't been able to find guidance as of yet. Any feedback would be appreciated. I'm rather new to Java and Android, so this is very much a learning experience for me.

Thank you in advance.

Your Utils class should not be extending an Activity . Activities are your user facing interfaces, meaning that you implement an Activity when you want to display some sort of UI to the user. Read up on the documentation for an Activity .

Your Utils class should look something like this:

class Utils
{
    private static final String DEBUG_TAG = "Utils";

    public ArrayList<String> parseRankFile(Context context) throws XmlPullParserException, IOException
    {       
        ArrayList<String> scoutRanks = new ArrayList<String>();

        Log.i(DEBUG_TAG, "Inside loadRankFile utility.");
        XmlResourceParser scoutRanksParser = context.getResources().getXml(R.xml.scoutranks);
        Log.i(DEBUG_TAG, "xml file loaded");

        // do your parsing and populate the scoutRanks ArrayList

        return scoutRanks;
    }
}

Then in your Activity you would call your Utils method like this :

ArrayList<String> scoutRanks = Utils.parseRankFile(this);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM