javaandroidnullpointerexceptionmain-activity

How to remove Android ExceptionInitializeError?


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;
    }
}

Solution

  • 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
        }
    }