Steven Barbra
Steven Barbra

Reputation: 37

Can't execute MySQL stored procedure in JDBC getting nullpointer exception

im not sure where to go from here i have messed with my code and i still get a null pointer exception. leading me to believe that maybe my connection is messed up... anyway unsure any help would be cool.

package callassstatement;
    import java.sql.CallableStatement;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.Scanner;


    public class CallAssStatement {
    private static Connection conn;


    public static void printOptions() {

    System.out.println("enter 1 to get employee");
    System.out.println("enter 2 to get department");
    System.out.println("enter 3 to exit program");

}

public static String getEmployeeMethod(String id) {
    String abc = null;
    try {

        CallableStatement cs = conn.prepareCall(" { call sp_GetEmployee(1)}");


        cs.setString(1, id);
        //register the OUT parameter before calling the stored procedure
         cs.registerOutParameter(2, java.sql.Types.VARCHAR);
        cs.registerOutParameter(3, java.sql.Types.VARCHAR);
        cs.registerOutParameter(4, java.sql.Types.VARCHAR);
        cs.registerOutParameter(5, java.sql.Types.VARCHAR);

        cs.execute();

        //read the OUT parameter now
        String employeeId = cs.getString(1);
        String lastName = cs.getString(2);
        String firstName = cs.getString(3);
        String departmentId = cs.getString(4);
        String startDate = cs.getString(5);



            abc = ("EmplyeeID: " + employeeId + " " + lastName + "," + firstName + "" + " in " 
                    + departmentId + " Department "+ ", StartDate:"+ startDate);
            return abc;

    } catch (SQLException ex) {
        System.out.println(ex.getMessage());
    }
    return abc;
}

public static void main(String[] args) {

    CandDLoader.createConn();

    printOptions();
    Scanner s = new Scanner(System.in);
    Scanner id = new Scanner(System.in);

    String input = s.nextLine();


    switch (input) {
        case "1":
            System.out.println("calling get employee");
            System.out.println(" Enter employeeID:");
            String ab = id.nextLine();
            getEmployeeMethod(ab);
            break;
        case "2":
            System.out.print("calling get department");
            break;
        case "3":
            System.out.print("exiting");
            System.exit(0);
        default:
            System.out.print("what are you trying to do");
            printOptions();




    }
    }

here is my connection class.

package callassstatement;

   import java.sql.Connection;
   import java.sql.DriverManager;
   import java.sql.SQLException;


   public class CandDLoader {


   public static Connection createConn(){
Connection conn = null;
try{

Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/timeclock", "timeclockuser", "password_1234");
    }
    catch(SQLException | ClassNotFoundException ex){}


    return conn;
    }
    }

Upvotes: 0

Views: 1473

Answers (3)

OneCricketeer
OneCricketeer

Reputation: 191681

In createConn() you do return conn, however in main(), you say

CandDLoader.createConn(); 

Therefore doing nothing with the returned conn and essentially throwing it away.

Either change that line to this

conn = CandDLoader.createConn();

Or change

private static Connection conn;

to this

private static Connection conn = CandDLoader.createConn();

When you declare object fields without assignment, they are assigned as null.

For example

private static Connection conn;

Is the exact same as saying

private static Connection conn = null;

Upvotes: 1

Paweł Chorążyk
Paweł Chorążyk

Reputation: 3643

private static Connection conn; in your CallAssStatement class is always null, you create a connection in CandDLoader.createConn(); but you do not actually assign it to your conn variable in CallAssStatement.

Upvotes: 0

Vivek Singh
Vivek Singh

Reputation: 2073

Check the below line of your codebase

conn = DriverManager.getConnection("jdbc:mysql://localhost/timeclock", "timeclockuser", "password_1234");

The url provided here does not contain the port no, it should be as below:

conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/timeclock", "timeclockuser", "password_1234");

Upvotes: 0

Related Questions