Issue
I try to make a program to register users, so I created an array of users with the attributes: Usuario
(User ID), nombre
(name) and contraseña
(password), and everytime I create a new user the program has to ensure that the User ID hasn't registered before, if it's already registered the program must reject the user register until I put a different UserID, every Object created(every user with it's attributes) is saved in it's respective position of the array, so I create a method with these functions (int contador
will count the times I register a User, the array has a maximum of 10 so contador
<10):
public void registrarUsuario(int contador){
int f = contador;
usuario = t3.getText();
nombre = t4.getText();
contraseña = t5.getText();
for(int i=0; i<users.length; i++){
\line 43 with error if(users[i].getUsuario() == usuario){
JOptionPane.showMessageDialog(null, "El nombre de usuario ya ha sido utilizado");
}
}
if(this.t6.getText().equals(this.t5.getText())){
Usuario user = new Usuario(usuario, nombre, contraseña);
users[contador] = user;
JOptionPane.showMessageDialog(null, "El usuario se ha registrado con exito!");
this.t3.setText("");
this.t4.setText("");
this.t5.setText("");
this.t6.setText("");
}else{
JOptionPane.showMessageDialog(null, "ERROR: las contraseñas no coididen");
this.t5.setText("");
this.t6.setText("");
}
}
but when I run the program I get this error:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at proyecto1.Proyecto1.registrarUsuario(Proyecto1.java:143)
( see above to line 143)
Solution
I got what you're trying to achieve but I didn't fully understand your method because you didn't send all the classes, so i'll send you how i solved it and if you want you can resend us all your classes and code to have a full view :)
My code: (of course you can edit it to be interactive with the user, here i'm showing the general idea only)
//User class
package userArray;
public class User {
private int id;
private String name;
private String password;//there is special classes for password in java, not going to use them in this problem
public User(int id, String name, String password) {
super();
this.id = id;
this.name = name;
this.password = password;
}
//getters and setters + toString()
//UsersList class
package userArray;
import java.util.*;
public class UsersList {
ArrayList<User> users = new ArrayList<User>();
//The original function
public User createUser(int id, String name, String password) throws Exception {
for(User u : this.users) {
if(id == u.getId())
throw new Exception("User already exists...");
}
User u = new User(id,name,password);
this.users.add(u);
return u;
}
//The function used to wrap the original function in try/catch each time executed
public User registerUser(int id, String name, String password) {
try {
return createUser(id,name,password);
} catch (Exception e) {
System.out.println("ID "+ id +" for user "+name+" is for an existing user...");
return null;
}
}
public UsersList() {
super();
this.users = users;
}
// + toString()
//Main class
package userArray;
public class Main {
public static void main(String[] args) {
UsersList list = new UsersList();
list.registerUser(1, "Kevin", "Kevin123");
list.registerUser(2, "Sarah", "Sarah123");
list.registerUser(1, "Steven", "Steven123");
list.registerUser(3, "Stephany", "Stephany123");
list.registerUser(3, "Alex", "Alex123");
System.out.println("The list is: ");
System.out.println(list.toString());
}
}
The execution result:
ID 1 for user Steven is for an existing user...
ID 3 for user Alex is for an existing user...
The list is:
UsersList [users=[User [id=1, name=Kevin, password=Kevin123], User [id=2, name=Sarah, password=Sarah123], User [id=3, name=Stephany, password=Stephany123]]]
Answered By - Elie Bsaibes
Answer Checked By - Candace Johnson (JavaFixing Volunteer)