Rachit Mishra
Rachit Mishra

Reputation: 6112

Can an Object be declared static in java?

public static Connection connect;
    public static ResultSet resultSet;
    static final String DATABASE_URL = "jdbc:mysql://localhost/java_dev";

    public ServerFunctions() {
        String Driver = "com.mysql.jdbc.Driver";            
        String DB_USERNAME = "lucky";
        String DB_PASSWORD = "lucky";

        try {
            Class.forName(Driver);
            connect = DriverManager.getConnection(DATABASE_URL,DB_USERNAME, DB_PASSWORD);
        } catch(Exception e) {
            System.out.println("Database Not Connected ! ");
        }
    }

    public static Boolean verificator(String username, String password) {
        try {
            PreparedStatement prepare = connect.prepareStatement(
                "Select * from users where username='?'&&password='?'");

Above is my code fragment. I am declaring the Connection and Resultset objects static so they can be called from the static verificator method. also the verificator method is declared static so that it can be called from a different class as class variable just by using className.verificator(param, param).

It compiles and run when I test the class alone but then i get the NullPointerException error at the PerparedStatement Line whenever a call is made from different class.

Can someone Please help me why it is Happening ?

Thankyou

Upvotes: 1

Views: 9911

Answers (5)

Yongxin Zhao
Yongxin Zhao

Reputation: 124

Can an Object be declared static in java?

You can, but it's a bad idea.

public static Boolean verificator(String username, String password) {
    try {
        synchronized(DATABASE_URL){
            if(connect==null){
                Class.forName(Driver);
                connect = DriverManager.getConnection(DATABASE_URL,DB_USERNAME, DB_PASSWORD);
            }
        }

        PreparedStatement prepare = connect.prepareStatement(

Upvotes: 2

G-Man
G-Man

Reputation: 1331

A constructor is executed when an instance of the class gets constructed, aka when you use the new keyword. By using the static connect variable before creating an instance of ServerFunctions the constructor doesn't get executed.

you can use a static initializer block to rectify this which will be executed when the class is loaded.

static{
    //code from your constructor here
}

Upvotes: 1

pcalcao
pcalcao

Reputation: 15990

You said you declared the verificator method as static, so it can be accessed from other classes, and the verificator method is using the connection, which is also static.

The problem is that the connection is only initialized in the constructor of the object, so, if the constructor is never called, connect will be null.

You need to either initilize your connect variable in a static block, or verify if it is null before using it.

Upvotes: 10

epoch
epoch

Reputation: 16605

This means that your Connection object was closed somewhere or not initialized properly (by calling the constructor). I would highly recommend not having static resources such as Connection and ResultSet, rather make them instance based and call that instance from a static method.

Upvotes: 1

Dahaka
Dahaka

Reputation: 517

You get a NullPointerException because you do not initialize the variable. Try something like

public static ResultSet resultSet = new ResultSet();

or run the function wich initializes the variables before accessing them.

Upvotes: 1

Related Questions