简体   繁体   English

使用 Java 中的 setter 方法访问通过静态方法创建的实例的私有字段出错了 - 可以做什么?

[英]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.描述:我有一个 Connection 类,它的默认构造函数设置为私有。 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()).更多的方法也是此类功能的一部分(例如 setTraits(...) 和 display())。 I created the instance of Connection in the main thread through a method and assigned it to a reference.我在主线程中通过一个方法创建了Connection的实例,并赋值给了一个引用。

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.问题:然而,当我试图从这个引用调用一个方法以访问和更改它的私有字段的值时,我得到了一个 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.编辑,解决:类 Connection: public static Connection createConnection() {} 中的代码检查 connectionCount 数组的长度是否小于 5。由于 connectionCount 数组是在类的顶部分配的(并且它的长度为 5) ,if 语句将始终返回 null 并因此导致 NullPointerException,因为从未创建过连接对象并且您正在尝试访问它的字段。

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.这是ConnectionUtils 类,它提供了getConArrayLength(Connection[] c) 方法,该方法返回指定数组的长度。 [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);):现在,这是在第 6 行抛出 NullPointerException 的主类 (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();
    }
}
private static Connection[] connectionCount = new Connection[5];

You create an array with 5 elements (fixed)您创建一个包含 5 个元素的数组(固定)

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 .调用createConnection方法时,检查数组的长度是否小于 5,如果是,则创建新连接,否则返回null Since 5 < 5 will never return true, your create method always returns null .由于5 < 5永远不会返回 true,因此您的 create 方法始终返回null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM