0

I have to create a simple login program using a public class and the main driver console.

Here's my code:

Main

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.");
    }


}

User Class

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;
    }

}

The problem is I keep getting this error after I input the correct info: Exception in thread "main" java.lang.NullPointerException at UserAccount.checkPassword(UserAccount.java:19)at main.main(main.java:25)

3 Answers 3

1

You're not assigning your object properties in your UserAccount constructor. Change as follows:

public UserAccount(String username, String password) {
    this.username = username;
    this.password = password;
}

Furthermore, if you plan to do something with that active boolean, you'll have to add it as a property to your class as well. Just declaring it in your constructor/methods like you're doing now will have no effect.

Sign up to request clarification or add additional context in comments.

Comments

0

Your UserAccount constructor should be initializing the class variables. Like this -

 public UserAccount(String username, String password) 
    {
        this.username = username;
        this.password = password;
        boolean active;
    }

HTH

Comments

0
public UserAccount(String username, String password) 
{
   this.username = username;
   this.password = password;
   // boolean active;   //not a good coding standard 
   //in constructor only initialization of class variables  must be done

}

this will show how to use constructor-- good coding standard (important : constructor must be used only for initialization not for delcaration)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.