简体   繁体   中英

Java:Calling method, initialized in one class, gives Nullpointerexception in other class

I defined a Logininfo class to store the email and password of a user in. I get the email and password from a Login GUI. There are put into the Logininfo class using setMethods. I'm connecting the programming to a mysql server (mysql is not an issue here). I need the the emailadres and password so I can use it for queries in other GUI's later on.

This is the Logininfo class

public class Logininfo {

public static Emailadres emailadres = new Emailadres();
public static Password password = new Password();

public static class Emailadres
{
    private String emailadres;

    public Emailadres()
    {
        emailadres = " ";
    }

    public void setEmailadres(String emailadres)
    {
        this.emailadres = emailadres;
    }

    public String getEmailadres()
    {
        return(emailadres);
    }

}

public static class Password
{

    private String password ;

    public Password()
    {
        password = " ";
    }

    public void setPassword(String password)
    {
        this.password = password;
    }

    public String getPassword()
    {
        return(password);
    }

}

}

In the login GUI I use the setMethod to store the emailadress and password.

public class Login extends javax.swing.JFrame {

    Logininfo logininfo;
    Logininfo.Emailadres emailadres = new Logininfo.Emailadres();
    Logininfo.Password password = new Logininfo.Password();

    private boolean validate_login(String email,String wachtwoord) {

    emailadres.setEmailadres(email);
    wachtwoord.setPassword(password);

Later on I try to retrieve the emailadress and password in another class.

public class Account extends javax.swing.JFrame {

Logininfo logininfo;
Logininfo.EmailAdres emailadres;
Logininfo.WachtWoord password;

emailadres.getEmailadres(email);
password.getPassword(password);

I get a Nullpointerexception here. I know that you have to make a new instance of Logininfo in the Login GUI screen. However in the Account class you can't make another new instance. Are public class not supposed to be used for this and should I use something else?

Any help would be appreciated

email_adres in your Login class is an instance field of that class, and is completely unrelated to emailadres instance field of Account class. The two pointers point to different places in memory.

If you want the instance of Account to have that data from Login instance, you have to give it to it somehow.

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