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

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.

Follow 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 Guidance as a whole.

The problem I'm having is the error:

  

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

I left a comment on the lines that IDE is pointing out that it is in trouble. But by the way of doubts follow them below:

calc.setNumerosSomar(a);

numerosSomar[i] = input.nextDouble();

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

The for loop is 0 < qntNumeros(que vale 2) . Here seems to me no mistake at all.

I also do not see error in line numerosSomar[i] = input.nextDouble(); , because I really believe this is how you save a value in a double-type vector.

Looking at this question Exception in thread "main" java.lang. NullPointerException I believe I am trying to write data to a null state, and that I need to initialize the vector before. But I did not understand how I should initialize the vector before.

    
asked by anonymous 24.05.2016 / 04:05

1 answer

1

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

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 the change and it all worked OK

Note: this "100" value inside the array declaration is the maximum value that it can allocate either or "How many numbers do you want ..."

    
24.05.2016 / 04:32