首页 文章

String的NullPointerException

提问于
浏览
0

我仍然是java的新手并且没有挂起这个NullPointerException的东西......我想我可能正确地初始化变量有问题 .

所以我在我的 class 中有这些实例变量:

protected String sequence;
protected ArrayList<Character> set = new ArrayList<Character>();
protected char[] op_1;
protected char[] op_2;
protected char[] ans;

这是他们在构造函数中初始化的方式:

public Puzzle (String puzzle_equation){
        int i = puzzle_equation.indexOf('+');
        int j = puzzle_equation.indexOf('=');

        String operand_1 = puzzle_equation.substring(0, i-1);   
        String operand_2 = puzzle_equation.substring(i+1, j-1);
        String answer = puzzle_equation.substring(j+1);

        op_1= operand_1.toCharArray();      
        op_2= operand_2.toCharArray();      
        ans= answer.toCharArray();          


        //initializing set with all the letters existing in the equation without repetition 
        for (char c : op_1){
            if (! set.contains(c)) {
                set.add(c);
            }
        }

        for (char c : op_2){
            if (! set.contains(c)) {
                set.add(c);
            }
        }

        for (char c : ans){
            if (! set.contains(c)) {
                set.add(c);
            }
        }


        //sequence= " ";  --> at 1st i didn't initialize sequence at all in the cunstructor


    }

这是一个类的方法:

protected void puzzleSolve(int k, String s, ArrayList<Character> u){


    for (Character c:u){

    if(k==1){           

        **if(isAnswer(s+u.get(0)))**

            System.out.println(s+u.get(0)+" is the correct sequence."+ '\n');
        return;

    }

    else{
        u.remove(c);
        **puzzleSolve(k-1, s+c , u);**
        u.add(c);
        removeLastChar(s);
    }

    } 

}

还有另一种方法只使用给定的参数调用上面的方法:

**puzzleSolve(set.size(), sequence , set);**

最后一种方法是答案:

protected boolean isAnswer(String seq){

    int[] digitAssign =new int[seq.length()];

    for (int z=0; z<digitAssign.length; z++){   
        digitAssign[z] = z;
    }

    char[] seqArray = seq.toCharArray();


    String op_1_to_digit=null ;
    String op_2_to_digit=null  ;
    String ans_to_digit=null ;

    //converts the letters of words to the assigned digits to each letter

    **for(int n=0; n<op_1_to_digit.length(); n++)**
        for(int f=0; f<seqArray.length ; f++){
            if (op_1[n] == seqArray[f]) 
                op_1_to_digit += digitAssign[f];
        }


    //op_2
    for(int n=0; n<op_2_to_digit.length(); n++)
        for(int f=0; f<seqArray.length ; f++){
            if (op_2[n] == seqArray[f]) 
                op_2_to_digit += digitAssign[f];
        }

    //ans
    for(int n=0; n<ans_to_digit.length(); n++)
        for(int f=0; f<seqArray.length ; f++){
            if (ans[n] == seqArray[f]) 
                ans_to_digit += digitAssign[f];
        }


    int x= Integer.parseInt(op_1_to_digit);
    int y= Integer.parseInt(op_2_to_digit);
    int l=  Integer.parseInt(ans_to_digit);

    return (x+y==l);

}

我在**指示的行上出现NullPointerException错误

pleeeeeeaaase帮助我 . 我真的很困惑 .

附:我尝试将序列onece初始化为sequence = null; &once like sequence =“”;并且“”对于op_1_to_digit等等,但后来我得到了OutOfBoundry异常!

1 回答

  • 2

    当你说

    String op_1_to_digit = null;
    

    你不能马上打电话给 op_1_to_digit.length()

    for(int n=0; n<op_1_to_digit.length(); n++)
    

    因为 Stringnull .

相关问题