[Android] Correct order to set Context in Dao

Caution

--You should fill the Context in the constructor. --If you write in a local sentence, Context will be set in Helper as it is empty.

OK

HogeAppDao.java


public class HogeAppDao {
    private Context mContext;
    private HugaHelper hugaHelper = new HugaHelper(mContext);

    public HogeAppDao(Context context) {
        mContext = context;
        hugaHelper = new HugaHelper(mContext);
    }

    //Method to get information of all users
    public List<UserInfoEntity> selectAll() {
       ...
    };
}

NG

HogeAppDao.java


public class HogeAppDao {
    private Context mContext;
    private HugaHelper hugaHelper = new HugaHelper(mContext);

    public HogeAppDao(Context context) {
        mContext = context;
    }

    hugaHelper = new HugaHelper(mContext);

    //Method to get information of all users
    public List<UserInfoEntity> selectAll() {
       ...
    };
}

Recommended Posts

[Android] Correct order to set Context in Dao
How to set Lombok in Eclipse
[Android] Precautions for where to call context
Steps to set a favicon in Rails
Java to C and C to Java in Android Studio
How to use ExpandableListView in Android Studio
Bitmap too large to be uploaded into a texture trying to set a large Bitmap in Android ImageView
[Android Studio] I want to set restrictions on the values registered in EditText [Java]
How to sort in ascending / descending order with SQLite
How to set the display time to Japan time in Rails
[Android / Java] Set up a button to return to Fragment
[Rails 6] How to set a background image in Rails [CSS]
How to set up Android OR mapper "Orma" (Kotlin)