0

我可以获取我的数据,因为我在第 66 行遇到错误。但错误说我得到一个空值。尽管我已经查看了代码,但我认为即便如此,它也能获得正确的值。我尝试使用我专门为它制作的方法。但在那之后它不起作用,我使用了一种有效的方法,我用它来登录。

label_KontoId.setText(myPrivate.getAccountName());

我的班级看起来像这样:在我的主要课程中,我得到了这段代码

public void SetId(String AccountId, String kode) {
    AccountName = AccountId;
    Password =  kode;
}

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                PrivatekunderGUI frame = new PrivatekunderGUI();
                frame.setVisible(true);
                myPrivate = PR.LogindCheck(AccountName, Password);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

    label_KontoId.setText(myPrivate.getAccountName());
}

这不是所有的代码,只是必要的。为 GUI 生成其他代码:

public class Private extends Customers {

    private String AccountName;
    private String Password;
    private int Balance;
    private int AccountId;

    public Private(String Name, int Number, int Zip, String Address, int Phone,
            int Balance, int AccountId, String AccountName, String Password) {
        super(Name, Number, Zip, Address, Phone);
        this.AccountId = AccountId;
        this.Balance = Balance;
        this.AccountName = AccountName;
        this.Password = Password;
    }

    public String getAccountName() {
        return AccountName;
    }

    public String getPassword() {
        return Password;
    }

    public int getBalance() {
        return Balance;
    }

    public int getAccountId() {
        return AccountId;
    }

    public void setAccountName(String accountName) {
        AccountName = accountName;
    }

    public void setPassword(String password) {
        Password = password;
    }

    public void setBalance(int balance) {
        Balance = balance;
    }

    public void setAccountId(int accountId) {
        AccountId = accountId;
    }

    void account() {
    }

    @Override
    public String toString() {
        return "Private [AccountName=" + AccountName + ", Password=" + Password
                + ", Balance=" + Balance + ", AccountId=" + AccountId
                + ", getName()=" + getName() + ", getNumber()=" + getNumber()
                + ", getZip()=" + getZip() + ", getAddress()=" + getAddress()
                + ", getPhone()=" + getPhone() + "]";
    }
}

我的PrivateRegistre样子是这样的:

public class PrivateRegistre {
    private ArrayList<Private> privater = new ArrayList<>();

    public PrivateRegistre() {
        gem("Driton", 32233223, 2400, "Hallovej 54", 32233223, 543442, 1,
                "Driton", "1234");
    }

    public void gem(String Name, int Number, int Zip, String Address,
            int Phone, int Balance, int AccountId, String AccountName,
            String Password) {
        privater.add(new Private(Name, Number, Zip, Address, Phone, Balance,
                AccountId, AccountName, Password));
    }

    public ArrayList<Private> hentalleprivatekunder() {

        return privater;
    }

    public Private findkunde(int AccountId) {
        for (Private privat : privater) {
            if (privat.getAccountId() == AccountId) {
                return privat;
            }
        }
        return null;
    }

    public Private LogindCheck(String AccountName, String Password) {
        for (Private privat : privater) {
            if (privat.getAccountName().equals(AccountName)
                    && privat.getPassword().equals(Password)) {
                return privat;
            }
        }
        return null;
    }

    public boolean sletprivatekunde(int AccountId) {
        Private privat = findkunde(AccountId);
        if (privat == null)
            return false;

        privater.remove(privat);
        return true;
    }

    @Override
    public String toString() {
        return "PrivateRegistre [Private Kunder=" + privater + "]";
    }

    public Private HentLogindOplysninger(String AccountName) {
        for (Private privat : privater) {
            if (privat.getAccountName().equals(AccountName)) {
                return privat;
            }
        }
        return null;
    }
}
4

2 回答 2

1

因为你正在返回null你的方法

    findkunde(...)
    HentLogindOplysninger(...)
    LogindCheck(...)

你不应该返回null- 返回一些有用的东西,如果你没有返回更好void的方法返回类型

于 2013-01-28T16:11:39.510 回答
1

myPrivate = PR.LogindCheck(AccountName, Password);在这里你得到了null因为你正在返回null方法

public Private LogindCheck(String AccountName, String Password)

&也在

 public Private findkunde(int AccountId)

只需将return null语句替换为有意义且可以分配然后使用而不会导致的内容NullPointerException

于 2013-01-28T16:11:22.473 回答