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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:01:10+00:00 2026-05-22T23:01:10+00:00

First to explain how projects are setup: Android Library project with two classes: UserActivity

  • 0

First to explain how projects are setup:

  • Android Library project with two classes:
    • UserActivity – uses static field in HolderClass
    • HolderClass – this one has static field Data
  • Android Application that references Library project, has only one class:
    • StarterActivity – when app is started, in onCreate it sets static field Data to some value, and it starts UserActivity

Now, the problem is: sometimes I get java.lang.NullPointerException for that static field Data – as if I never initialized it (or value got deleted in meantime). It never happened on my test device, but I keep getting error reports from client devices. I can only guess how that happens – maybe user navigates away from app, then comes back and system recreates whole application context, but in that context HolderClass has empty static field Data?

My question:

Is initialization of that static field from Activity’s onCreate wrong approach? Should I put data in ApplicationContext? Or do something else?

I am open for all suggestions.

P.S. If you have problem visualizing from description, here is how everything I’ve said would look in code:

// IN ANDROID APP PROJECT
public class StarterActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 

        // just start activity in library
        Intent myIntent = new Intent(this, AutolaunchActivity.class);
        startActivityForResult(myIntent, 1);    
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // exit when activity from library exits
        finish();       
    }
}


// IN LIBRARY PROJECT

public class HolderClass {
    public static String Data;
}

public class UserActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 

        // init layout  
    }

    public void someButtonClicked() {
        HolderClass.Data.trim();
    }
}
  • 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-22T23:01:11+00:00Added an answer on May 22, 2026 at 11:01 pm

    OK, this was basically the problem of global variables – in the end I’ve solved it by reading this answer: How to declare global variables in Android?

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

Sidebar

Related Questions

Can someone explain this result to me. The first test succeeds but the second
This question might not seem programming related at first, but let me explain. I'm
Can anyone explain in simple words what First and Second Level caching in Hibernate/NHibernate
First, let me explain what I am doing. I need to take an order,
Alright let me explain my situation first: I am part of an organization that
I just completed my first (minor) Python project, and my boss wants me to
First I will try to explain what I want to do. The app loads
I have two projects, with identical priorities and work hours demand, and a single
First, let's get the security considerations out of the way. I'm using simple authentication
First off, I am using Windows XP. I have multiple hard drives and it

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.