1

I am getting the following error in my logcat:

03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime: FATAL EXCEPTION: main
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime: Process: in.aekansh.cortibae, PID: 27708
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime: java.lang.ExceptionInInitializerError
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime:     at in.aekansh.cortibae.MainActivity.onCreate(MainActivity.java:130)
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5990)
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2310)
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2419)
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime:     at android.app.ActivityThread.access$900(ActivityThread.java:154)
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135)
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5293)
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime:  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources in.aekansh.cortibae.App.getResources()' on a null object reference
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime:     at in.aekansh.cortibae.AndroidUtilities.<clinit>(AndroidUtilities.java:32)
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime:     at in.aekansh.cortibae.MainActivity.onCreate(MainActivity.java:130) 
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5990) 
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2310) 
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2419) 
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime:     at android.app.ActivityThread.access$900(ActivityThread.java:154) 
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135) 
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5293) 
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) 
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699) 
03-28 15:07:14.863 27708-27708/in.aekansh.cortibae I/Process: Sending signal. PID: 27708 SIG: 9

I don't know how to remove the error.

This is my MainActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    AndroidUtilities.statusBarHeight = getStatusBarHeight();

    chatMessages = new ArrayList<>();

    chatListView = (ListView) findViewById(R.id.chat_list_view);

    chatEditText1 = (EditText) findViewById(R.id.chat_edit_text1);
    enterChatView1 = (ImageView) findViewById(R.id.enter_chat1);

    // Hide the emoji on click of edit text
    chatEditText1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (showingEmoji)
                hideEmojiPopup();
        }
    });


    emojiButton = (ImageView)findViewById(R.id.emojiButton);

    emojiButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showEmojiPopup(!showingEmoji);
        }
    });

    listAdapter = new ChatListAdapter(chatMessages, this); //load the messages from the array.

    chatListView.setAdapter(listAdapter);

    chatEditText1.setOnKeyListener(keyListener);

    enterChatView1.setOnClickListener(clickListener);

    chatEditText1.addTextChangedListener(watcher1);

    sizeNotifierRelativeLayout = (SizeNotifierRelativeLayout) findViewById(R.id.chat_layout);
    sizeNotifierRelativeLayout.delegate = this;

    NotificationCenter.getInstance().addObserver(this, NotificationCenter.emojiDidLoaded);
}

AndroidUtilities.java:

public class AndroidUtilities {

    public static float density = 1;
    public static int statusBarHeight = 0;
    public static Point displaySize = new Point();

    static {
        density = App.getInstance().getResources().getDisplayMetrics().density;
        checkDisplaySize();
    }

    public static int dp(float value) {
        return (int)Math.ceil(density * value);
    }

    public static void runOnUIThread(Runnable runnable) {
        runOnUIThread(runnable, 0);
    }

    public static void runOnUIThread(Runnable runnable, long delay) {
        if (delay == 0) {
            App.applicationHandler.post(runnable);
        } else {
            App.applicationHandler.postDelayed(runnable, delay);
        }
    }

    public native static void loadBitmap(String path, Bitmap bitmap, int scale, int width, int height, int stride);


    public static boolean copyFile(InputStream sourceFile, File destFile) throws IOException {
        OutputStream out = new FileOutputStream(destFile);
        byte[] buf = new byte[4096];
        int len;
        while ((len = sourceFile.read(buf)) > 0) {
            Thread.yield();
            out.write(buf, 0, len);
        }
        out.close();
        return true;
    }

    public static boolean copyFile(File sourceFile, File destFile) throws IOException {
        if(!destFile.exists()) {
            destFile.createNewFile();
        }
        FileChannel source = null;
        FileChannel destination = null;
        try {
            source = new FileInputStream(sourceFile).getChannel();
            destination = new FileOutputStream(destFile).getChannel();
            destination.transferFrom(source, 0, source.size());
        } catch (Exception e) {
            //FileLog.e("tmessages", e);
            return false;
        } finally {
            if(source != null) {
                source.close();
            }
            if(destination != null) {
                destination.close();
            }
        }
        return true;
    }

    public static void checkDisplaySize() {
        try {
            WindowManager manager = (WindowManager) App.getInstance().getSystemService(Context.WINDOW_SERVICE);
            if (manager != null) {
                Display display = manager.getDefaultDisplay();
                if (display != null) {
                    if (android.os.Build.VERSION.SDK_INT < 13) {
                        displaySize.set(display.getWidth(), display.getHeight());
                    } else {
                        display.getSize(displaySize);
                    }
                }
            }
        } catch (Exception e) {
        }
    }

    public static int getViewInset(View view) {
        if (view == null || Build.VERSION.SDK_INT < 21) {
            return 0;
        }
        try {
            Field mAttachInfoField = View.class.getDeclaredField("mAttachInfo");
            mAttachInfoField.setAccessible(true);
            Object mAttachInfo = mAttachInfoField.get(view);
            if (mAttachInfo != null) {
                Field mStableInsetsField = mAttachInfo.getClass().getDeclaredField("mStableInsets");
                mStableInsetsField.setAccessible(true);
                Rect insets = (Rect)mStableInsetsField.get(mAttachInfo);
                return insets.bottom;
            }
        } catch (Exception e) {
            // FileLog.e("tmessages", e);
        }
        return 0;
    }



}

