Postingan lainnya
java.lang.NullPointerException: Cannot read the array length because "<local3>" is null
import java.util.Scanner;
import java.util.Arrays;
public class User {
private boolean admin;
private int userIndex;
private Object[][] userDB;
private String username;
private String password;
private int roleUser;
private boolean statusUser;
public void user(String username, String password, int roleUser, boolean statusUser) {
this.username = username;
this.password = password;
this.roleUser = roleUser;
this.statusUser = statusUser;
}
private Object[][] user = new Object[][]{
{"admin", "admin", "0", "true"},
{"202110370311147", "UMM_a2021", "1", "true"},
{"2022110370311145", "UMM_a2021", "1", "false"}
};
private void login(String username, String password) {
for (Object[]User:userDB) {
if (user[0].equals(username) && userDB[1].equals(password)) {
//userIndex = i;
//roleUser = Integer.parseInt(userdb[i][2]);
//statusUser = Boolean.parseBoolean(userdb[i][3]);
//if (roleUser == 0) {
//admin = true;
}
break;
}
}
public void dashboard() {
if (admin) {
System.out.println("Menu for Admin:");
System.out.println("1. Update Status of Student");
System.out.println("2. Update Password for Student");
System.out.println("3. Logout");
} else {
System.out.println("Menu for Student:");
System.out.println("1. Change Password");
System.out.println("2. Logout");
}
}
public void updateStatus(int index, boolean status) {
if (admin) {
userDB[index][3] = String.valueOf(status);
} else {
System.out.println("Access denied! You are not an admin.");
}
}
public void updatePassword(int index, String newPassword) {
if (admin || roleUser == 1) {
userDB[index][1] = newPassword;
} else {
System.out.println("Access denied! You are not authorized to update password.");
}
}
public boolean isPassSymbols(String password) {
return password.matches(".*[!@#$%^&*(),.?\":{}|<>].*");
}
public boolean isPassDigit(String password) {
return password.matches(".*\\d.*");
}
public boolean isUpperCase(String password) {
return password.matches(".*[A-Z].*");
}
public boolean isPassLowerCase(String password) {
return password.matches(".*[a-z].*");
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
User loginSystem = new User();
System.out.println("Login System");
System.out.print("Enter username: ");
String username = scanner.nextLine();
System.out.print("Enter password: ");
String password = scanner.nextLine();
loginSystem.login(username, password);
if (loginSystem.userIndex != -1) {
System.out.println("Login successful!");
loginSystem.dashboard();
} else {
System.out.println("Invalid username or password.");
}
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setRoleUser(int roleUser) {
this.roleUser = roleUser;
}
public void setStatusUser(boolean statusUser) {
this.statusUser = statusUser;
}
}
0
1 Jawaban:
<div>error tersebut karena variabel userDB bernilai null.<br>inisialisasi terlebih dahulu variabel userDB dengan array user pada constructor kelas User, </div><pre>public User() { userDB = user; }</pre><div><br>coba hal diatas apakah bisa atau tidak,,, </div><pre>import java.util.Scanner;
public class User { private boolean admin; private int userIndex; private Object[][] userDB; private String username; private String password; private int roleUser; private boolean statusUser;
public User(String username, String password, int roleUser, boolean statusUser) {
this.username = username;
this.password = password;
this.roleUser = roleUser;
this.statusUser = statusUser;
}
private Object[][] user = new Object[][]{
{"admin", "admin", "0", "true"},
{"202110370311147", "UMM_a2021", "1", "true"},
{"2022110370311145", "UMM_a2021", "1", "false"}
};
public User() {
userDB = user;
}
private void login(String username, String password) {
for (Object[] user : userDB) {
if (user[0].equals(username) &amp;&amp; user[1].equals(password)) {
userIndex = Arrays.asList(userDB).indexOf(user);
roleUser = Integer.parseInt(userDB[userIndex][2].toString());
statusUser = Boolean.parseBoolean(userDB[userIndex][3].toString());
if (roleUser == 0) {
admin = true;
}
break;
}
}
}
public void dashboard() {
if (admin) {
System.out.println("Menu for Admin:");
System.out.println("1. Update Status of Student");
System.out.println("2. Update Password for Student");
System.out.println("3. Logout");
} else {
System.out.println("Menu for Student:");
System.out.println("1. Change Password");
System.out.println("2. Logout");
}
}
public void updateStatus(int index, boolean status) {
if (admin) {
userDB[index][3] = String.valueOf(status);
}
<br></pre>
0
Tanggapan
terimakasih tapi itu type data methodnya tidak ada mungkin bisa bantu lebih jelas?