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 8084687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:52:16+00:00 2026-06-05T17:52:16+00:00

I am developing an android application. My question is can I get the application

  • 0

I am developing an android application. My question is can I get the application context in an abstract class? it gives me null pointer exception for the context variable and I assume it is maybe because I am using it inside an abstract class.

NOTE: In my code I have an abstract class (the abstract class itself extends Activity) that I use for instantiating other classes – Activites. In abstract class i create an instance of an activity helper, a class that handles some common activity-related functionality in the app. The is my activity helper class:

public class ActivityHelper { 
protected Activity mActivity; 

public static ActivityHelper createInstance(Activity activity, Context context) { 
return UIUtils.isTablet(context) ? 
new ActivityHelperTablet(activity) : 
new ActivityHelper(activity); 
} 

protected ActivityHelper(Activity activity) { 
mActivity = activity; 
}  
     }

Where the function isTablet receives a context variable that I create in my abstract class

     public static boolean isTablet(Context context) {
      // Can use static final constants like HONEYCOMB, declared in later versions 
      // of the OS since they are inlined at compile time. 
     return isHoneycomb() ||    
    ((context.getResources().getConfiguration().screenLayout    &        
    Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE);
    } 

the important part of BaseActivity:

public abstract class BaseActivity extends Activity { 

Context context = this.getApplicationContext(); 
final ActivityHelper mActivityHelper = ActivityHelper.createInstance(this, context);
    }

But it gives me a null pointer exception. So I thought that it’s maybe because of the abstract class.
And this is the log:

06-12 15:59:56.011: E/AndroidRuntime(1206): Caused by: java.lang.NullPointerException
06-12 15:59:56.011: E/AndroidRuntime(1206):     at   
android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101)
06-12 15:59:56.011: E/AndroidRuntime(1206):     at   
com.navayo.sec.voip.activities.BaseActivity.<init>(BaseActivity.java:35)
06-12 15:59:56.011: E/AndroidRuntime(1206):     at 
com.navayo.sec.voip.activities.MainActivity.<init>(MainActivity.java:43)
06-12 15:59:56.011: E/AndroidRuntime(1206):     at 
java.lang.Class.newInstanceImpl(Native Method)
06-12 15:59:56.011: E/AndroidRuntime(1206):     at   
java.lang.Class.newInstance(Class.java:1319)
06-12 15:59:56.011: E/AndroidRuntime(1206):     at 
android.app.Instrumentation.newActivity(Instrumentation.java:1023)
06-12 15:59:56.011: E/AndroidRuntime(1206):     at   
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1870)
  • 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-06-05T17:52:17+00:00Added an answer on June 5, 2026 at 5:52 pm

    Having an abstact class doesn’t matter.

    You have a null pointer because your context is null, which means somewhere in your methods there is an error, without the code to review i can’t help you more.

    EDIT:

    public static ActivityHelper createInstance(Activity activity) { 
    return UIUtils.isTablet(activity.getApplicationContext()); 
    } 
    

    this should be enough. tell me if this is helping. simply an activity extends from context so you even might do: UIUtils.isTablet(activity);

    now another subject would be the way you are actually creating the instances which is impossible to tell from your code, Activities are started with intents and have a lifecycle, they cannot be constructed with constructors.

    EDIT 2:

    Your issue is in this code:

    final ActivityHelper mActivityHelper = ActivityHelper.createInstance(this, context);
    

    At this time the activity is not yet created so your context (“this”) is null.

    you need to move the activity helper initialization in onCreate. the activity works a bit differently from other classes, it might be a good idea to also read a bit more on the activity lifecycle and see some more examples (try google :)).

    the correct case would be:

    private ActivityHelper mActivityHelper;
    
    protected void onCreate(Bundle savedInstanceState){
    super(savedInstanceState);
    // here do the initialization
    
    mActivityHelper = ActivityHelper.createInstance(this);
    .
    .
    .
    
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am about to start developing an Android application and had a question if
I'm developing an Android 3.1 and above application, but this question is not specific
I have been developing Android application, and I have one question - ListView uses
i have a question. I'm developing an Android Application. Actually, i have a thread
I am developing android application for client. Now client testing my application in various
I am developing android application. In my application i have used sql server2008 R2.
I am developing android application in which i have to open option menu from
I have been developing Android application for Android 3.x and now I want to
I am developing android application, and I want to implement PHP as backend. I
I am developing Android Application for Chat between two users as private session .

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.