0

How should I add a whole class in a linked list that also has arguments such as name, password and a boolean to check if it's connected to a server?

The strange thing about this issue is that the User is successfully registered in the User.java, but not in the UserRegistery.java.

public class User {

    private String name = "";
    private String password = "";
    private String email = "";

    private boolean connection;

    public User(String name, String password, boolean connection){

        this.name = name;
        this.password = password;
        this.connection = connection;

        setUserName(name);
        setUserPassword(password);
        setUserConnection(connection);
        System.out.println("Client Created : User ["+format()+"]");
    }

    public void setUserName(String name){
        this.name = name;
    }

    public String getUserName(){
        return name;
    }

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

    public String getUserPassword(){
        return password;
    }   

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

    public String getUserEmail(){
        return email;
    }

    public void setUserConnection(boolean connection){
        this.connection = connection;
    }

    public boolean getUserConnection(){
        return connection;
    }

    public String format(){
        return String.format("%-5s, %-5s, %s" , getUserName(), 
                getUserPassword(), getUserConnection());
    }
}
public class UserRegistry {

    private LinkedList<User> users;
    private User user;
    private String name = "", password = "";
    private boolean conn = false;

    //Constructor:
    public UserRegistry() { //Setting new students in the Linked List
        users = new LinkedList<User>();
    }

    public void addUser(User aUser) {
        System.out.println("Waiting to "
            + "add user to the server . . . ");

        users.add(aUser);
        System.out.println("Client Creation "
            + ": Confirmed !!! ");

        System.out.println("Client Creation "
            + ": Declined !!! ");
    }


}

This is the output I get:

Screenshot

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Loizos Vasileiou
  • 674
  • 10
  • 37
  • Where did you create the `registration`shown in the screenshot? Be also more precise on your problem - is it the NPE? – Stefan Freitag Jan 03 '18 at 19:06
  • 1
    Did you ever called `users.add(aUser)`? And why did you wrote "Confirmed" AND "Declined" without checking/validating? – zlakad Jan 03 '18 at 19:09
  • 1
    @zlakad I think that is shown in the image displayed when clicking on the link "CLICK HERE TO SEE..." – Stefan Freitag Jan 03 '18 at 19:14
  • Hello @Stefan Freitag, the registration is created in the main method and i pass it as an argument to get to the needed place... **MAIN METHOD --->** UserRegistry registration = new UserRegistry(); frame = new ServerFrame(registration); **SERVER FRAME --->** In the constructor I set the UserRegistry as an argument and then I use ----->this.registration = registration; and I pass it to the panel MainPanel main_Panel = new MainPanel(registration) ---> **MAIN PANEL ---> **I do the same in constructor and then i call a method **SIGNUP METHOD --->** calls registration.addUser(new User() – Loizos Vasileiou Jan 03 '18 at 20:47
  • @zlakad I call the users.add(aUser); in the **UserRegistry.java -> method: public void addUser(User aUser);** – Loizos Vasileiou Jan 03 '18 at 20:53
  • In UserRegistry.java, method isn't called, it is declared. Also, from your picture, it is said that some exception occurred in MainPanel.java line 202? – zlakad Jan 03 '18 at 20:59
  • @LoizosVasileiou please check if the registration is null. – Stefan Freitag Jan 03 '18 at 21:06
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Stefan Freitag Jan 03 '18 at 21:07
  • Thanks for the quick reply @zlakad .. What do u mean is not called, it is declared??The line 202 is where I call the method that calls the registration.addUser(new User()); – Loizos Vasileiou Jan 03 '18 at 21:08
  • @StefanFreitag you are a god.. I used if(registration != null){ frame = new ServerFrame(registration); } and fixed my problem – Loizos Vasileiou Jan 03 '18 at 21:15

2 Answers2

0

Is your 'registration' variable in MainPanel.java initialized? By the way, you do not need to call setters inside User constructor and you also do not need that part in UserRegistry.java:

private User user;
private String name = "", password = "";
private boolean conn = false;
Aniruddha Das
  • 20,520
  • 23
  • 96
  • 132
Matus Danoczi
  • 137
  • 1
  • 10
0

As shown in the provided screenshot, the registration is null. Please fix this.

Stefan Freitag
  • 3,578
  • 3
  • 26
  • 33