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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:29:12+00:00 2026-06-04T14:29:12+00:00

I have a LoginActivity.java that imports VersionCheck.java. VersionCheck.java has a class VersionCheck that extends

  • 0

I have a LoginActivity.java that imports VersionCheck.java.

VersionCheck.java has a class VersionCheck that extends Activity to gain access to getPackageManager().

In LoginActivity I get an object of class VersionCheck, and run method getVersionName to retrieve the versionName of the application.

When I run the application, I get a java.lang.NullPointerException at pinfo = getPackageManager().getPackageInfo(packageName, 0); in getVersionName method.

If I place the getVersionName method in my main class the code works, but I would like to create a separate class with this method as I use this method frequently in applications.

See code below.

LoginActivity.java

package com.itse.htsurvey;

import com.itse.tools.*;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class LoginActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        displayVersionName();           

    }

    //---------------------------------------------------------------------------------------------------------
    //retrieves version name and displays it
    //---------------------------------------------------------------------------------------------------------
    private void displayVersionName() {

        //object of class versioncheck
        VersionCheck vc = new VersionCheck();

        TextView tvVersion = (TextView) findViewById(R.id.tvVersion);
        tvVersion.setText("Version " + vc.getVersionName(getPackageName()));
    }


}

VersionCheck.java

package com.itse.tools;

import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;

public class VersionCheck extends Activity{

    //---------------------------------------------------------------------------------------------------------
    //Retrieves the version name of the application and returns to method caller
    //---------------------------------------------------------------------------------------------------------
    public String getVersionName(String packageName) {

        PackageInfo pinfo = null;
        try {
            pinfo = getPackageManager().getPackageInfo(packageName, 0);
        } catch (NameNotFoundException e) {         
            e.printStackTrace();
        }

        return pinfo.versionName;    
    }

}

Error message

05-24 13:07:23.146: W/dalvikvm(888): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
05-24 13:07:23.166: E/AndroidRuntime(888): FATAL EXCEPTION: main
05-24 13:07:23.166: E/AndroidRuntime(888): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.itse.htsurvey/com.itse.htsurvey.LoginActivity}: java.lang.NullPointerException
05-24 13:07:23.166: E/AndroidRuntime(888):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-24 13:07:23.166: E/AndroidRuntime(888):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-24 13:07:23.166: E/AndroidRuntime(888):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-24 13:07:23.166: E/AndroidRuntime(888):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-24 13:07:23.166: E/AndroidRuntime(888):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-24 13:07:23.166: E/AndroidRuntime(888):  at android.os.Looper.loop(Looper.java:137)
05-24 13:07:23.166: E/AndroidRuntime(888):  at android.app.ActivityThread.main(ActivityThread.java:4424)
05-24 13:07:23.166: E/AndroidRuntime(888):  at java.lang.reflect.Method.invokeNative(Native Method)
05-24 13:07:23.166: E/AndroidRuntime(888):  at java.lang.reflect.Method.invoke(Method.java:511)
05-24 13:07:23.166: E/AndroidRuntime(888):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-24 13:07:23.166: E/AndroidRuntime(888):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-24 13:07:23.166: E/AndroidRuntime(888):  at dalvik.system.NativeStart.main(Native Method)
05-24 13:07:23.166: E/AndroidRuntime(888): Caused by: java.lang.NullPointerException
05-24 13:07:23.166: E/AndroidRuntime(888):  at android.content.ContextWrapper.getPackageManager(ContextWrapper.java:86)
05-24 13:07:23.166: E/AndroidRuntime(888):  at com.itse.tools.VersionCheck.getVersionName(VersionCheck.java:16)
05-24 13:07:23.166: E/AndroidRuntime(888):  at com.itse.htsurvey.LoginActivity.displayVersionName(LoginActivity.java:29)
05-24 13:07:23.166: E/AndroidRuntime(888):  at com.itse.htsurvey.LoginActivity.onCreate(LoginActivity.java:16)
05-24 13:07:23.166: E/AndroidRuntime(888):  at android.app.Activity.performCreate(Activity.java:4465)
05-24 13:07:23.166: E/AndroidRuntime(888):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-24 13:07:23.166: E/AndroidRuntime(888):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-24 13:07:23.166: E/AndroidRuntime(888):  ... 11 more
  • 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-04T14:29:14+00:00Added an answer on June 4, 2026 at 2:29 pm

    You cannot just extend the Activity class. You should pass the Context of the first activity to that method.

    Like:

    public static String getVersionName(Context context, String packageName) {
    
        PackageInfo pinfo = null;
        try {
            pinfo = context.getPackageManager().getPackageInfo(packageName, 0);
        } catch (NameNotFoundException e) {         
            e.printStackTrace();
            // Can not return pinfo.versionName since pinfo wasn't assigned due to exception
            return null;
        }
    
        return pinfo.versionName;    
    }
    

    Only extend the Activity class if you are using it as an activity 😀

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

Sidebar

Related Questions

Have following setup: MainActivity class - extends activity MyLayout class - extends View Prefs
In my application I have Loginactivity. It has a static variable username and it
Have a matrix report now that has Position, Hours and Wages for a location
My project has an Activity Monitor that monitors the activity of a user. Currently
I have this class: package com.problemio; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList;
I have a login activity which has recover login and register features. If you
I have a TabActivity that has a TabHost with two tabs. Each tab has
I'm working on an application that currently has two activites, a log in activity
Have a SomeLib.pro file that contains: CONFIG += debug TEMPLATE = lib TARGET =
Have you ever created or encountered a self modifying code in Java? If yes,

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.