在对象中获取空指针异常

时间:2013-01-28 16:05:54

标签: java object arraylist

我可以获取我的数据,因为我在第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;
    }
}

2 个答案:

答案 0 :(得分:1)

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

public Private LogindCheck(String AccountName, String Password)

&安培;也在

 public Private findkunde(int AccountId)

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

答案 1 :(得分:1)

因为您在方法中返回null

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

你不应该返回null - 返回一些有用的东西,如果你没有什么可以返回的,那么在方法返回类型中使用void