-1

I want to access a table I created and print it within the console of Eclipse. However, I keep getting an error message that says this:

java.lang.NullPointerException: Cannot invoke "java.sql.Connection.createStatement()" because "databaseSQL.conAdministrator" is null.

I also uploaded a jar file: mysql-connector-java-8.0.26.jar

However, my openDataBaseConnection method returns that the database is connected.

My code:

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.*;
import java.sql.DriverManager;

public class databaseSQL {
    private static Connection conAdministrator;

    public static void main(String[] args) {
        openDataBaseConnection(); 
        LoadListFromDatabase();
    }

    public static void LoadListFromDatabase() { 
        String strConnect = "jdbc:mysql://localhost:3306/DBexample";
        String sql = "SELECT * FROM employeeInfo";
        Statement sqlCommand = null;
        ResultSet rstTSource = null;
        openDataBaseConnection();
        try {
            sqlCommand = conAdministrator.createStatement();
            rstTSource = sqlCommand.executeQuery(sql);
            while(rstTSource.next()) {
                String name = rstTSource.getString(1);
                System.out.println(name);
            }
        } catch(Exception e) {
            System.out.println(e);
        }
    }

    public static boolean openDataBaseConnection() {
        Connection conAdministrator = null;
        String strConnect = "";
        boolean blnResult = false;
        try {
            strConnect = "jdbc:mysql://localhost:3306/DBexample";
            conAdministrator =  (Connection)DriverManager.getConnection(strConnect, "root", "Classof2017!");
            if(conAdministrator != null) {
                System.out.println("Database is Connected!");
                blnResult = true;
            }
        } catch(Exception e) {
            System.out.println("Not Connected!");
        }
        return blnResult;
    }

    public static boolean closeDataBaseConnection() {
        boolean blnResult = false;
        try {
            if(conAdministrator != null) {
                if(conAdministrator.isClosed() == false) {
                    conAdministrator.close();
                    conAdministrator = null;
                }
            }
            blnResult = true;
            System.out.println("Database is Closed.");
        } catch(Exception e) {
            System.out.println(e);
        }
        return blnResult;
    }
}

This is the SQL table I am trying to display in the console:

USE DBexample;
CREATE TABLE employeeInfo(intEmployeeID INT,
                            strFirstName VARCHAR(20),
                            strLastName VARCHAR(20),
                            strHomePhone VARCHAR(20),
                            strEmail VARCHAR(30),
                            dtmDateOfBirth DATE);
INSERT INTO employeeInfo VALUE('123000','Brittney', 'Pugh', '(513)738-4730', 'pughb@email.com', '1998-12-01');
INSERT INTO employeeInfo VALUE('246000', 'Emilee', 'Peters', '(513)222-9876', 'emileep@email.com', '1990-05-21');
INSERT INTO employeeInfo VALUE('689000', 'Sally', 'Jenkins', '(513)456-2590', 'jenkinssally@email.com', '1985-03-23');
INSERT INTO employeeInfo VALUE('537000', 'John', 'Smith', '(513)889-0754', 'SmithJ@email.com','1995-07-07');
SELECT * FROM employeeInfo;
Brittney P
  • 21
  • 7

2 Answers2

1

pay attention on your openDataBaseConnection() method. You are declaring again the conAdministrator variable e so the openDataBaseConnection is using conAdministrator as method variable and not the class variable.

Try this version of openDataBaseConnection:

/**
 * Name: openDataBaseConnection
 * ABSTRACT: Opens the connection to the database
 * @return blnResult determining factor if the database is connected
 */
public static boolean openDataBaseConnection() {
    // Connection conAdministrator = null;
    String strConnect = "";
    boolean blnResult = false;
    try {
        strConnect = "jdbc:mysql://localhost:3306/DBexample";
        conAdministrator = (Connection)DriverManager.getConnection(strConnect, "root", "Classof2017!");
        
        if(conAdministrator != null) {
            System.out.println("Database is Connected!");
            blnResult = true;
        }
    }
    catch(Exception e) {
        System.out.println("Not Connected!");

   }
       return blnResult;
    
}

I hope can help you

kuroaza
  • 71
  • 2
1

Its because you have the variable conAdministrator twice. Once in the Attribute-Section of the class and once at the top of the method openDataBaseConnection, so the connection gets written into the variable in the function.

Removing the first line of the method should solve that problem. To avoid such bugs in the future, try to access variables which are not in the method with the keyword this

Bamba675
  • 103
  • 7