1

I know that a similar question has been answered before in other questions but all the ones i found does not apply to my situation so i decided that i ask it.

This line gives an error:

User users = new User();

Error message:

constructor User in class User cannot be applied to given types;
  required: String,String,String,String
  found: no arguments
  reason: actual and formal argument lists differ in length

Below is my java class file.

public class User {

    private String username;
    private String pwd;
    private String email;
    private String role;

    public User(String username, String pwd, String email, String role) {
        this.username = username;
        this.pwd = pwd;
        this.email = email;
        this.role = role;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }
}
zforgo
  • 2,508
  • 2
  • 14
  • 22
ETCasual
  • 106
  • 11
  • You have one constructor that takes 4 `String`s and try to call a constructor that takes nothing, which doesn't exists, so the compiler says "nope". Notice that a default constructor will only be defined for you when you haven't defined any constructor yourself. – akuzminykh Nov 25 '20 at 19:13

2 Answers2

1

Just create another constructor:

public User(){}

When you make a class this constructor is made for you by default. When you create a constructor yourself, this default constructor isn't made for you anymore and you have to add it in yourself.

thetechnician94
  • 545
  • 2
  • 21
1

Your User constructor takes 4 arguments: username, pwd, email, and role, and you're trying to construct it with new User(), which provides none of the arguments. You should actually provide them:

User user = new User("username here", "pwd here", "email here", "role here");

Or, create a constructor with no arguments of the form:

public User() {
    this.username = /* some default value */;
    this.pwd = /* some default value */;
    this.email = /* some default value */;
    this.role = /* some default value */;
}

Or, to reuse your constructor:

public User() {
    this(/* username default */, /* pwd default */, /* email default */, /* role default */);
}

You could use null as the default value, but that will probably just lead to NullPointerExceptions down the line.

Aplet123
  • 33,825
  • 1
  • 29
  • 55
  • If i create a constructor with ``` public User() { } ``` will it still be able to use getter and setters? – ETCasual Nov 25 '20 at 19:30
  • Yes, it'd be a `User` like any other constructor with access to all of `User`'s methods. However, the values that you get may be `null` if you don't initialize them. – Aplet123 Nov 25 '20 at 19:32
  • yeah, the issue im facing now is that i keep getting null on all my values, i setUsername on another interface then when i getUsername in another interface, it returns null – ETCasual Nov 25 '20 at 19:37
  • Which is why you should provide default values like my answer told you to instead of just leaving it up to the compiler. – Aplet123 Nov 25 '20 at 19:37
  • sorry... i keep getting null even if i have default value, how do i initialize them? – ETCasual Nov 25 '20 at 19:46
  • Either, provide default values like my answer told you to, or make sure you call a setter before you call a getter. – Aplet123 Nov 25 '20 at 19:51
  • may we move to chat? sorry for taking up ur time but i would like to show u my code and hope that u can help me >. – ETCasual Nov 25 '20 at 19:57
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/225127/discussion-between-etcasual-and-aplet123). – ETCasual Nov 25 '20 at 19:57