Exception in thread "main" java.lang.Nullpointerexception: Calculator

Asked

Viewed 239 times

2

I started studying Object Orientation and decided to make a simple calculator program. At the moment I just made the sum to see if the way I was doing is right.

Follows the code:

package javaapplication100;
import java.util.Scanner;

/**
 *
 * @author Marcielli
 */
public class JavaApplication100 {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        Calculadora calc = new Calculadora();

        System.out.println("Digite:\n+ - Somar\n* - Multiplicar\n/ - Dividir\n- - Subtrair");
        String escolha = input.nextLine();

        switch (escolha) {
            case "+":
                System.out.println("Somar quantos numeros? ");
                int a = input.nextInt();            

                calc.setNumerosSomar(a);//Esta acusando erro aqui.
                //calc.setNumerosSomar(calc.qntNumeros = input.nextInt());                
                System.out.println("A soma: "+calc.getNumerosSomar());
                break;
            case "*":
                break;
            case "/":
                break;
            case "-":
                break;
            default:
                break;
        }
    }
}

class Calculadora {

    Scanner input = new Scanner(System.in);

    protected double[] numerosSomar;
    private double[] numerosMultiplicar;
    private double[] numerosDividir;
    private double[] numerosSubtrair;
    public int qntNumeros;
    public double resSoma;
    public double resMultiplicar;
    public double resDividir;
    public double resSubtrair;

    public void setNumerosSomar(int a) {

        this.qntNumeros = a;

        for(int i=0; i<qntNumeros; i++) {

            System.out.println("Digite um numero: ");
            numerosSomar[i] = input.nextDouble(); //Esta acusando erro aqui.

        }

       for(int i=0; i<qntNumeros; i++) {

            resSoma+=numerosSomar[i];

        }     

    }

    public double getNumerosSomar() {

        return resSoma;

    }
}

I’m really not sure if I’m doing anything right, because I’m still trying to understand the concept of Object Orientation as a whole.

The problem I’m having is the mistake:

Exception in thread "main" java.lang.Nullpointerexception.

I left a comment on the lines that the IDE is pointing out that it is in trouble. But just in case they follow below:

calc.setNumerosSomar(a);

numerosSomar[i] = input.nextDouble();

My question here is this, is it a mistake of logic? Because I believe that the problem is not in the method, because if I typed 2 in qntNumeros (variable used to determine when the for will stop), this variable will be valid 2.

The loop for is of 0 < qntNumeros(que vale 2). Here it seems to me there is no mistake.

I also see no mistake in the line numerosSomar[i] = input.nextDouble();, because I really believe that’s how you store a value in a double-type vector.

Looking at that question Exception in thread "main" java.lang.Nullpointerexception I believe that I’m trying to write data into a null state, and that I need to initialize the vector first. But I didn’t understand how I should initialize the vector before.

1 answer

1


The error is simple. You are not declaring the initial value of the number array.

protected double[] numerosSomar = new double[100];
private double[] numerosMultiplicar = new double[100];
private double[] numerosDividir = new double[100];
private double[] numerosSubtrair  = new double[100];

I tested with the change and gave everything OK

Obs.: this value "100" within the array declaration is the maximum value it can allocate or "How many numbers you want..."

  • Wow, really simple. I thought I could leave it open, regardless of how many numbers were typed. Now I get it, thank you. ;)

  • After a search in Collections in Java, they can solve the problem of when we do not know how many values will enter or leave this data list, as is your case. Hug.

  • Oh cool, I’ll look it up. Thanks. Hug.

Browser other questions tagged

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