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;
    }
}
avatar rafly24
@rafly24

2 Kontribusi 0 Poin

Diperbarui 1 tahun yang lalu

1 Jawaban:

<div>error tersebut karena variabel userDB bernilai null.<br>inisialisasi terlebih dahulu variabel userDB dengan array user pada constructor kelas User,&nbsp;</div><pre>public User() { userDB = user; }</pre><div><br>coba hal diatas apakah bisa atau tidak,,,&nbsp;</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[][]{
        {&quot;admin&quot;, &quot;admin&quot;, &quot;0&quot;, &quot;true&quot;},
        {&quot;202110370311147&quot;, &quot;UMM_a2021&quot;, &quot;1&quot;, &quot;true&quot;},
        {&quot;2022110370311145&quot;, &quot;UMM_a2021&quot;, &quot;1&quot;, &quot;false&quot;}
};

public User() {
    userDB = user;
}

private void login(String username, String password) {
    for (Object[] user : userDB) {
        if (user[0].equals(username) &amp;amp;&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(&quot;Menu for Admin:&quot;);
        System.out.println(&quot;1. Update Status of Student&quot;);
        System.out.println(&quot;2. Update Password for Student&quot;);
        System.out.println(&quot;3. Logout&quot;);
    } else {
        System.out.println(&quot;Menu for Student:&quot;);
        System.out.println(&quot;1. Change Password&quot;);
        System.out.println(&quot;2. Logout&quot;);
    }
}

public void updateStatus(int index, boolean status) {
    if (admin) {
        userDB[index][3] = String.valueOf(status);
    }

<br></pre>

avatar yukari06
@yukari06

137 Kontribusi 66 Poin

Dipost 1 tahun yang lalu

Tanggapan

terimakasih tapi itu type data methodnya tidak ada mungkin bisa bantu lebih jelas?

Login untuk ikut Jawaban