this.getApplication() returns NULL pointer

by Stefan » Sat, 10 Oct 2009 01:49:05 GMT

Sponsored Links
 I'm running the following line in an Activity, which is within the
same application, but in a different package:
AppObject appObj = (AppObject)this.getApplication();  // FYI:
AppObject is my extension class of Application.

It returns only a null pointer, while when I move it to the "main"
package and run it from there it returns the application reference as

I've defined the activity in my AndroidManifest.xml  with the full
qualified class name, since it is in another package: <activity

Any ideas of what I've to do different?



this.getApplication() returns NULL pointer

by Dianne Hackborn » Sat, 10 Oct 2009 02:27:18 GMT

 Are you calling it in the constructor?  The activity is not yet fully
initialized at that point.

Dianne Hackborn
Android framework engineer

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.


Sponsored Links

this.getApplication() returns NULL pointer

by Stefan » Thu, 15 Oct 2009 17:32:32 GMT

 Nope. Right now, for testing purposes, it is triggered through a
button click.

I've also included android:name="AppObject" within the <application>-
tag of the Manifest. And the initialization of the AppObject seems to
work properly.

The only thing that doesn't work is calling AppObject appObj =
(AppObject)this.getApplication(); from an Activity that is not in the
same package as the AppObject (which is in the "main" package). As
soon as I move this Activity into the "main" package it works properly
and (AppObject)this.getApplication(); returns the Application Object.


this.getApplication() returns NULL pointer

by jotobjects » Fri, 16 Oct 2009 02:53:56 GMT

 Does "this" refer to the OnClickListener button listener object?
Often this is an inner member class of the activity so it would work
if you take the "this." off.  But you seem to have something a little
more complicated going on.  Maybe we need to see the OnClickListener


Other Threads

1. contentobserber for my database


       i  tried to create a contentobserver for database,but when the
tables changed, it did  not work in my activity.

        is   the Uri wrong?

        here is a mycode:

        public static final String AUTHORITY =
        public static final Uri CONTENT_URI= Uri.parse("content://" +
AUTHORITY + "/mytable");

class DownloadsChangeObserver extends ContentObserver {
    public DownloadsChangeObserver(Handler handler) {
        super(new Handler());
    public void onChange(boolean selfChange) {

DownloadsChangeObserver(new Handler()));

I don't know where is wrong in my code,hope for help !Thank you!


2. Socket timeout not working?

In the sample code, a SocketTimeoutException should be thrown if the
connection can't be stablished in a second, isn't it?

                try {
                        s = new Socket(host, port);
                        if (s.isConnected()) s.close();
                } catch (SocketTimeoutException e) {
                } catch (UnknownHostException e) {
                } catch (ConnectException e) {
                } catch (IOException e) {

But the timeout take a looong time to be detected.


3. Localice Android applications and base system

4. Blogging Tool

5. Listeners cause unexpected stop??

6. Showing only certain images with ACTION_VIEW

7. Emulator won't work. emulator window was out of view and was recentred