0

I try add method in another class which need help me processing errors. And when I need get toast, I get error. How can I decide this situation? Please, help me and explain me how use contexts. Thank you.

This my error:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference

And this my code:

public class DataHelper extends BaseAppCompatActivity {
protected Intent intent;

/**
 * Выводит сумму услуги
 * @param price цена
 * @param discount скидка
 * @return
 */
public static Double Sum (double price, double discount){
    Double sum = null;
    if (price == 0 && discount == 0){
         return null;
    } else if (price == discount){
         sum = price;
    } else if (price > discount || price < discount){
         sum = price - discount;
    }
    return sum;
}

/**
 * Обработка ошибок
 * @param errorCode код ошибки
 * @param errorDescription описание ошибки
 */
public void processingError(String errorCode, String errorDescription){
    switch (errorCode){
        case "inside":
            Toast.makeText(getApplicationContext(), errorDescription, Toast.LENGTH_SHORT).show();
            break;
        case "method_not_found":
            Toast.makeText(getApplicationContext(), errorDescription, Toast.LENGTH_SHORT).show();
            break;
        case "method_access_denied":
            intent = new Intent(this, AuthorizationActivity.class);
            startActivity(intent);
            finish();
            Toast.makeText(getApplicationContext(), errorDescription, Toast.LENGTH_SHORT).show();
            break;
        case "bad_authorization":
            intent = new Intent(this, AuthorizationActivity.class);
            startActivity(intent);
            finish();
            Toast.makeText(getApplicationContext(), errorDescription, Toast.LENGTH_SHORT).show();
            break;
        case "bad_client_secret_or_id":
            intent = new Intent(this, AuthorizationActivity.class);
            startActivity(intent);
            finish();
            Toast.makeText(getApplicationContext(), errorDescription, Toast.LENGTH_SHORT).show();
            break;
        case "app_access_denied":
            intent = new Intent(this, AuthorizationActivity.class);
            startActivity(intent);
            finish();
            Toast.makeText(getApplicationContext(), errorDescription, Toast.LENGTH_SHORT).show();
            break;
        case "wrong_params":
            Toast.makeText(getApplicationContext(), errorDescription, Toast.LENGTH_SHORT).show();
            break;
        case "wrong_param_value":
            Toast.makeText(getApplicationContext(), errorDescription, Toast.LENGTH_SHORT).show();
            break;
        case "object_not_found":
            Toast.makeText(getApplicationContext(), errorDescription, Toast.LENGTH_SHORT).show();
            break;
    }
}

}

  • What is `BaseAppCompatActivity`?. https://developer.android.com/reference/android/content/Context.html Look at the hierarchy at the top and `getApplicationContext` method – Raghunandan Oct 27 '16 at 15:38
  • Do not create arbitrary subclasses of `Activity`. If you are creating an instance of `DataHelper` directly and expecting it to work, you will be disappointed. If you have a method that needs a `Context`, either implement that method on an existing `Activity`, `Service`, or other `Context`, or have the method take a`Context` as a parameter. – CommonsWare Oct 27 '16 at 15:43
  • @Raghunandan I just set standart context which available in this class. My problem: I don't understend value "context" – Денис Климков Oct 27 '16 at 15:44
  • @ДенисКлимков i thought your `BaseAppCompatActivity` does not have access to context. Read commonsware comment if your `BaseAppCompatActivity` extends from `Activity` and you do `DataHelper dh = new DataHelper()` – Raghunandan Oct 27 '16 at 15:46
  • @CommonsWare take context how parameter - it will be a good solution? – Денис Климков Oct 27 '16 at 15:49
  • @Raghunandan oh, I'm sorry, I looked wrong question. BaseAppCompatActivity this is my extended class with using standart AppCompatActivity – Денис Климков Oct 27 '16 at 15:54
  • @ДенисКлимков and do you have `DataHelper dh = new DataHelper()` somewhere in activity. If so remove `extends BaseAppCompatActivity`. And then `public void processingError(String errorCode, String errorDescription,Context context)`. – Raghunandan Oct 27 '16 at 15:56
  • @jordi-castilla it is not a duplicate... – alex Oct 27 '16 at 16:02
  • @dit not agree, sorry. – Jordi Castilla Oct 27 '16 at 16:07
  • @JordiCastilla `This question was marked as an exact duplicate of an existing question.`. If you think, you are the boss. – alex Oct 27 '16 at 16:17
  • @dit I'm not any boss Marc Marquez is. But yes I think so, a `NPE` is a `NPE`, understanding why happen will solve any. – Jordi Castilla Oct 27 '16 at 16:19
  • 2
    @JordiCastilla :D. Ok but I'm think he knows what NPE is, his question is about why is context null. I would also like to know how it can happen. Good one with Marquez ;) – alex Oct 27 '16 at 16:22

0 Answers0