1

I have the following bit of code, used on Android and tested quite extensively about 6 months ago. It has been working fine and has never been the cause of any problems.

private static class CurrentClassGetter extends SecurityManager {
    public String getClassName() {
      return getClassContext()[1].getName();
    }
}

The calling code:

    //---Get my name (last part of class name)
    System.out.println("Attempting to get class name");
    msSimpleName = new CurrentClassGetter().getClassName();

Which, I notice, is the exact same bit of code recommended by someone in this SO answer here:

Getting the class name from a static method in Java

Since this code was written, I've updated my Android SDK from r14 to r16. I also downloaded all of the newer SDK packages, from API10 (Android 2.3.3) to API15 (Android 4.0.3). However, the above code which was working fine is now throwing this error:

02-22 16:18:26.779: W/System.err(2593): java.lang.NullPointerException
02-22 16:18:26.786: W/System.err(2593):     at com.company.myCompany.mobile.android.myCompanyApp$CurrentClassGetter.getClassName(myCompanyApp.java:413)
02-22 16:18:26.806: W/System.err(2593):     at com.company.myCompany.mobile.android.myCompanyApp.monitor(myCompanyApp.java:289)
02-22 16:18:26.806: W/System.err(2593):     at com.company.myCompany.mobile.android.myCompanyApp.monitor(myCompanyApp.java:257)
02-22 16:18:26.826: W/System.err(2593):     at com.gomez.appname.appnameActivity.onCreate(appnameActivity.java:22)
02-22 16:18:26.836: W/System.err(2593):     at android.app.Activity.performCreate(Activity.java:4465)
02-22 16:18:26.846: W/System.err(2593):     at android.app.InstAppentation.callActivityOnCreate(InstAppentation.java:1049)
02-22 16:18:26.856: W/System.err(2593):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
02-22 16:18:26.866: W/System.err(2593):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
02-22 16:18:26.876: W/System.err(2593):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
02-22 16:18:26.876: W/System.err(2593):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
02-22 16:18:26.896: W/System.err(2593):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-22 16:18:26.896: W/System.err(2593):     at android.os.Looper.loop(Looper.java:137)
02-22 16:18:26.906: W/System.err(2593):     at android.app.ActivityThread.main(ActivityThread.java:4424)
02-22 16:18:26.926: W/System.err(2593):     at java.lang.reflect.Method.invokeNative(Native Method)
02-22 16:18:26.926: W/System.err(2593):     at java.lang.reflect.Method.invoke(Method.java:511)
02-22 16:18:26.946: W/System.err(2593):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-22 16:18:26.946: W/System.err(2593):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-22 16:18:26.956: W/System.err(2593):     at dalvik.system.NativeStart.main(Native Method)

Basically, what you're looking at is a call from onCreate in Android to my private API. Within the API, it's trying to get the current class name using the above code.

The Target API version was 8 then and it's 8 now (Android v2.2), so that has not changed at all. Java compliance level 1.6, running Java 1.6.0_24 64-bit.

Does anyone know why getClassContext()[1].getName() would be throwing a NullPointerException like this all of a sudden, when it worked before?

Community
  • 1
  • 1
AWT
  • 3,657
  • 5
  • 32
  • 60
  • Did you try to remove Android SDK v16 from your machine and install the v14 again to confirm that SDK version is the only difference? – Fixpoint Feb 23 '12 at 17:16
  • Working on that now, on a clean machine (I need v16 for other stuff as well so I can't remove it from this system). It takes a surprisingly long time to download all of different pieces of the SDK. – AWT Feb 23 '12 at 20:16
  • Based on what I'm seeing so far, the context from which the array of classes is derived is empty within getClassName(). I don't think it used to be like this. Outside of the call to getClassName(), I can see a ton of stuff in "this". Within the call, totally empty. – AWT Feb 23 '12 at 20:40
  • Confirmed. Something has changed between the Android SDK r13 and r16 that causes this call to return a null. – AWT Mar 01 '12 at 21:42

1 Answers1

0

No fix in sight, I ended up removing this block of code entirely. It was only being used in debug output anyway and it wasn't worth the research to figure out why this was failing.

AWT
  • 3,657
  • 5
  • 32
  • 60