简体   繁体   中英

java,returning null value from method

I need to return finalString value for input operator name. where,internalPrestring is fixed for specific operator,internalDigit would be retrieved from getting operator name.then all of'em would be added to 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 clm = new CallManager();
clm.setOperatorName("Banglalink");
System.out.println(clm.internalPreString);
System.out.println(clm.preString);

The reason why you are getting 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:

     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. 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. But try printing clm.operatorName and it will print the operator name that you passed and was initialzed inside your method 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 所做的那样设置值。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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