Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have problem with connecting my program to database, when I do everything in one file then it working, but when I want make it object oriented then it calls NullPinterException. There is code if you can help me:

Main.java:

public static void main(String[] args) {
    try{
    DbConnect.ConnectToDB();
    }catch(Exception e){
        System.out.println(e);
    }
}

DatabaseConnect.java:

public class DatabaseConnect {
private static Connection connect;
public static String isClosed;

public void ConnectToDB(){
    try{
        Class.forName("com.mysql.jdbc.Driver");
        connect = DriverManager.getConnection("jdbc:mysql://127.0.0.1/warsztattest", "root", "");
    }catch(Exception e){
        System.out.println(e);
    }
}
public String CheckConn() throws SQLException{
    if (connect.isClosed()){
        isClosed = "Disconnected";
    }else{
        isClosed = "Connected";
    }
    return isClosed;
}

}

share|improve this question
1  
You are mixing static attributes with non-static methods. And where is DbConnect declared? –  Jimmy T. Jul 6 at 12:01

3 Answers 3

up vote 1 down vote accepted

You can't call non-static methods in a static manner. Your code should work if you declare your ConnectToDB() as a static method or if you instantiate DatabaseConnect in your main class.

EDIT: In my honest opinion you should declare the methods as static, because it makes no reasonable sense to instantiate the class every time you want to use the database connection especially because every object will have the same properties. Would be a waste of resources.

share|improve this answer

I have tried to make minor changes to your code, so this is not the solution I'd use in my code (for example: no constructor has been defined). I haven't tested it but it should work.

Main.java:

public static void main(String[] args) {
    try{
    DatabaseConnect dbc = new DatabaseConnect();
    dbc.ConnectToDB();
    String status = dbc.isClosed() ? "Closed" : "Open";
    System.out.println("Connection status: "+status);
    }catch(Exception e){
        System.out.println(e);
    }
}

DatabaseConnect.java:

public class DatabaseConnect {
private Connection connect;

public void ConnectToDB(){
    try{
        Class.forName("com.mysql.jdbc.Driver");
        connect = DriverManager.getConnection("jdbc:mysql://127.0.0.1/warsztattest", "root", "");
    }catch(Exception e){
        System.out.println(e);
    }
}
public String CheckConn() throws SQLException{
    return connect.isClosed();
  }
}
share|improve this answer

Just instantiate the DatabaseConnect object then call its methods

public static void main(String[] args) {
    try{

    //DbConnect.ConnectToDB();

    DatabaseConnect dataConnect = new DatabaseConnect();
    dataConnect.ConnectToDB();
    dataConnect.CheckConn();

    }catch(Exception e){
        System.out.println(e);
    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.