当我在另一个类中调用时,为什么getter方法为null?

时间:2017-05-07 04:05:26

标签: java oop netbeans nullpointerexception

但是当我得到的值存在时,在另一个类的set方法中, 所以我有3个类,第一个类是getter setter类,第二个是填充集合的类,最后一个是获取方法getter的类,但它是null值...

    function connect() {
    if(!SOCKET) {
        chat("italic", "Generating authentication token...");   
        var hash = getCookie('hash');
        if(!hash) {
            chat("error", "Please sign in through Steam to connect.");
        } else {
            chat("italic", "Connecting to server...");
            SOCKET = io(HOST);
            SOCKET.on('connect', function(msg) {
                chat("alert", "Connected!");
                SOCKET.emit('hash', hash);
            });
            SOCKET.on('connect_error', function(msg) {
                chat("error", "Conection lost...");
            });
            SOCKET.on('message', function(msg) {
                onMessage(msg);
            });
        }
    } else {
        console.log("Error: connection already exists.");
    }
}

2 个答案:

答案 0 :(得分:0)

您的getUserName()将为null,因为您用于比较的对象没有分配给它的userName值。

 public void jalankan() {

       //creating an object here , br has no username assigned
        loginAdminn br = new loginAdminn();

        String kueri = "Select Username, password from Admin";

        int x = 0;

        try {
            ps = conn.prepareStatement(kueri);
            ResultSet rs = ps.executeQuery();

            while (rs.next()) {

                if (br.getUsername().equalsIgnoreCase(rs.getString("Username"))) {
                    if (br.getPassword().equalsIgnoreCase(rs.getString("Password"))) {
                        JOptionPane.showMessageDialog(null, "Masuk Berhasil");
                        x = 1;
                        break;
                    }
                }
            }

当你正在阅读br.getUserName()时,它假设你为null。

答案 1 :(得分:0)

A级

String  x;

setter getter x

A c = new A();   
c.get...    // object Class A type X

B级

A c = newA();  
c.get...    // object Class B type X

c.get form Class A   !=   c.get from Class B

这是您使用new创建的两个独立且不同的对象。