使用Scanner的NullPointerException

时间:2013-12-15 12:25:59

标签: java nullpointerexception

我是编程的新手,我开始在java中创建一个简单的计算器,但我一直在线上出错

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

错误说:

Exception in thread "main" java.lang.NullPointerException

有人可以帮帮我吗?

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

3 个答案:

答案 0 :(得分:1)

我有点困惑,为什么你有

public static String user_input

然后

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

最后你在循环中使用相同的var名称和ints

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

我强烈建议重构这段代码:我会在一分钟内给你发一个提示

编辑:

        .....
    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 var and for future 
// reference do not use variable names that are 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.

你能告诉我们这些循环的意义吗?因为我真的不明白这是什么?你想迭代一堆“事物”并将每个操作与给定的数组元素相匹配吗?或者你只是想要单个元素来匹配其中一个操作?

附注:班级名称应为CAPITALIZED。

public class StringCalculator

=============================================== =================================== 好吧,我已经制作了一个简单的程序,允许您将字符串添加到数组中,然后显示它。它基于你正在做的事情,虽然你会看到它的结构不同。这应该为您提供一个良好的开端,并允许您进一步实现这一点,并完成您正在做的任何事情。

    public class Thing {

    private String  operator;
    private void getUserInput(){
        Scanner input = new Scanner(System.in);
        if(input.hasNextInt()){
            System.out.println("i have entered integer : " + input.nextInt());
        }else {
            setOperator(input.nextLine());
            addOperators();
            System.out.println("i have entered 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;
    }
}

答案 1 :(得分:0)

您有一个名为user_input的静态字符串,定义为

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

在您使用扫描仪时为该字符串设置一个值...您不应将其命名为user_input

答案 2 :(得分:0)

您已声明for {循环中使用的public static String user_input;并未初始化,因此异常。在input方法中将其初始化为this.user_input=user_input.nextLine()