I can't get api url from there to fragmentcategory?

Viewed 23

I can't get from SharedPref to fragmentcategory.Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.kannada.newspaper.india.utils.SharedPref.getApiUrl()' on a null object reference I am getting this error i am new android devloper I have tried many times enter image description here Fragmentcategory

  @Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view,savedInstanceState);

    this.callbackCall = RestAdapter.createAPI(sharedPref.getApiUrl()).getHome(AppConfig.REST_API_KEY);
    this.callbackCall.enqueue(new Callback<CallbackHome>() {
        public void onResponse(Call<CallbackHome> call, Response<CallbackHome> response) {
            CallbackHome responseHome = response.body();
            if (responseHome == null || !responseHome.status.equals("ok")) {

                return;
            }
            displayData(responseHome);


        }

        private void displayData(CallbackHome responseHome) {

            displayCategory(responseHome.category);


        }


        public void onFailure(Call<CallbackHome> call, Throwable th) {
            Log.e("onFailure", th.getMessage());
            if (!call.isCanceled()) {

            }
        }
    });



}

this Sharedpref

 public class SharedPref {

    private Context context;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    private static final String IS_FIRST_TIME_LAUNCH = "IsFirstTimeLaunch";

    public SharedPref(Context context) {
        this.context = context;
        sharedPreferences = context.getSharedPreferences("setting", Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
    }

    public Boolean getIsDarkTheme() {
        return sharedPreferences.getBoolean("theme", false);
    }

    public void setIsDarkTheme(Boolean isDarkTheme) {
        editor.putBoolean("theme", isDarkTheme);
        editor.apply();
    }

    public void setFirstTimeLaunch(boolean isFirstTime) {
        editor.putBoolean(IS_FIRST_TIME_LAUNCH, isFirstTime);
        editor.commit();
    }

    public boolean isFirstTimeLaunch() {
        return sharedPreferences.getBoolean(IS_FIRST_TIME_LAUNCH, true);
    }

    public void setDefaultFilterRecipes(int i) {
        editor.putInt("filter", i);
        editor.apply();
    }

    public Integer getCurrentFilterRecipes() {
        return sharedPreferences.getInt("filter", 0);
    }

    public void updateFilterRecipes(int position) {
        editor.putInt("filter", position);
        editor.apply();
    }

    public Integer getRecipesViewType() {
        return sharedPreferences.getInt("recipes_list", RECIPES_GRID_2_COLUMN);
    }

    public void updateRecipesViewType(int position) {
        editor.putInt("recipes_list", position);
        editor.apply();
    }

    public void setYoutubeApiKey() {
        editor.putInt("youtube_api_key", 0);
        editor.apply();
    }

    public void saveConfig(String api_url, String application_id) {
        editor.putString("api_url", api_url);
        editor.putString("application_id", application_id);
        editor.apply();
    }

    public String getApiUrl() {
        return sharedPreferences.getString("api_url", "https://kalviaruvi.com/demoandroid/your_recipes_app/");
    }

    public String getApplicationId() {
        return sharedPreferences.getString("application_id", "com.app.yourrecipesapp");
    }

    public void saveCredentials(String youtube_api_key, String fcm_notification_topic, String onesignal_app_id, String more_apps_url) {
        editor.putString("youtube_api_key", youtube_api_key);
        editor.putString("fcm_notification_topic", fcm_notification_topic);
        editor.putString("onesignal_app_id", onesignal_app_id);
        editor.putString("more_apps_url", more_apps_url);
        editor.apply();
    }

    public String getYoutubeAPIKey() {
        return sharedPreferences.getString("youtube_api_key", "0");
    }

    public String getFcmNotificationTopic() {
        return sharedPreferences.getString("fcm_notification_topic", "your_recipes_app_topic");
    }

    public String getOneSignalAppId() {
        return sharedPreferences.getString("onesignal_app_id", "0");
    }

    public String getMoreAppsUrl() {
        return sharedPreferences.getString("more_apps_url", "https://play.google.com/store/apps/developer?id=Solodroid");
    }

}
0 Answers
Related