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

  • Home
  • SEARCH
  • 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 6763757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:30:51+00:00 2026-05-26T14:30:51+00:00

I followed the advice given here in several posts on how to declare global

  • 0

I followed the advice given here in several posts on how to declare global contants:

public class Constants {

    public static final int i1 = 1;
    public static final int i2 = 2; 
    }

I just include this class in my project and refer to the constants like this:

in any other class...
    GlobalsVars.gi1 = Constants.i1;

(ps I hope this is OK and do not need to do anything to the Constants class like initializing or anything.)

But as I found out here: assigning int to Integer using static global variables is not a good idea. My app crashes sometimes when accessing the constants.

Though I find it really weird, since my app is rather small, but may be the Constants class – not an activity – is really removed from the memory in certain cases, though I access its constants in all my activities. That’s why I would think it should not be removed from memory anyway.

But for sure, my app crashes in certain cases when accessing the Constants.i1 value.

What would be the best way just to declare some constants in a reliable way. (In c-Derivatives there are the easy to use macros) But there is nothing like this in Android.

-> all I need are “reliable” constants in Java…

EDIT:

declaration of GlobalVars class added

public class GlobalVars {
    public static Integer gi1;
    public static Integer gi2;
}

Many thanks

EDIT:
added crash log

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxxx.xxxx/com.xxxx.xxxx.screens.One_screen}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1830)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1851)
at android.app.ActivityThread.access$1500(ActivityThread.java:132)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:4293)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.xxxx.xxxx.screens.Settings_screen.presentOnScreen(One_screen.java:172)
at com.xxxx.xxxx.screens.Settings_screen.onCreate(One_screen.java:49)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1794)
… 11 more

and the line 172 in One_screen is:

if (GlobalVars.gi1 == Constants.i1){
  • 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-26T14:30:52+00:00Added an answer on May 26, 2026 at 2:30 pm

    Your NullPointerException is occurring because GlobalVars.gi1 is null, not Constants.i1. You should always be able to rely on hard-coded integer values as they are part of the class definition.

    If you simply wish to store a small number of long-lived integer variables. I suggest looking into SharedPreferences to store them (instead of GlobalVars).

    You can find out more here.

    If you wish to pass data only from one Activity to another, look at adding values to the Intent‘s extras using Intent.putExtra and retrieve the extras Bundle in the next Activity using Intent.getExtras on the Intent retrieved from Activity.getIntent.

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

Sidebar

Related Questions

I followed on some advice given on this very forum and I am still
I followed the advice here, but I'm still running into the same issues after
I followed the advice here on how to use JSON. After using $resultAml =
I've followed advice on how to setup 404s by: http://www.andornot.com/about/developerblog/archive/2009_10_01_archive.aspx and related: Best way
I followed the advice on various forums and S.O. and am still unable to
I'm using fnh and castle nhib facility. I followed the advice from mike hadlow
Followed this question about delayed_job and monit Its working on my development machine. But
I followed this tutorial on configuring the Rails plugin ExceptionNotifier. I know that I
Program followed by output. Someone please explain to me why 10,000,000 milliseconds from Jan
I followed the commonly-linked tip for reducing an application to the system tray :

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.