我必須使用公共類和主驅動程序控制臺創建一個簡單的登錄程序。使用類的Java登錄程序
這裏是我的代碼:
主要
import java.util.Scanner;
public class main
{
public static void main(String[] args)
{
Scanner input = new Scanner (System.in);
String username;
String password;
System.out.println("Welcome to your Social network site!");
System.out.println("\nEnter your username and password to login to your account.");
System.out.println("Username: ");
username = input.nextLine();
System.out.println("Password: ");
password = input.nextLine();
UserAccount login = new UserAccount(username, password);
if(login.checkPassword())
System.out.println("You are logged in!");
else
System.out.println("The username and password you entered are incorrect.");
}
}
用戶類別
public class UserAccount
{
private String username;
private String password;
private String[][] accounts = {{"anthony", "supernova"},{"steve", "java1"}};
public UserAccount(String username, String password)
{
String user = username;
String pass = password;
boolean active;
}
public boolean checkPassword()
{
if((username.equals(accounts[0][0])) && (password.equals(accounts[0][1])))
return true;
else
return false;
}
public void deactivateAccount()
{
boolean active = false;
}
}
的問題是我不斷收到此錯誤後,我輸入正確的信息: 異常線程「main 「java.lang.NullPointerException在UserAccount.checkPassword(UserAccount.java:19)at main.main(main.java:25)