Q:

Not getting screen resolution in custom new class

I am new to android development and after hours of efforts, i decided to put question here. I am trying to fetch screen resolution with the help of new class. If i implement code in MyActivity.java then it is working fine but if i try to implement code in new class then it is giving me null pointer exception.
Following is new class that i am trying to access in MyActivity class but it is not working and giving null pointer exception.
public class DeviceInfoClass {
    public String getScreenResolutionWidth(){           

        DisplayMetrics displayMetrics = new DisplayMetrics();
        WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);        
        wm.getDefaultDisplay().getMetrics(displayMetrics);
        int screenWidth = displayMetrics.widthPixels;

        return String.valueOf(screenWidth);
    }
}

A:

you should pass the context of the activity to the class
public class DeviceInfoClass {
public String getScreenResolutionWidth(Context c){           

    DisplayMetrics displayMetrics = new DisplayMetrics();
    WindowManager wm = (WindowManager)c.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);        
    wm.getDefaultDisplay().getMetrics(displayMetrics);
    int screenWidth = displayMetrics.widthPixels;

    return String.valueOf(screenWidth);
}
}

Edit: 
You can call it like that:
DeviceInfoClass def = new DeviceInfoClass();
String w = def.getScreenResolutionWidth(this);
Toast.makeText(this, w , Toast.LENGTH_LONG).show();

Another approach :
is to make the function static like this:
public class DeviceInfoClass {
public static String getScreenResolutionWidth(Context c){

    DisplayMetrics displayMetrics = new DisplayMetrics();
    WindowManager wm = (WindowManager)c.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
    wm.getDefaultDisplay().getMetrics(displayMetrics);
    int screenWidth = displayMetrics.widthPixels;

    return String.valueOf(screenWidth);
}
}

and call it like that in the main activity:
String w = DeviceInfoClass.getScreenResolutionWidth(this);

