0

This is a very simple example of my project which is a lot more bigger in scale.

Tasks:

  • The password will be set at the setpassword method.

  • The getpassword method will return the password.

  • The password is called at sendmail class in order to be send via email to the user to log in with the new credentials.

But When I run the whole code everything works except that the sendmail class won't access the password from the getpassword method in users class.

I put a simple version of my code:

users class >>>>>

public class  users {


private String password;


public users (){}

// GETTING PASSWORD
public String getpassword(){

    return password;
}


// SETTING PASSWORD
 public void setapassword(String password){
     this.password=password;
 }




}

Signup class>>>>>>

public class signup {

public void signsup(){

    users  user1 =new users();
    user1.setapassword("player"); 

    sendmail mail1 =new sendmail();
    mail1.Sendsmail();

}
}

sendmail class>>>>

public class sendmail   {

public void  Sendsmail(){

    users user1 =new users(); // object

    user1.getpassword(); //getting password 

 System.out.println(user1.getpassword()); // If print here I get null
 }
 }

Main signup Class>>>>

public class SignupMain {

public static void main(String[] args) {

     signup signup1= new signup();
     signup1.signsup();
   }

 }
Klayd Pro
  • 1
  • 1
  • 2
  • 7
  • You can pass user1.getpassword() in constructor of sendmail class. – Sudhir Ojha Apr 11 '18 at 08:37
  • Please make sure you will follow the [java coding conventions](http://www.oracle.com/technetwork/java/codeconventions-150003.pdf) so that others can read and understand your code more easily. Thanks. – L.Spillner Apr 11 '18 at 08:43
  • where can I read more about the above problem I have ? such that I understand the concept better . – Klayd Pro Apr 11 '18 at 09:31

3 Answers3

2

The user1 object from your signup class and your sendmail class are different. Surely their variable is named the same way but they refer to different Objects. To access the password from the user you have to pass the user to the sendmail class e.g.:

Signup.java

public class signup
{

  public void settingPassowrd()
  {
    users user1 = new users();
    user1.setapassword( "player" );

    sendmail mail1 = new sendmail();
    mail1.Sendsmail(user1);

  }
}

with:

public class sendmail
{

  public void Sendsmail(user usr)
  {

    usr.getpassword(); // getting password

    System.out.println( usr.getpassword() ); // will be the proper value from now on.
  }
}
L.Spillner
  • 1,772
  • 10
  • 19
0

Problem is that in your sendmail class you create a new user users user1 =new users(); --> so you don't access the user you previously created, but you create a new one which obviously does not have a password.

Instead, pass your user to the sendmail function in the signup class:

public void settingPassowrd(){

    users  user1 =new users();
    user1.setapassword("player"); 

    sendmail mail1 =new sendmail();
    mail1.Sendsmail(user1);

}

and in class sendsmail:

public void  Sendsmail(users user) {
    System.out.println(user.getpassword());
}

By the way, I suggest you read some coding guidelines for Java. for example:

  • Class names should always start with a capital letter
  • Class names are usually singular (class users --> class User)
  • Method names always start with a lowercase letter and use camelCase (getpassword() --> getPassword())
Patric
  • 1,489
  • 13
  • 28
0

Please follow the standard coding conventions. I changed your code with standard coding conventions. just copy paste it , it will work

Users class

public class  Users {
private String password;
public users (){}
// GETTING PASSWORD
public String getpassword(){

    return password;
}
// SETTING PASSWORD
 public void setapassword(String password){
     this.password=password;
 }
}

Signup class

public class Signup {

public void settingPassowrd(){

    Users  user1 =new Users();
    user1.setapassword("player"); 

    SendMail mail1 =new SendMail(user1);
    mail1.Sendsmail();

}
}

SendMail class

public class SendMail   {
    private Users user1;
    SendMail(Users user1){
        this.user1 = user1
    }
    public void  sendMail(){
    user1.getpassword(); //getting password 
    System.out.println(user1.getpassword()); // If print here I get null
}
}

Main class to test the code

 public class SignupMain {

public static void main(String[] args) {

 Signup signup1= new Signup();
 signup1.settingPassowrd();
}

}
Ramesh Fadatare
  • 561
  • 4
  • 12
  • Hats off Buddy !! It was a whole day that I was trying to do it !! Finally I had the email sent with the credentials of the user! thanks a lot! – Klayd Pro Apr 11 '18 at 09:22
  • Well Come ..You can refer my blog posts of Java/J2EE design patterns - http://ramesh-java-design-patterns.blogspot.in/p/start-here.html – Ramesh Fadatare Apr 11 '18 at 09:44