简体   繁体   中英

Null static field even after instantiating

I have a class with static field and static setter function to set its value.

class Intermediate{
  private static Type myObject=null;
  public static void setIntermediate(Type ob){
     myObject=ob;
  }

  public static String  getValue(){
      if(myObject!=null)
        return myObject.getValue();
      else
        return "";   // <== always returning this value

  }


}

Intermediate.getValue() is called by a native code cpp.

In my main activity I am initiating the value as

class myActivity extends Activity{
    void  onCreate(){
        Intermediate.setIntermediate(new subType()); 
    }    
}

Here subType is a subclass of the Type class.

In native side I am calling getValue() of Intermediate class and its myObject is always null ;

You aren't initializing the string property of the subType in your code. Your onCreate() method is doing this:

new subType()

So when Intermediate.getValue() is called you are hitting this line

return myObject.getValue();

Your can fix this by doing this instead

void  onCreate(){
    Type t = new subType();
    t.setValue("whatever string you want");
    Intermediate.setIntermediate(t); 
}  

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