繁体   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?

描述:我有一个 Connection 类,它的默认构造函数设置为私有。 可以通过静态方法创建此类。 更多的方法也是此类功能的一部分(例如 setTraits(...) 和 display())。 我在主线程中通过一个方法创建了Connection的实例,并赋值给了一个引用。

问题:然而,当我试图从这个引用调用一个方法以访问和更改它的私有字段的值时,我得到了一个 NullPointerException。 我的代码有什么问题?

编辑,解决:类 Connection: public static Connection createConnection() {} 中的代码检查 connectionCount 数组的长度是否小于 5。由于 connectionCount 数组是在类的顶部分配的(并且它的长度为 5) ,if 语句将始终返回 null 并因此导致 NullPointerException,因为从未创建过连接对象并且您正在尝试访问它的字段。

课程:

这是连接类:

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);
    }

}

这是ConnectionUtils 类,它提供了getConArrayLength(Connection[] c) 方法,该方法返回指定数组的长度。 [已弃用]

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

现在,这是在第 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];

您创建一个包含 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;
}

调用createConnection方法时,检查数组的长度是否小于 5,如果是,则创建新连接,否则返回null 由于5 < 5永远不会返回 true,因此您的 create 方法始终返回null

暂无
暂无

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

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