简体   繁体   中英

What is wrong with these static getter & setter methods - Android?

I'm befuddled by a very tiny problem in my Android code. All the getters and setters I created in a custom class (ie 'ResultsRoomInfoCustRLYT') give me the following 'Java NullPointerException':

05-31 13:37:13.222  29262-29262/com.whitsoft.stan E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
        at com.whitsoft.stan.mods.ResultsRoomInfoCustRLYT.setSingleRoomsNumberTextValue(ResultsRoomInfoCustRLYT.java:61)
        at com.whitsoft.stan.utils.DataFixer.updateRelevantViewsWithSelectedData(DataFixer.java:48)
        at com.whitsoft.stan.mods.StanListFragment.checkIfTheListAdapterDataHasChanged(StanListFragment.java:98)
        at com.whitsoft.stan.mods.StanListFragment.onActivityCreated(StanListFragment.java:49)
        at android.app.Fragment.performActivityCreated(Fragment.java:1707)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:921)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1075)
        at android.app.BackStackRecord.run(BackStackRecord.java:682)
        at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1455)
        at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5493)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
        at dalvik.system.NativeStart.main(Native Method)

Here is the custom class ('ResultsRoomInfoCustRLYT') where the getters & setters are implemented:

public class ResultsRoomInfoCustRLYT extends RelativeLayout {

LayoutInflater stanInflater;
private static TextView singleRoomsNumberTV, singleRoomsDescTV, vipRoomsNumberTV, vipRoomsDescTV;

public ResultsRoomInfoCustRLYT(Context context) {
    super(context);

    stanInflater = LayoutInflater.from(context);
    initializeAndLayoutChildren();
}

public ResultsRoomInfoCustRLYT(Context context, AttributeSet attrs) {
    super(context, attrs);

    stanInflater = LayoutInflater.from(context);
    initializeAndLayoutChildren();
}

public ResultsRoomInfoCustRLYT(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    stanInflater = LayoutInflater.from(context);
    initializeAndLayoutChildren();
}

private void initializeAndLayoutChildren() {

    stanInflater.inflate(R.layout.cust_rlyt_results_room_info, this, true);

    singleRoomsNumberTV = (TextView) findViewById(R.id.stan_Single_Rooms_Number_TV);
    singleRoomsDescTV = (TextView) findViewById(R.id.stan_Single_Rooms_Description_TV);
    vipRoomsNumberTV = (TextView) findViewById(R.id.stan_VIP_Rooms_Number_TV);
    vipRoomsDescTV = (TextView) findViewById(R.id.stan_VIP_Rooms_Description_TV);
}

public static String getSingleRoomsNumberTextValue() {
    return singleRoomsNumberTV.getText().toString();
}

public static void setSingleRoomsNumberTextValue(String singleRoomsNumberText) {
    singleRoomsNumberTV.setText(singleRoomsNumberText);
} 

public static void setVipRoomsNumberTextValue (String vipRoomsNumberText) {
    vipRoomsNumberTV.setText(vipRoomsNumberText);
}

public static String getVipRoomsDescTextValue () {
    return vipRoomsDescTV.getText().toString();
}

public static void setVipRoomsDescTextValue (String vipRoomsDescText) {
    vipRoomsDescTV.setText(vipRoomsDescText);
}  }

...as you can see I love using static getters and setters as it gives me easy access to the required views with a single line of code. The really odd thing about this error I am getting at runtime is that I have two other classes like this one that use exactly the same setup - and those classes work fine (update the data as required). However, when this class's setters are called with the following calls they all fail to execute:

ResultsRoomInfoCustRLYT.setSingleRoomsNumberTextValue(singleRoomsNumberInfo);
    ResultsRoomInfoCustRLYT.setSingleRoomsDescTextValue(singleRoomsDescInfo);
    ResultsRoomInfoCustRLYT.setVipRoomsNumberTextValue(vipRoomsNumberInfo);
    ResultsRoomInfoCustRLYT.setVipRoomsDescTextValue(vipRoomsDescInfo);

Any help will be much appreciated. Thanks. Shore-T.

Well the problem is that you are trying to set the texts of some null text views. For example, the following line:

ResultsRoomInfoCustRLYT.setSingleRoomsNumberTextValue(singleRoomsNumberInfo);

tries to do the following:

singleRoomsNumberTV.setText(singleRoomsNumberText);

But what is singleRoomsNumberTV? Where is it assigned? You need to call:

new ResultsRoomInfoCustRLYT(this);

from your activity to initialize the static TextViews.

Your static getters and setters access static references that get initialized by a non-static method - initializeAndLayoutChildren . When you initialize a static variable in a non-static method, you run the risk of accessing the static variable before it is initialized. You also run the risk of initializing the static variables multiple times - each time a new instance is created.

For example, the static method setSingleRoomsNumberTextValue accesses the static variable singleRoomsNumberTV which gets initialized by the instance method initializeAndLayoutChildren called by the constructor of ResultsRoomInfoCustRLYT . If you call that static method before creating any instance of ResultsRoomInfoCustRLYT , you'll get NullPointerException .

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