3

I am trying to make a user login system at the moment and i need help on reading objecs and their fields of an existing arraylist in another class in order to authenticate.Belowis the code for the object constructor,

public class User {
    public String UserName ;
    public String UserSurname ;
    public String UserUsername ;
    public String UserPassword ;
    public int UserType ; // 0 for cashier 
                          // 1 for manager

public User() {}

public User(String UserName, String UserSurname, String UserUsername, String UserPassword, int UserType) {
    this.UserName = UserName;
    this.UserSurname = UserSurname;
    this.UserUsername = UserUsername;
    this.UserPassword = UserPassword;
    this.UserType = UserType;
}
/* 
Getters and Setters
*/

Here is my "database " of users and itemlist (Note-I have a constructor for these obj)

public class DataBase {
    ArrayList<User> userlist;
    ArrayList<Item> itemlist;


    public DataBase(){
        User user1 = new User("example","example","example","example",1) ;
        User user2 = new User("sda","fas","gdf","vcx",0) ;
        User user3 = new User("htr","ytr","vxc","xaxxzx",0) ;
        User user4 = new User("dag","gdf","dfgfgd","thrhf",0) ;

        ArrayList<User> userlist = new ArrayList<User>();
        userlist.add(user1);
        userlist.add(user2);
        userlist.add(user3);
        userlist.add(user4);

        ArrayList<Item> itemlist = new ArrayList<Item>() {};
        Item item1 = new Item(1,"sadas",25.0) ;
        Item item2 = new Item(1,"dcxz",25.0) ;
        Item item3 = new Item(1,"czx",25.0) ;
        Item item4 = new Item(1,"zxccxz",25.0) ;
        Item item5 = new Item(1,"czx",25.0) ;
        Item item6 = new Item(1,"tertgdf",25.0) ;
        Item item7 = new Item(1,"zxcfes",25.0) ;
        Item item8 = new Item(1,"erwt",25.0) ;
        Item item9 = new Item(1,"gfdvcx",25.0) ;
        Item item10 = new Item(1,"hjfgh",25.0) ;
        itemlist.add(item1);
        itemlist.add(item2);
        itemlist.add(item3);
        itemlist.add(item4);
        itemlist.add(item5);
        itemlist.add(item6);
        itemlist.add(item7);
        itemlist.add(item8);
        itemlist.add(item9);
        itemlist.add(item10);
    }

    public DataBase(ArrayList<User> userlist, ArrayList<Item> itemlist) {
        this.userlist = userlist;
        this.itemlist = itemlist;
    }
    public ArrayList<User> getUsers() {
        return userlist;
    } //end get Users

    public ArrayList<Item> getItems(){
        return itemlist;
    } //end get Items
} // end class

And here is what i have done to read from the ArrayList i showed above .. :

String username = UsernameField.getText();
        String pass = PasswordField.getText();

        try
        {   

        users = new DataBase();
        ArrayList<User> userlist = users.getUsers();           
        for(User d : userlist){
        if(d.getUserUsername() != null && d.getUserUsername() == username && d.getUserPassword() != null && d.getUserPassword() == pass){
        Succed success = new Succed();
        success.setVisible(true);
        }
        else {
            failure fail = new failure() ;
            fail.setVisible(true);
        }
            }// end for 
        }/* end try  */ catch (NullPointerException ex) {
        }

I would appriciate if someone could help me to solvet this.I've spent a lot of time trying to solve this problem.

The Action Listener code :

LoginButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            LoginButtonActionPerformed(evt);
        }
    });
HasS
  • 201
  • 1
  • 17
  • as a sidenote, i´d suggest to use a `Enum` instead of an `int` to identify if the user is a cashier or a manager. – SomeJavaGuy Jan 15 '16 at 11:07
  • 3
    Possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – SomeJavaGuy Jan 15 '16 at 11:09
  • When do you populate the two lists in _DataBase_ ? – Arnaud Jan 15 '16 at 11:12
  • Hello @Berger thank you for comment , checck the code again , i added the list population – HasS Jan 15 '16 at 11:20
  • @KevinEsche thank you for your support , latter when i implement other classes i might change that , but at the moment need help with this :/ – HasS Jan 15 '16 at 11:20
  • @KevinEsche I cant find an correct answer in the post you mentioned , becouse i need to compare the String from the text field and passwordfield with the objects username and their passwords , – HasS Jan 15 '16 at 11:29
  • @HasS you are comparing the references of the String values, and not the value of the `String` itself. If you compare `Objects` then you need to use the `equals` method. – SomeJavaGuy Jan 15 '16 at 11:32
  • @KevinEsche I understand , d.getUserUsername().equals(username) is what i will use , but i need to know , is the code for reading my DataBase objects acceptable , or my mistake is on that part of code?? – HasS Jan 15 '16 at 11:43
  • @HasS I found a missing line in your question `public Database(){` This was quite misleading as I had to figure out the code using the braces. – Vamshi Krishna Alladi Jan 15 '16 at 13:38
  • @VamshiKrishnaAlladi code edited :) now i only need to understand how to make the login works – HasS Jan 15 '16 at 14:17

1 Answers1

1

One should never compare strings for equality using ==, instead make use of .equals(). == compares the references not the contents of the string variables. For comparison of contents .equals() or .equalsIgnoreCase() should be used based on the requirement.

