Fish_In_A_Suit
Fish_In_A_Suit

Reputation: 45

Accessing private fields of an instance created through a static method with a setter method in Java gone wrong - what can be done?

Description: I have a class Connection, with it's default constructor set to be private. One can create this class through a static method. Some more methods are also part of this Class's functionality (for example setTraits(...) and display()). I created the instance of Connection in the main thread through a method and assigned it to a reference.

Problem: However, when I tried to call a method from this reference in order to access and change the value of it's private fields, I was given a NullPointerException. What is wrong with my code?

Edit, resolved: The code in class Connection: public static Connection createConnection() {} checks if the length of the connectionCount array is less than 5. Since the connectionCount array was allocated at the top of the class (and it's length is 5), the if statement will always return null and therefore cause the NullPointerException, since the connection object wasn't ever created and you're trying to access it's fields.

Classes:

This is the Connection class:

public class Connection {
    private int bandwidth;
    private int timeLength;
    private int downloadSpeed;
    private int uploadSpeed;

    private static Connection[] connectionCount = new Connection[5];


    private Connection() {

    }

    public static Connection createConnection() {
        if (connectionCount.length < 5) {
            Connection con = new Connection();
            //connectionCount[ConnectionUtils.getConArrayLength(connectionCount)] = con;
            connectionCount[connectionCount.length -1] = con;
            return con;
        } else return null;
    }

    public void setTraits(int bandwidth, int timeLength, int downloadSpeed, int uploadSpeed) {
        this.bandwidth = bandwidth;
        this.timeLength = timeLength;
        this.downloadSpeed = downloadSpeed;
        this.uploadSpeed = uploadSpeed;
    }

    public int getBandwidth() {
        return bandwidth;
    }

    public int getTimeLength() {
        return timeLength;
    }

    public int getDownloadSpeed() {
        return downloadSpeed;
    }

    public int getUploadSpeed() {
        return uploadSpeed;
    }

    public void display() {
        System.out.println("bandwidth: " + bandwidth);
        System.out.println("timeLength: " + timeLength);
        System.out.println("downloadSpeed: " + downloadSpeed);
        System.out.println("uploadSpeed: " + uploadSpeed);
    }

}

This is the ConnectionUtils class, which provides getConArrayLength(Connection[] c) method, which returns the length of the specified array. [DEPRECATED]

public class ConnectionUtils {
    public static int getConArrayLength(Connection[] c) {
        return c.length;
    }
}

And now, this is the main Class which throws NullPointerException at line 6 (con1.setTraits(50, 60, 100_000, 2000);):

public class ConnectionManager {
    public static void main(String[] args) {
        Connection con1 = Connection.createConnection();
        con1.setTraits(50, 60, 100_000, 2000);
        con1.display();
    }
}

Upvotes: 0

Views: 47

Answers (1)

Stultuske
Stultuske

Reputation: 9437

private static Connection[] connectionCount = new Connection[5];

You create an array with 5 elements (fixed)

public static Connection createConnection() {
    if (connectionCount.length < 5) {
        Connection con = new Connection();
        //connectionCount[ConnectionUtils.getConArrayLength(connectionCount)] = con;
        connectionCount[connectionCount.length -1] = con;
        return con;
    } else return null;
}

When you call the createConnection method, you check whether the length of your array is less than 5, if it is, you create a new connection, otherwise, you return null. Since 5 < 5 will never return true, your create method always returns null.

Upvotes: 1

Related Questions