0

New to Java! I have created a class that has constructors for various fields. However, when I try and print the fields, I recieve "null" as the output. Please help me understand why. Here is the class:`

 public class User {
   //All relevant information to identify each user.
      private String FirstName;
      private String LastName;
      private String Email;
      private long PhoneNum;
      private long CardNum;

   //Build Constructors for User info.

public User(String Fname, String Lname, String Mail, long num, long card)
{
   Fname=FirstName;
    Lname=LastName;
    Mail=Email;
    num=PhoneNum;
    card=CardNum;
}

  //Method to set FirstName.  
 public void setFirstName (String Fname) 
{
    FirstName=Fname;
}
//Method to get FirstName.
public String getFirstName()
{
    return FirstName;
}
 //Method to set LastName. 
public void setLastName (String Lname)
{
    LastName=Lname;
}


 //Method to get Lastname. 
    public String getLastname()
    {
        return LastName;
    }
    //Method to set email.
    public void setEmail (String Mail)
    {
        Email=Mail;
    }
     //Method to get email.
    public String getEmail()
    {
        return Email;
    }
    //Method to set phonenumber.
    public void setPhoneNum(long num)
    {
        PhoneNum=num;
    }
   //Method to get phonenumber.
    public long getPhoneNum()
    {
        return PhoneNum;
    }
   //Method to set credit card number.
        public void setCardNum(long card)
    `enter code here`{
        CardNum=card;
    }
     // Method to get credit card number.
         public long getCardNum()
    {
        return CardNum;
    }

Now when I run this code, I receive "null":

public class UserDemo {
public static void main(String[] args)
{
    String first="Matt";
    String last="Burns";
    String email="[email protected]";
   long phone=333;
   long card=222;
    User Matt=new User(first,last,email,phone,card);
    System.out.print(Matt.getLastname());
}

What am I doing wrong? Thank you in advance!

`

1
  • 1
    Pay attention to the compiler warning messages. You should have gotten lots of "variable may be uninitialized" warnings. Commented May 6, 2016 at 3:58

3 Answers 3

1

Is the other way around, instead of:

public User(String Fname, String Lname, String Mail, long num, long card){
    Fname=FirstName;
    Lname=LastName;
    Mail=Email;
    num=PhoneNum;
    card=CardNum;
}

it should be:

public User(String Fname, String Lname, String Mail, long num, long card){
    FirstName = Fname;
    LastName = Lname;
    Email = Mail;
    PhoneNum = num;
    CardNum = card;
}

To avoid this kind of thing you can use the this keyword. Here is an example:

String firstName;
public Constructor(String firstName){
    this.firstName = firstName;
}
Sign up to request clarification or add additional context in comments.

Comments

0

You have your constructor assignments the wrong way:

public User(String Fname, String Lname, String Mail, long num, long card)
{
    // this refers to the current object being constructed
    this.FirstName = Fname;
    this.LastName = Lname;
    this.Email = Mail;
    this.PhoneNum = num;
    this.CardNum = card;
}

Comments

0

Java constructor objects should be created like so:

public User(String Fname, String Lname)
{
    this.firstName = Fname; 
    //keyword 'this' refers to the new object, so the new object's Fname would equal the firstName place below
    this.lastName = Lname; //same as above
}

To create a new User, use this code:

User Matt = new("Matt", "Burns"); //Creates a new User Matt, with firstName "Matt" and lastName "Burns"
System.out.println(Matt.getfirstName()); //prints User Matt's firstName to the console.

1 Comment

This solves the user's problem but it would be useful if you could explain why/what they did wrong too.

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.