For reference see this

Update:

Right now you are running a loop on userlist and checking if the entered username is in the list. Instead do it like this:

Succed success = new Succed();
failure fail = new failure();

for(User d : userlist){
    if(d.getUserUsername().equals(username) && d.getUserPassword().equals(pass)){
        success.setVisible(true);
        break;
    }
}

if(!success.isVisible()){
    fail.setVisible(true);
}

assuming isVisible() is defined on class Succed which return true if the success is set to visible and return false in other case.

  • Right on. In his case, something like this: `if(d.getUsername() != null && d.getUsername().equals(username) && d.getPassword() != null && d.getPassword().equals(pass))` ... – vikingsteve Jan 15 '16 at 11:35
  • Just `if(username.equals(d.getUsername()) && pass.equals(d.getPassword()))` would do, As `.equals()` method implicitly does the "not `null`" validation. – Vamshi Krishna Alladi Jan 15 '16 at 11:47
  • @VamshiKrishnaAlladi I changed that part of code but the part of code when i read the DataBase objects is part of an action listener button (login ) and when i click it , it does nothing ... maybe the code when i try to read the objecs is wrong ??... – HasS Jan 15 '16 at 11:55
  • @HasS can you post the code of the action listener? – Vamshi Krishna Alladi Jan 15 '16 at 11:59
  • @VamshiKrishnaAlladi you are presuming `username` is not null. – vikingsteve Jan 15 '16 at 12:02
  • @VamshiKrishnaAlladi LoginButton.setText("Login"); LoginButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { LoginButtonActionPerformed(evt); } }); – HasS Jan 15 '16 at 12:03
  • @vikingsteve my bad. its actually the reverse of that. I was thinking the value `username` is from DB and other one as the value from text field. so the code would be `if(d.getUsername().equals(username) && d.getPassword().equals(pass))`. – Vamshi Krishna Alladi Jan 15 '16 at 12:07
  • @HasS can you update the Question itself with your code related to action listener. so that it would be more readable. also if you can, include the `LoginButtonActionPerformed()` code as well. – Vamshi Krishna Alladi Jan 15 '16 at 12:10
  • Is the 3rd snippet the code for the `LoginButtonActionPerformed()`? – Vamshi Krishna Alladi Jan 15 '16 at 12:11
  • Yes , i think everything is fine about that code , check it out ive updated the question @VamshiKrishnaAlladi – HasS Jan 15 '16 at 12:14
  • @VamshiKrishnaAlladi well in that case you are assuming `d.getUsername()` and `d.getPassword` are not null... ;) – vikingsteve Jan 15 '16 at 12:15
  • @HasS try replacing `catch (NullPointerException ex) { }` with `catch(Exception e){ e.printStackTrace();}`, also modify the condition check with `if(d.getUsername().equals(username) && d.getPassword().equals(pass))`. – Vamshi Krishna Alladi Jan 15 '16 at 12:15
  • @vikingsteve yes I am assuming the values in DB are not `null`. correct me if I am wrong @HasS – Vamshi Krishna Alladi Jan 15 '16 at 12:17
  • in this case debug console prints error : java.lang.NullPointerException at View.GuiLogin.LoginButtonActionPerformed(GuiLogin.java:146) at View.GuiLogin.access$000(GuiLogin.java:17) at View.GuiLogin$1.actionPerformed(GuiLogin.java:50) @VamshiKrishnaAlladi – HasS Jan 15 '16 at 12:21
  • @HasS I have a question for you. – Vamshi Krishna Alladi Jan 15 '16 at 12:21
  • @VamshiKrishnaAlladi yes , the values in DB are not null – HasS Jan 15 '16 at 12:22
  • check what line number 146 in GuiLogin.java is. – Vamshi Krishna Alladi Jan 15 '16 at 12:22
  • @VamshiKrishnaAlladi for(User d : userlist){ // line 146 – HasS Jan 15 '16 at 12:24
  • Just before the for each loop can you verify if userlist is not null. – Vamshi Krishna Alladi Jan 15 '16 at 12:34
  • @VamshiKrishnaAlladi i wrote a if statement before "for " loop like this if(userlist != null) for(User d : userlist){ in this case when i click button it does nothing , and it does not print any error in debug console – HasS Jan 15 '16 at 12:41
  • so your `user.getUsers()` is returning null. check if you are initializing your DB properly? I mean check what your DB constructor is doing. – Vamshi Krishna Alladi Jan 15 '16 at 12:44
  • All my DB code is on my post , can you check it and tell me what is missing? Becouse I have no idea what I did wrong @VamshiKrishnaAlladi – HasS Jan 15 '16 at 12:50
  • what is the constructor doing? add that code also to the question. – Vamshi Krishna Alladi Jan 15 '16 at 12:51
  • @VamshiKrishnaAlladi i added the constructor to the code , anyway i dont understand ur question , is there any way to send you everything i've done so far? – HasS Jan 15 '16 at 12:56
  • [Just for sake of mentioning] basically you were re-declaring the userlist and itemlist values in constructor. that is why the return value from getter is null. – Vamshi Krishna Alladi Jan 15 '16 at 13:29
  • Problem solved .. ! thank you a lot for your useful help ! – HasS Jan 15 '16 at 14:40