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

使用Java中的setter方法访问通过静态方法创建的实例的私有字段出错了-可以做什么?,java,static,Java,Static,说明: 我有一个类连接,它的默认构造函数设置为private。可以通过静态方法创建此类。还有一些方法也是此类功能的一部分(例如setTraits(…)和display()。我通过一个方法在主线程中创建了连接实例,并将其分配给一个引用 问题: 然而,当我试图从这个引用调用一个方法来访问和更改它的私有字段的值时,我得到了一个NullPointerException。我的代码有什么问题 编辑,已解析:类连接中的代码:public static Connection createConnection()

说明: 我有一个类连接,它的默认构造函数设置为private。可以通过静态方法创建此类。还有一些方法也是此类功能的一部分(例如setTraits(…)和display()。我通过一个方法在主线程中创建了连接实例,并将其分配给一个引用

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

编辑,已解析:类连接中的代码: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);
    }

}
现在,这是在第6行抛出NullPointerException的主类(con1.setTraits(50601000002000);):

创建一个包含5个元素的数组(固定)

公共静态连接createConnection(){
如果(连接计数长度<5){
连接con=新连接();
//connectionCount[ConnectionUtils.getConArrayLength(connectionCount)]=con;
connectionCount[connectionCount.length-1]=con;
返回con;
}否则返回null;
}

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

您的createConnection返回null,显然,由于5<5不是true,我创建了一个长度为5的数组,然后测试它是否小于5:(.谢谢你的留言,我自己永远也找不到。
getConArrayLength
是一门艺术。
getConArrayLength
是一个无效的方法,它的复杂名称总是返回
5
。与其编辑问题来添加解决方案,不如将其作为答案发布!(甚至可以在页面上直接回答问题)没错,同样的功能也可以通过数组的.length方法实现。我已经删除了它。
public class ConnectionUtils {
    public static int getConArrayLength(Connection[] c) {
        return c.length;
    }
}
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];
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;
}