简体   繁体   中英

Getting a null pointer exception in object

I can get my data, because i get an error on line 66 which is. but error saying that I get a null value. although I've looked through the code, I think even that, it gets the right value in. I tried using a method that I made just for it. but after that it did not work, I used a method that works, which I use to log in with.

label_KontoId.setText(myPrivate.getAccountName());

My class's looks like this: In my main i got this code

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

This is not all the code, just what's necessary. The other code is generated for the 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() + "]";
    }
}

and my PrivateRegistre looks like this:

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

myPrivate = PR.LogindCheck(AccountName, Password); here you are getting null beacuse you are returning null in method

public Private LogindCheck(String AccountName, String Password)

& also in

 public Private findkunde(int AccountId)

just replace return null statement with something that would make sense and can be assigned and then used without resulting into NullPointerException ;

Because you are returning null in your methods

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

You shouldn't be returning null - return something useful and if you have nothing to return better use void in method return type

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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