-3

I am having a problem at a Java program. Exception in thread "main" java.lang.NullPointerException

Exception in thread "main" java.lang.NullPointerException
    at DBOperation.DBOperation.checkLogin(DBOperation.java:30)
    at DBOperation.DBOperation.main(DBOperation.java:82)

who can help me find error? how to fix them? (Please note that the code is still complete and I have entered some print statements for checking) My code:

package DBOperation;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
public class DBOperation {
    private String url;
    private String serverName;
    private String dbName;
    private String usr;
    private String pwd;
    private Connection cnn;     
    public boolean checkLogin(String user, String pass) {
        boolean tmp = false;
        Statement stm = null;
        ResultSet rs = null;

        try {
            stm = cnn.createStatement();
            rs = stm.executeQuery("Select *from ListUser where username = '"+user+"' and password = '"+pass+"'");
            if(rs.next())
                return tmp = true;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            try {
                rs.close();
                stm.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        return tmp;
    }

    public DBOperation() {

        this.url = "jdbc:sqlserver://";
        this.serverName = "localhost";
        this.dbName = "sharefile";
        this.usr = "sa";
        this.pwd = "123456";
    }
    @SuppressWarnings("finally")
    public Connection getMyCon()
    {
        Connection cn = null;
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            try {
                cn = DriverManager.getConnection(getMyUrl(),usr,pwd);
                if(cn!=null)
                {
                    System.out.println("Ket noi thanh cong");
                }
                else{
                    System.out.println("Ket noi khong thanh cong");
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
        return cn;
        }
    }
    private String getMyUrl() {
        // TODO Auto-generated method stub
        return url+serverName+"; databaseName="+dbName;
    }
    public static void main(String[] args) {
        DBOperation objDB = new DBOperation();
        objDB.getMyCon();
        objDB.checkLogin("teri", "123456");

    }


}
Rustam
  • 6,485
  • 1
  • 25
  • 25
teri.nk79
  • 15
  • 3

1 Answers1

3

The case is that in do not set the value of connection. In method getMyCon() you set only local one.

You can Call the getMyCon() in check login for example.