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

1

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

Below is my code:

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

}

The code assigns the user name to log.valores[1] and the password to log.valores[2] and log.valores[3] is only used to confirm the password.

    
asked by anonymous 09.06.2016 / 23:02

1 answer

3

This error indicates that you are trying to access an object that has not been initialized. Will solve by initializing the vector. Apparently 4 positions are sufficient. Maybe even 3 if it starts at index 0:

String[] valores = new String[4];

The code has other bad things, but I'll stick to the one asked.

    
09.06.2016 / 23:19