Please tell me where am I getting the NullPointerException. I think I have called all the necessary functions after super.onCreate(); so I don't think there should be any problem. Please help me out. Ask me for more information if you need it.

******* UPDATE *******
App.java:

public class App extends Application {

    private static App Instance;
    public static volatile Handler applicationHandler = null;

    @Override
    public void onCreate() {
        super.onCreate();

        Instance=this;

        applicationHandler = new Handler(getInstance().getMainLooper());

        NativeLoader.initNativeLibs(App.getInstance());

    }

    public static App getInstance()
    {
        return Instance;
    }
}
Aekansh Dixit
  • 513
  • 1
  • 9
  • 20

2 Answers2

0

Your App.getInstance() is returning null. The issue here is static block code at AndroidUtilities.java:

static {
    density = App.getInstance().getResources().getDisplayMetrics().density;
    checkDisplaySize();
}

Static initialization blocks are run when the JVM (class loader - to be specific) loads StaticClass (which occurs the first time it is referenced in code) and before your application started (before onCreate() method of App class was invoked), you can read more at here and here.

The solution is remove above static code block and create a static method at AndroidUtilities.java

public class AndroidUtilities {

    public static float density = 1;
    public static int statusBarHeight = 0;
    public static Point displaySize = new Point();

    public static void init() {
        density = App.getInstance().getResources().getDisplayMetrics().density;
        checkDisplaySize();
    }

    // your other code
}

App.java:

public class App extends Application {

    public void onCreate() {
        super.onCreate()
        Instance = this;
        AndroidUtilities.init();

        // your other code
    }
}
Community
  • 1
  • 1
tjeubaoit
  • 986
  • 7
  • 7
  • I did this: `private MainActivity Instance; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Instance = this; AndroidUtilities.init(); //other code } ` But I still get the same error. – Aekansh Dixit Mar 28 '16 at 10:44
  • Add at App.java, not MainActivity.java, and remove static code block – tjeubaoit Mar 28 '16 at 10:48
  • Well, my bad. Alright. I did that. Now `ExceptionInInitializerError` is gone but a new error has come up: `03-28 16:20:01.826 3864-3864/in.aekansh.cortibae E/AndroidRuntime: FATAL EXCEPTION: main 03-28 16:20:01.826 3864-3864/in.aekansh.cortibae E/AndroidRuntime: Process: in.aekansh.cortibae, PID: 3864 03-28 16:20:01.826 3864-3864/in.aekansh.cortibae E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object in.aekansh.cortibae.App.getSystemService(java.lang.String)' on a null object reference ...` – Aekansh Dixit Mar 28 '16 at 10:53
  • Did you remove `checkDisplaySize()` from static block ? – tjeubaoit Mar 28 '16 at 11:10
  • I just did that. Now I get this: `FATAL EXCEPTION: main Process: in.aekansh.cortibae, PID: 6134 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object in.aekansh.cortibae.App.getSystemService(java.lang.String)' on a null object reference` – Aekansh Dixit Mar 28 '16 at 11:12
  • In AndroidUtilities.java: `public static void init() { density = App.getInstance().getResources().getDisplayMetrics().density; //checkDisplaySize(); }` – Aekansh Dixit Mar 28 '16 at 11:13
  • Like this: static { //density = App.getInstance().getResources().getDisplayMetrics().density; //checkDisplaySize(); }, not at init() – tjeubaoit Mar 28 '16 at 11:15
  • Yes, I have removed that initial static block code. I replaced it with the one you told me to. And then, I went over to App.java and made the necessary changes. Then, this error still persists. Any idea what's happening? – Aekansh Dixit Mar 28 '16 at 11:17
  • checkDisplaySize() was invoked before onCreate(), you call it at somewhere else ? Is this all your code? – tjeubaoit Mar 28 '16 at 11:27
  • Remove all static block code that call App.getInstance() – tjeubaoit Mar 28 '16 at 11:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/107600/discussion-between-aekansh-dixit-and-tjeubaoit). – Aekansh Dixit Mar 29 '16 at 07:11
0

Android Manifests check application, add android:name=".App" and android:allowBackup="true" and permission

  <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"></uses-permission>

 <application
        android:name=".App"
        android:allowBackup="true"
        android:supportsRtl="true"

And activity add windowSoftInputMode attribute

  <activity
            android:name=".UserChatActivity"
            android:windowSoftInputMode="adjustResize"></activity>

Grandle Control useLibrary and sourceSets.main

useLibrary 'org.apache.http.legacy'
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    sourceSets.main {
        jniLibs.srcDir 'libs'
        jni.srcDirs = [] //disable automatic ndk-build call
    }

Rebuid.