0

I am new to android app developement.I did found questions related to my issue but none provide the correct solution. Here is my code where i am trying get string. I am getting error like this

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getString(int)' on a null object reference

public class CommonUtils {
    private static Context _context;

    public CommonUtils(Context context) {
        this._context = context;
    }
    public static String convertTimeToHoursMinutesString(long lTimeinMillis){
        // Calculate hours first.
        long hours = TimeUnit.MILLISECONDS.toHours(lTimeinMillis);
        lTimeinMillis -= TimeUnit.HOURS.toMillis(hours);
        // Calculate minutes then.
        long minutes = TimeUnit.MILLISECONDS.toMinutes(lTimeinMillis);
        lTimeinMillis -= TimeUnit.MINUTES.toMillis(minutes);
        // Calculate seconds last.
        //long seconds = TimeUnit.MILLISECONDS.toSeconds(lTimeinMillis);

        StringBuilder str = new StringBuilder(64);
        str.append(hours);

        str.append(_context.getString(R.string.Hour));
        str.append(minutes);
        str.append(_context.getString(R.string.Min));
        //str.append(seconds);
        //str.append(" Seconds");
        return(str.toString());
        }
    }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

1

The issue which you are getting is NullPointerException on _context.

I think it is because you have declared _context as static variable and convertTimeToHoursMinutesString as static method.

But you are initializing the _context in constructor.

public CommonUtils(Context context) {
    this._context = context;
}

But as the method is static so you would not be creating an object to call it. Instead you would be calling it directly by class name, like CommonUtils.convertTimeToHoursMinutesString. So the constructor never gets called, hence _context is null.

If you want to keep it static then you can do it like this,

public static String convertTimeToHoursMinutesString(Context _context, long lTimeinMillis){
    // Calculate hours first.
    long hours = TimeUnit.MILLISECONDS.toHours(lTimeinMillis);
    lTimeinMillis -= TimeUnit.HOURS.toMillis(hours);
    // Calculate minutes then.
    long minutes = TimeUnit.MILLISECONDS.toMinutes(lTimeinMillis);
    lTimeinMillis -= TimeUnit.MINUTES.toMillis(minutes);
    // Calculate seconds last.
    //long seconds = TimeUnit.MILLISECONDS.toSeconds(lTimeinMillis);

    StringBuilder str = new StringBuilder(64);
    str.append(hours);

    str.append(_context.getString(R.string.Hour));
    str.append(minutes);
    str.append(_context.getString(R.string.Min));
    //str.append(seconds);
    //str.append(" Seconds");
    return(str.toString());
}

Also you can access getString() like context.getResources().getString(R.string.YOUR_STRING);

gprathour
  • 14,813
  • 5
  • 66
  • 90
  • Is there any way to initialize string array by using (R.string.thatvalue) ??? – Jasmine Antony Nov 25 '19 at 12:02
  • @JasmineAntony Check this, it might help you. https://stackoverflow.com/q/4161256/1055241 – gprathour Nov 25 '19 at 12:08
  • I want to initialize in my enum class eg..public enum WorkStateEnums { WORK_EMPTY_CAR(000, "WORK_EMPTY_CAR", R.string.xyz);} – Jasmine Antony Nov 25 '19 at 12:22
  • @JasmineAntony Try it out first, if you face any issues first try to understand it and fix it yourself, if it doesn't work then you can post a new question here. – gprathour Nov 25 '19 at 12:28
  • Hi i am getting NullPointerException (Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference) – Jasmine Antony Dec 05 '19 at 06:21
  • @JasmineAntony Hello, I think that we solved that day, when you posed this question. – gprathour Dec 05 '19 at 07:52
0

in your code

str.append(_context.getString(R.string.Min));

please replace it with

str.append(_context.getResources().getString(R.string.Min));
unownsp
  • 727
  • 5
  • 19