- Home /
JNI Exception in the editor [Android] Unity 4.2
Hello,
So I just updated to Unity 4.2 and I now get this error in my console when running my game:
Exception: JNI: Init'd AndroidJavaClass with null ptr!
UnityEngine.AndroidJavaClass..ctor (IntPtr jclass) (at C:/BuildAgent/work/cac08d8a5e25d4cb/Runtime/Export/AndroidJavaImpl.cs:533)
Now I ignored this error at first because I assumed it was because I'm not on an Android device. But because of this error my events don't work so I can't truely test my game without exporting it.
Let me note that this error did NOT happen on Unity 4.1, I did no change in my code, just updated Unity.
This is the code that is cuasing the error:
#if UNITY_ANDROID
AdClass = new AndroidJavaClass("com.package.name.Ads");
#endif
AdClass is declared as followed
public static AndroidJavaClass AdClass;
When I build my APK and put it on my android device there are no problems. So this is just happening in the editor. Like stated before I can't ignore this error because it stops all my events so my game doesn't run properly.
Anyone have any suggestions on how I can get rid of this error?
Answer by trs9556 · Aug 05, 2013 at 12:58 AM
So this isn't a solution but it is a work around:
I added this around my JavaClass calls:
#if UNITY_EDITOR
ignoreAdClass = true;
#endif
#if UNITY_ANDROID
if(!ignoreAdClass){
AdClass = new AndroidJavaClass("com.package.name.Ads");
}
#endif
And I declared ignoreAdClass as false initially.
(Any real solution rather than a work around would still be greatly appreciated.
Answer by xanxa3624 · Dec 15, 2013 at 01:48 PM
I had the same problem and I used this:
#if UNITY_ANDROID && !UNITY_EDITOR
AdClass = new AndroidJavaClass("com.package.name.Ads");
#endif
I hope it's works for you.
I just saw this question being bumbed without any new posts or edits, so i checked the moderation queue and found your answer ;)
I was about to post an answer or comment suggesting this since it make the most sense. JNI only works on the actual device. UNITY_ANDROID is just a switch for the targetplatform, but doesn't exclude the editor.
+1
Answer by viame · Aug 11, 2013 at 11:32 AM
How about using this code
if(Application.platform == RuntimePlatform.Android) // do something in android platform
Answer by liortal · Sep 14, 2014 at 10:38 PM
UNITY_ANDROID will be defined if the Android platform is selected in the Build Settings menu.
In order to ignore this code inside the editor, you can check use an extra #if:
#if UNITY_EDITOR
// do nothing ?
#elif UNITY_ANDROID
AdClass = new AndroidJavaClass("com.package.name.Ads");
#endif
Your answer
![](https://koobas.hobune.stream/wayback/20220613111537im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
AndroidJavaClass. Call returns null ptr. 1 Answer
Using PackageManager to launch/check app on Android 1 Answer
NoSuchMethodError calling android method 2 Answers
How to pass Java object to C#? 2 Answers