tags:

views:

331

answers:

2

Hi all,

I'm trying to use a custom class (be.myname.rssreader.util.Global) extending android.app.Application, but when I try to use its accessors, I get the following exception:

02-23 11:42:12.973: ERROR/AndroidRuntime(2050): java.lang.RuntimeException: Unable to start activity ComponentInfo{be.myname.rssreader/be.myname.rssreader.view.RSSReader}: java.lang.NullPointerException

Before you ask, yes, it's in my manifest, as follows:

<application android:name=".util.Global" android:icon="@drawable/icon"
        android:label="@string/app_name" android:debuggable="true">

The code where the exception is thrown is this:

global.setService(new FeedService(getApplicationContext()));

It's placed in the onCreate of my main Activity, immdiately after the calls to super.onCreate and setContentView.

Any ideas why this keeps throwing that exception?

Thanks.

+1  A: 

Did you check global isn't null ?

EDIT : If you're trying to access your Application class in an Activity or a Service, you have to retrieve it like that :

/* e.g. Global is the name of your class which extends Application */
this.global = (Global) getApplication()
kosokund
Yeah, I forgot to mention that: global is in fact null, but its onCreate method is executed (I placed a Log statement in it).According to the Android docs: "... which will cause that class to be instantiated for you when the process for your application/package is created." (http://developer.android.com/reference/android/app/Application.html)So I don't get why it's null.
benvd
Ok so you're trying to access your Application class called Global from an another activity ?
kosokund
Yes, I am. The documentation lead me to believe that should be possible. I'm probably just missing something. Either way, I "solved" the problem using a simple class with static fields that I set in my main onCreate().
benvd
Look at my response I edited it yesterday, did you use `getApplication()` to retrieve your application class ?
kosokund
A: 

Check this link. Try ./emulator -wipe -data before running your application.

Macarse
Hmm.. I'm running it on my Hero, so wiping isn't a very good idea I think. But I tried executing the app on an emulator (after wiping, to be sure) and still the same exception...
benvd