Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7006679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:30:30+00:00 2026-05-27T21:30:30+00:00

I’ve been having some issues reading an XML file in my ‘res’ folder and

  • 0

I’ve been having some issues reading an XML file in my ‘res’ folder and I think I’ve narrowed it down to an issue with my apps ‘activity’.

I keep getting a NullPointerException on line # 2 below.

Here is my code to get the activity. Is there a better or a right way to do this?

1. Activity activity = this;

2. Resources res = activity.getResources();

3. XmlResourceParser xpp = res.getXml(R.xml.encounters);

Here is the class:

public class XmlParser extends Activity  {


public XmlParser()
    throws XmlPullParserException, IOException
{
    try {
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setValidating(false);

        Activity activity = this;
        Resources res = activity.getResources();
        XmlResourceParser xpp = res.getXml(R.xml.encounters);

    } catch (Exception e) {
        String stackTrace = Log.getStackTraceString(e);
        Log.e("error", stackTrace);
    }
   }
}

And I’m getting the error on the “Resources res = activity.getResources();” line…
Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:30:31+00:00Added an answer on May 27, 2026 at 9:30 pm

    Something tells me that there’s a problem with this or activity and there’s some code missing here. The code that you’ve put there shouldn’t ever generate a NPE because this is never null and if 2 followed 1 immediately, activity shouldn’t be null. It’s possible that this isn’t an instance of Activity but that would generate a compile error, not an NPE.

    Edit Ah, I think I see your problem. You need to override onCreate() and this Activity will be instantiated by the Android framework. You then need to move all of the code from the constructor and put it in the onCreate() method. However, I think this is indicative of a fundamental understanding of how applications work in Android. Instead, what I would do is create a standard Android application in Eclipse. That will create a base activity for you. From there, remove the subclassing Activity from your XmlParser class and change the constructor to take a Context argument. From there, instantiate the XmlParser in your other Activity, the one that was created by Eclipse.

    All of that said, I think you should spend some time reading over the application fundamentals in the developer docs to understand how you’re supposed to access resources, etc.

    Edit 2 For more information about Activities check out this link. Also, check out this SO post for how to do what you’re trying to do.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm parsing an XML file, the creators of it stuck in a bunch social
I have a jquery bug and I've been looking for hours now, I can't
For some reason, after submitting a string like this Jack’s Spindle from a text
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
i want to parse a xhtml file and display in UITableView. what is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.