Android get helper class methods issue
I have a little issue with getting my helper class's in android.I have 3 classes A, B, C. B extends A and C I'm trying to use methods in C from B,but everytime I try to use them is throwin me a NullPointerException.
Here is a the 开发者_如何学运维code I'm using :
In class A I have this :
public class A {
//declaration from class C
C newC;
}
In class C I have :
public class C {
static Context context;
String version = "";
public static int strVerToIntVer(String strLen){
String newStr = strLen.replace(".", "");
int i=Integer.parseInt(newStr);
return i;
}
public static String getPackageVersion(){
try {
PackageManager pm = context.getPackageManager();
PackageInfo packageInfo = pm.getPackageInfo(context.getPackageName(), 0);
version = packageInfo.versionName;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return version;
}
public static boolean checkClientApiVer(String clientApiVer){
int s = C.strVerToIntVer(clientApiVer);
int c = C.strVerToIntVer(getPackageVersion());
return (c>=s);
}
}
In class B I have :
public class B extends A {
C newC;
serverApiVer = jsonObj.getString("server_api_ver");
Log.w("SERVER API VER","SHOW SERVER API VERSION : "+serverApiVer);
C.checkClientApiVer(serverApiVer);
}
and this line : C.checkClientApiVer(serverApiVer);
is throwin me a NullPointerException, and I still got no idea how to fix that.
LogCat :
09-
15 15:47:37.752: WARN/System.err(8150): java.lang.NullPointerException
09-15 15:47:37.752: WARN/System.err(8150): at com.stampii.stampii.comm.rpc.C.getPackageVersion(RPCCommunicator.java:1035)
09-15 15:47:37.752: WARN/System.err(8150): at com.stampii.stampii.comm.rpc.C.checkClientApiVer(RPCCommunicator.java:1048)
09-15 15:47:37.752: WARN/System.err(8150): at com.stampii.stampii.comm.rpc.B.executeBefore(InfoStartRPCPacket.java:103)
09-15 15:47:37.752: WARN/System.err(8150): at com.stampii.stampii.user.UserLogin$2.onClick(UserLogin.java:215)
09-15 15:47:37.752: WARN/System.err(8150): at android.view.View.performClick(View.java:2408)
09-15 15:47:37.752: WARN/System.err(8150): at android.view.View$PerformClick.run(View.java:8817)
09-15 15:47:37.752: WARN/System.err(8150): at android.os.Handler.handleCallback(Handler.java:587)
09-15 15:47:37.752: WARN/System.err(8150): at android.os.Handler.dispatchMessage(Handler.java:92)
09-15 15:47:37.762: WARN/System.err(8150): at android.os.Looper.loop(Looper.java:144)
09-15 15:47:37.762: WARN/System.err(8150): at android.app.ActivityThread.main(ActivityThread.java:4937)
09-15 15:47:37.762: WARN/System.err(8150): at java.lang.reflect.Method.invokeNative(Native Method)
09-15 15:47:37.762: WARN/System.err(8150): at java.lang.reflect.Method.invoke(Method.java:521)
09-15 15:47:37.762: WARN/System.err(8150): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-15 15:47:37.762: WARN/System.err(8150): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-15 15:47:37.762: WARN/System.err(8150): at dalvik.system.NativeStart.main(Native Method)
09-15 15:47:37.762: WARN/ERROR(8150): Error - java.lang.NullPointerException
Any suggestions?
you context is null.
which you would have known would you have looked at the stack trace.
Also, version is not declared. it should not even compile.
edit
there are, like, 3 variables in the method that throws the NPE. It has to be one of these. So. Either your context is null (not initialized anywhere), or your packagemanager is null (quite unlikely), or your packageinfo is null (unlikely also, as it should throw an exception).
I suggest you test all three, but particularly the context.
(also, you version field is not static and cannot be accessed from getPackageVersion, which is static
精彩评论