title: Importing class extending activity without a view
body: <p>I have a LoginActivity.java that imports VersionCheck.java.</p>
  
  <p>VersionCheck.java has a class VersionCheck that extends Activity to gain access to getPackageManager().</p>
  
  <p>In LoginActivity I get an object of class VersionCheck, and run method getVersionName to retrieve the versionName of the application.</p>
  
  <p>When I run the application, I get a <em>java.lang.NullPointerException</em> at <code>pinfo = getPackageManager().getPackageInfo(packageName, 0);</code> in getVersionName method.</p>
  
  <p>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.</p>
  
  <p>See code below. </p>
  
  <p><strong>LoginActivity.java</strong></p>
  
  <pre><code>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()));
      }
  
  
  }
  </code></pre>
  
  <p><strong>VersionCheck.java</strong></p>
  
  <pre><code>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;    
      }
  
  }
  </code></pre>
  
  <p><strong>Error message</strong></p>
  
  <pre><code>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
  </code></pre>