java,从方法返回null值

时间:2012-11-22 05:12:20

标签: java

我需要为输入操作符名称返回finalString值。 其中,internalPrestring是针对特定运算符修复的,将从获取运算符名称中检索internalDigit。然后将所有' em添加到finalString。 但是它给出了null,我无法理解这个问题

 import java.io.*;
 import java.lang.*;

 class CallManager
 {


   public static final String postString = "#";

   StringBuilder stringBuilder;

   String internalPreString;
   String preString;
   String middleString;
   String finalString;
   String operatorName;


   int internalDigit;



   //needs to set oprator name
   public void setOperatorName( String getMeFromPreferences)
   {

    operatorName = getMeFromPreferences;
        System.out.println("I got it " + operatorName);
   }


//afeter having operator name need to set inrernal digit for each operator
public void setOperatorBasedInternalDigit(int getIntegerForOperator)
    {
        internalDigit = getIntegerForOperator;
        System.out.println("I got it too " + internalDigit);
    }

//it needs to get string from ocr  
public void setString( String getMeFromOCR )
    {
        middleString = getMeFromOCR;
    }


//preString creator for differnet operator
public String getOperatorBasedPreString(String operatorName)
{
        if(operatorName.equals("Airtel"))
        internalPreString = "787";

        else if(operatorName.equals("Banglalink"))
        internalPreString = "123";

        else if(operatorName.equals("Grameen"))
        internalPreString = "555";

        else if(operatorName.equals("Robi"))
        internalPreString = "111";

        else if(operatorName.equals("TeleTalk"))
        internalPreString = "151";


        stringBuilder.append("*").append(internalPreString).append("*");
        preString = stringBuilder.toString();

        return preString;

}

//get operator name and retrive midlle string's digit size from it
public int getOperatorBasedInternalDigit( String operatorName)
{

        if(operatorName.matches("^Airtel | Grameen | Robi$"))
        internalDigit = 16; 

        else if(operatorName.matches("^Banglalink$"))
        internalDigit = 14;

        else if(operatorName.matches("^TeleTalk$"))
        internalDigit = 13;


        return internalDigit;
}

//check operator-based digit number with input middle string as a number then retrive final string
public String getString( String toBeInserted, int inetrnalDigit)
    {

        if(toBeInserted.length() == internalDigit)
        {

            int counter = 0;
            char [] insertHere  = new char[internalDigit];

            for(int verifier = 0; verifier < internalDigit; verifier ++)
            {
                insertHere[verifier] = toBeInserted.charAt(verifier);
                    if(!Character.isDigit(insertHere[verifier]))
                    break;

            counter ++;
            }

            if(counter == internalDigit)
            {

                    stringBuilder.append(preString).append(toBeInserted).append(postString);
                    finalString = stringBuilder.toString();
                    //to see what i've got finally as input for using this call manager method.it would be removed too
                    System.out.println(finalString);
                    return finalString;
            }

            else
            {
                //this printing could be used in main program
                System.out.println("number is less or more than desired ..... INVALID SCAN");
                System.out.println(middleString);
                //here i will call the method for scan the card again
                //
                //
                   return middleString;
             }
          }


          else
              {
                //this printing could be used in main program
                System.out.println("number is less or more than desired ..... INVALID SCAN");
                System.out.println(middleString);
                //here i will call the method for scan the card again
                //
                //
                return middleString;
              }



    }


}


//tester class that CallManager works rightly or not
class CallManagerDemo
{
    public static void main(String args[]) throws IOException
    {
            BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));



          System.out.println("Enter name of Operator");
          CallManager clm = new CallManager();

          clm.setOperatorName("Banglalink");
          System.out.println(clm.internalPreString);
          System.out.println(clm.preString);

    }
}

3 个答案:

答案 0 :(得分:3)

您只有四行来处理您的CallManager课程:

CallManager clm = new CallManager();
clm.setOperatorName("Banglalink");
System.out.println(clm.internalPreString);
System.out.println(clm.preString);

你得到null的原因:

  1. 您正在使用默认构造函数,并且处理是 完成它。所以这不是问题
  2. 现在在下一行,您调用setOperator方法,该方法包含以下代码:

    public void setOperatorName( String getMeFromPreferences)  
    {  
       operatorName = getMeFromPreferences;  
       System.out.println("I got it " + operatorName);  
    }
    

    现在,您只需要设置变量operatorName而不是其他任何内容。因此,所有其他变量都为null,因为您没有进行任何处理或将其初始化为某些内容。

  3. 因此,当您打印clm.internalPreStringclm.preString时,您会因为未初始化而获得null。但是请尝试打印clm.operatorName,它会打印您传递的操作员名称,并在方法setOperatorName内进行初始化。

  4. 因为您已经在类中定义了这么多方法,所以使用它们以便根据您的逻辑设置所有变量

    <强>更新

    public void setOperatorName( String getMeFromPreferences)  
    {  
         operatorName = getMeFromPreferences;  
    
         //call any methods for example and use the values returned from the method by storing it inside a variable 
         String mystring = getOperatorBasedPreString(String operatorName) 
    }
    

答案 1 :(得分:0)

你不认为你应该使用object来调用任何函数而不是字符串变量。

您只是调用一个

的函数
public void setOperatorName(String getMeFromPreferences) {

        operatorName = getMeFromPreferences;
        System.out.println("I got it " + operatorName);
    }

你正在调用默认构造函数,没有任何变量设置, 您尚未初始化任何正在调用表单对象的String。

我认为你应该调用任何一个函数e-g

public int getOperatorBasedInternalDigit(String operatorName)

OR

public String getString(String toBeInserted, int inetrnalDigit) 

然后你会得到一些你期待的字符串...... 希望这会对你有所帮助。

答案 2 :(得分:0)

您从未为获得NULL值的变量设置变量值。必须在直接访问属性的地方使用术语get / set。阅读Java Programming Style GuideLines更清晰。使用适当的getter和用于获取和设置值的setter,就像您为operatorName所做的那样。