Home » Java » android – How to overcome java.lang.NullPointerException: Attempt to invoke virtual method 'java.sql.Connection' on a null object reference?-Exceptionshub

android – How to overcome java.lang.NullPointerException: Attempt to invoke virtual method 'java.sql.Connection' on a null object reference?-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment


I cant connect to my database project? I use xampp phpMyAdmin as my database. I already create two(2) activities for my project. First is mainActivity and second the home page. The mainActivity is the login page. I want to make login process that can connect to database.

Here is the code for onclickListener for login button in login page. Right now i’m just trying to connect to database. The code is not complete yet:–

login_bt.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        Connection connnect=null;
        connnect = DatabaseConnection.ConnectDB();
        try {



                Toast.makeText(getApplicationContext(),"No K/Tangan atau Kata Laluan tidak sah",Toast.LENGTH_LONG).show();

        }catch (Exception e){

I also create a class for database connection. Here are the codes in that class:–

import android.os.StrictMode;
import java.sql.Connection;
import java.sql.DriverManager;

class ConnectionClass {

    public Connection ConnectDB(){

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        Connection conn=null;

            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/pkb_db","user","password");

        }catch (Exception e){ }
        return conn;

I think everything is ok but there is some error in logcat as shown below:–

    Process: com.e.test, PID: 16114
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.sql.Connection com.e.test.ConnectionClass.ConnectDB()' on a null object reference
        at com.e.test.MainActivity$3.onClick(MainActivity.java:78)
        at android.view.View.performClick(View.java:6597)
        at android.view.View.performClickInternal(View.java:6574)
        at android.view.View.access$3100(View.java:778)
        at android.view.View$PerformClick.run(View.java:25885)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

Can anyone please help me….. Please
Here is the screenshot for my working environment

How to&Answers:

As per your logs object in DatabaseConnection is null so thats why its throwing null pointer exception when you call DatabaseConnection.ConnectDB make sure you initialize DatabaseConnection object properly before calling ConnectDB() method.

Hope this will help you.