Exception in Thread "Main" java.land.Nullpointerexception com.Login.Login.mai(Login.java:18)

Asked

Viewed 58 times

1

Today I tried to make a login system (again), only this time I used a array one-dimensional to guard my values. When I ran my program, Eclipse had an error (null pointer to be more specific), my theory is that assign a value to array caused the error.

Follow my code below:

package com.Login;
import java.util.Scanner;
import java.util.Random;

public class Login {
    Random r = new Random();

    String[] valores;
    boolean isSessionValid;
    int sessID = r.nextInt();

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Login log = new Login();


        System.out.println("Digite o nome do usuário:");
        log.valores[1] = in.nextLine();
        System.out.println("Digite sua senha:");
        log.valores[2] = in.nextLine();

        System.out.println("Digite novamente a senha:");
        log.valores[3] = in.nextLine();
        if(log.valores[3].equals(log.valores[1])){
            System.out.println("Você está logado!");
            log.isSessionValid = true;
            System.out.println("Sua Sessão é:"+log.sessID);
        }
    }

}

Simply the code assigns the user name to log.valores[1] and the password to log.valores[2] and log.valores[3] serves only to confirm the password.

  • 1

    Just a little observation, vectors start from Indice 0, it is good to get used to and adapt to it.

1 answer

4


  • 1

    I’m going to do a canonical one on this, every day there’s a question about this and it’s very basic. So it’s better to close as a duplicate. This is practically a typo (although technically not).

  • I had my finger on the post when the notification of your reply appeared kkkk

  • Thank you all :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.