0

I am new to programming and I'm starting to create a simple calculator in Java, but I keep getting an error on the line

    for (int i = 0; i < user_input.length(); i++)

The error says:

Exception in thread "main" java.lang.NullPointerException

How can I fix this problem?

import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.util.Scanner;

public class stringCalculator
{
    public static ArrayList<String> input = new ArrayList<String>();
    public static ArrayList<String> calcOperators = new ArrayList<String>();
    public static ArrayList<Integer> calcOperands = new ArrayList<Integer>();
    public static String user_input;
    public static String first;
    public static int int1;
    public static String char1;
    public static int int2;
    public static String next;
}

public static void input()
{
    Scanner user_input = new Scanner(System.in);
    int1 = user_input.nextInt();
    char1 = user_input.next();
    int2 = user_input.nextInt();
    next = user_input.nextLine();
}

public void calcOperators()
{
    for (int i = 0; i < user_input.length(); i++)
    {
        if (char1 == "+")
        {
            calcOperators.add(char1);
        }
        else if (char1 == "-")
        {
            calcOperators.add(char1);
        }
        else if (char1 == "char1")
        {
            calcOperators.add(char1);
        }
        else if (char1 == "/")
        {
            calcOperators.add(char1);
        }
    }


public void calcOperands()
{
    for (int i = 0; i < user_input.length(); i++)
    {
        if (int1 == 1 || int2 == 1)
        {
            calcOperands.add(1);
        }
        else if (int1 == 2 || int2 == 2)
        {
            calcOperands.add(2);
        }
        else if (int1 == 3 || int2 == 3)
        {
            calcOperands.add(3);
        }
        else if (int1 == 4 || int2 == 4)
        {
            calcOperands.add(4);
        }
        else if (int1 == 5 || int2 == 5)
        {
            calcOperands.add(5);
        }
        else if (int1 == 6 || int2 == 6)
        {
            calcOperands.add(6);
        }
        else if (int1 == 7 || int2 == 7)
        {
            calcOperands.add(7);
        }
        else if (int1 == 8 || int2 == 8)
        {
            calcOperands.add(8);
        }
        else if (int1 == 9 || int2 == 9)
        {
            calcOperands.add(9);
        }
        else if (int1 == 0 || int2 == 0)
        {
            calcOperands.add(0);
        }
    }
}

}

public class Main
{
    public static void main(String[] args)
    {
        stringCalculator c = new stringCalculator();
        c.input();
        c.calcOperators();
        c.calcOperands();
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Curtis Thompson
  • 63
  • 1
  • 2
  • 10

3 Answers3

1

I am kind of confused in here why you have

public static String user_input

and then

Scanner user_input = new Scanner(System.in);
int1 = user_input.nextInt();
char1 = user_input.next();
int2 = user_input.nextInt();
next = user_input.nextLine();

And finally you are using the same var name for strings and ints in loops:

for (int i = 0; i < user_input.length(); i++)
for (int i = 0; i < user_input.length(); i++)

I would highly recommend refactoring this code.

        .....
    private ArrayList<String> input = new ArrayList<String>();
    private ArrayList<String> calcOperators = new ArrayList<String>();
    private ArrayList<Integer> calcOperands = new ArrayList<Integer>();
    private String user_input;
    private String first;
    private String next;
    private String char1;
    private int integer2;
    private int integer1;
}

public static void input()
{
    Scanner input = new Scanner(System.in);
    int int1 = input.nextInt();
    int int2 = input.nextInt();
    string str = input.next();
    string nxt = input.next();

    setInteger1(int1);
    setInteger2(int2);
    setStringFirst(str);
    setStringNext(...);
    ....
    // And so on
}

private void setInteger1(int int1) {
    this.integer1 = int1;
}

private Integer getInteger1() {
    return this.integer1;
}

private void setInteger2(int int2) {
    this.integer2 = int2;
}

private Integer getInteger2() {
    return this.integer2;
}

private void setStringFirst(String fst) {
    this.first = fst;
}

private String getStringFirst() {
    return this.first;
}

// And so on. Create all get and set methods for each global variable and for future
// reference do not use variable names that are the same as method, names
// and try to use more meaningful variable names. In fact, if you look at
// Java naming conventions it would do you good.

Could you also tell us perhaps what these loops are meant to do? As I don't really understand what is this? Do you want to iterate over an array of "things" and match each operation to given array element? Or do you just want a single element to match one of the operations?

Side note: Class names should be capitalized:

public class StringCalculator

==================================================================================

OK, I have made a simple program that allows you to add a string into an array and then display it. It is based on what you were doing although you will see it is structured differently. This should give you a head start and allow you to implement this further and finish whatever you are doing.

    public class Thing {

        private String  operator;

        private void getUserInput() {
            Scanner input = new Scanner(System.in);
            if(input.hasNextInt()) {
                System.out.println("I have entered an integer: " + input.nextInt());
            }
            else {
                setOperator(input.nextLine());
                addOperators();
                System.out.println("I have entered a string: " + getOperator());
            }
            displayThing();
        }

        private ArrayList<String> addOperators() {
            ArrayList<String> operatorsList = new ArrayList<String>();

            if(getOperator().equals("+")) {
                operatorsList.add(operator);
            }
            if(getOperator().equals("-")) {
                operatorsList.add(operator);
            }
            else {
                operatorsList.add(getOperator());
            }
            return operatorsList;
        }

        private void displayThing() {
            System.out.println(addOperators());
        }

        public static void main(String[] args) {
            Thing program = new Thing();
            program.getUserInput();
        }

        // Setters and getters
        private void setOperator(String operator) {
            this.operator = operator;
        }

        private String getOperator() {
            return this.operator;
        }

    }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Maciej Cygan
  • 5,351
  • 5
  • 38
  • 72
  • Thanks for the reply. The loops are meant to iterate through the user input and if it includes an integer, add to the calcOperands arraylist and if it includes +,-,*,/ add to the calcOperators arraylist – Curtis Thompson Dec 15 '13 at 13:02
  • I still seem to be getting the same error Exception in thread "main" java.lang.NullPointerException – Curtis Thompson Dec 15 '13 at 15:19
  • 1
    @user3104303 its probably because a variable you are using is null at the time of invocation. Make sure that any global variable(if any) is not static, and make sure you are using the correct variable. Also if the variable is set by Setter method and retrived by getter, make sure that these work to – Maciej Cygan Dec 15 '13 at 16:46
0

You have a static String named user_input defined as:

public static String user_input; // This is null and what the for loop is reading

Set a value to that string when you're using your Scanner... which you shouldn't have also named user_input.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

You have declared the public static String user_input; which is used in a for loop and not initialised, so thus the exception. Initialise it in your input method, like this.user_input=user_input.nextLine().

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Deepak
  • 2,287
  • 1
  • 23
  • 30