简体   繁体   English

java,从方法返回空值

[英]java,returning null value from method

I need to return finalString value for input operator name.我需要为输入运算符名称返回 finalString 值。 where,internalPrestring is fixed for specific operator,internalDigit would be retrieved from getting operator name.then all of'em would be added to finalString.其中,internalPrestring 是针对特定运算符固定的,internalDigit 将从获取运算符名称中检索。然后所有的都将添加到 finalString。 but it is giving null, i can't understand the problem但它给出了空值,我无法理解这个问题

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

    }
}

You are having only four lines that deals with your CallManager class:您只有四行处理CallManager类:

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

The reason why you are getting null :你得到 null 的原因:

  1. You are using a default constructor right and there processing is done in it.您正在使用默认构造函数,并在其中完成处理。 So this is not a problem所以这不是问题
  2. Now on next line you call setOperator method which has this code:现在在下一行调用具有以下代码的setOperator方法:

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

    Now here you are only setting thw variable operatorName and nothing else.现在在这里您只设置变量operatorName而没有其他任何内容。 So all other variables are null as you not doing any processing or something that will initialize them to something.所以所有其他变量都是空的,因为你没有做任何处理或将它们初始化为某些东西的东西。

  3. So when you print clm.internalPreString and clm.preString you get null as they are not initialized.因此,当您打印clm.internalPreStringclm.preString您会得到 null,因为它们未初始化。 But try printing clm.operatorName and it will print the operator name that you passed and was initialzed inside your method setOperatorName .但是尝试打印clm.operatorName ,它会打印您传递并在您的方法setOperatorName的操作员名称。

So as you have defined so many method inside your class, use them so that all the variables are set as per your logic因此,当您在类中定义了如此多的方法时,请使用它们以便根据您的逻辑设置所有变量

UPDATE更新

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

Don't you think that you should call any of the function instead of the string variables using object.你不认为你应该调用任何函数而不是使用对象的字符串变量吗?

You are just calling one function that is你只是调用一个函数

public void setOperatorName(String getMeFromPreferences) {

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

You are calling default constructor with out any variable setting there, You had not initialized any String you are calling form object.您正在调用没有任何变量设置的默认构造函数,您尚未初始化您正在调用表单对象的任何字符串。

I think you should call any of the function eg我认为你应该调用任何函数,例如

public int getOperatorBasedInternalDigit(String operatorName)

OR或者

public String getString(String toBeInserted, int inetrnalDigit) 

Then you will get some string as you are expecting ... Hope this will help you.然后你会得到一些你期望的字符串......希望这会帮助你。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM