0

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;
    }
}
1
  • i forgot to tell that i call this to before Private privat = PR.LogindCheck(textField_brugernavn.getText(), textField__koderod.getText()); if(privat != null){ MainGUI.this.setVisible(false); PrivatekunderGUI PKG = new PrivatekunderGUI(); PKG.SetId(brugernavn, Kodeord); PKG.setVisible(true); Commented Jan 28, 2013 at 16:28

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Yeah if it doesn't find anything. but if i use the same name and password as in the login. that works and i get though way does it not work this time?
i forgot to tell that i call this to before Private privat = PR.LogindCheck(textField_brugernavn.getText(), textField__koderod.getText()); if(privat != null){ MainGUI.this.setVisible(false); PrivatekunderGUI PKG = new PrivatekunderGUI(); PKG.SetId(brugernavn, Kodeord); PKG.setVisible(true);
1

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 replacereturn null statement with something that would make sense and can be assigned and then used without resulting into NullPointerException;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.