Note that there are some explanatory texts on larger screens.

plurals
  1. POAttempting to get class name on Android using getClassContext()[1].getName()
    primarykey
    data
    text
    <p>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.</p> <pre><code>private static class CurrentClassGetter extends SecurityManager { public String getClassName() { return getClassContext()[1].getName(); } } </code></pre> <p>The calling code:</p> <pre><code> //---Get my name (last part of class name) System.out.println("Attempting to get class name"); msSimpleName = new CurrentClassGetter().getClassName(); </code></pre> <p>Which, I notice, is the exact same bit of code recommended by someone in this SO answer here:</p> <p><a href="https://stackoverflow.com/questions/936684/getting-the-class-name-from-a-static-method-in-java">Getting the class name from a static method in Java</a></p> <p>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:</p> <pre><code>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) </code></pre> <p>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.</p> <p>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.</p> <p>Does anyone know why getClassContext()[1].getName() would be throwing a NullPointerException like this all of a sudden, when it worked before?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload