简体   繁体   中英

Why am I getting a null pointer error in my Android Code?

I am getting a null point error in my code and the log is pointing here

199     public void getPurchasedSubs () throws RemoteException {
200         Bundle ownedItems = mService.getPurchases(3, getPackageName(), "subs", null);

Here is the logcat file dump

03-08 13:24:45.619: E/AndroidRuntime(7149): FATAL EXCEPTION: main
03-08 13:24:45.619: E/AndroidRuntime(7149): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.imobilize.ETV_V3_INAPP_SUB/com.imobilize.ETV_V3_INAPP_SUB.IntroBLevActivity}: java.lang.NullPointerException
03-08 13:24:45.619: E/AndroidRuntime(7149):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
03-08 13:24:45.619: E/AndroidRuntime(7149):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
03-08 13:24:45.619: E/AndroidRuntime(7149):     at android.app.ActivityThread.access$600(ActivityThread.java:128)
03-08 13:24:45.619: E/AndroidRuntime(7149):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
03-08 13:24:45.619: E/AndroidRuntime(7149):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-08 13:24:45.619: E/AndroidRuntime(7149):     at android.os.Looper.loop(Looper.java:137)
03-08 13:24:45.619: E/AndroidRuntime(7149):     at android.app.ActivityThread.main(ActivityThread.java:4517)
03-08 13:24:45.619: E/AndroidRuntime(7149):     at java.lang.reflect.Method.invokeNative(Native Method)
03-08 13:24:45.619: E/AndroidRuntime(7149):     at java.lang.reflect.Method.invoke(Method.java:511)
03-08 13:24:45.619: E/AndroidRuntime(7149):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
03-08 13:24:45.619: E/AndroidRuntime(7149):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
03-08 13:24:45.619: E/AndroidRuntime(7149):     at dalvik.system.NativeStart.main(Native Method)
03-08 13:24:45.619: E/AndroidRuntime(7149): Caused by: java.lang.NullPointerException
03-08 13:24:45.619: E/AndroidRuntime(7149):     at com.imobilize.ETV_V3_INAPP_SUB.IntroBLevActivity.getPurchasedSubs(IntroBLevActivity.java:200)
03-08 13:24:45.619: E/AndroidRuntime(7149):     at com.imobilize.ETV_V3_INAPP_SUB.IntroBLevActivity.checkValidation(IntroBLevActivity.java:238)
03-08 13:24:45.619: E/AndroidRuntime(7149):     at com.imobilize.ETV_V3_INAPP_SUB.IntroBLevActivity.onCreate(IntroBLevActivity.java:96)
03-08 13:24:45.619: E/AndroidRuntime(7149):     at android.app.Activity.performCreate(Activity.java:4470)
03-08 13:24:45.619: E/AndroidRuntime(7149):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)

SPECS for this method

    /**
 * Returns the current SKUs owned by the user of the type and package name specified along with
 * purchase information and a signature of the data to be validated.
 * This will return all SKUs that have been purchased in V3 and managed items purchased using
 * V1 and V2 that have not been consumed.
 * @param apiVersion billing API version that the app is using
 * @param packageName package name of the calling app
 * @param type the type of the in-app items being requested
 *        ("inapp" for one-time purchases and "subs" for subscription).
 * @param continuationToken to be set as null for the first call, if the number of owned
 *        skus are too many, a continuationToken is returned in the response bundle.
 *        This method can be called again with the continuation token to get the next set of
 *        owned skus.
 * @return Bundle containing the following key-value pairs
 *         "RESPONSE_CODE" with int value, RESULT_OK(0) if success, other response codes on
 *              failure as listed above.
 *         "INAPP_PURCHASE_ITEM_LIST" - StringArrayList containing the list of SKUs
 *         "INAPP_PURCHASE_DATA_LIST" - StringArrayList containing the purchase information
 *         "INAPP_DATA_SIGNATURE_LIST"- StringArrayList containing the signatures
 *                                      of the purchase information
 *         "INAPP_CONTINUATION_TOKEN" - String containing a continuation token for the
 *                                      next set of in-app purchases. Only set if the
 *                                      user has more owned skus than the current list.
 */
Bundle getPurchases(int apiVersion, String packageName, String type, String continuationToken);

When I check for the package name it comes back valid

Here is the code where mService is created IInAppBillingService mService;

ServiceConnection mServiceConn = new ServiceConnection() {
   //@Overide
       public void onServiceDisconnected(ComponentName name) {
       mService = null;
   }
       //@Overide
   public void onServiceConnected(ComponentName name, 
      IBinder service) {
       mService = IInAppBillingService.Stub.asInterface(service);
   }

};  

I am not that good at interpreting the logcat file, so the answer may be right in front of me.

This piece of code I thought was supposed to open it up so I coud use mService anytime.

        bindService(new 
            Intent("com.android.vending.billing.InAppBillingService.BIND"),
                    mServiceConn, Context.BIND_AUTO_CREATE);

I think you use mService before connect to service.

My suggestion is to use boolean variable to check if activity connect to service or not, or use getPurchasedSubs () in onServiceConnected like this:

ServiceConnection mServiceConn = new ServiceConnection() {
   public void onServiceDisconnected(ComponentName name) {
       mService = null;
   }

   public void onServiceConnected(ComponentName name, 
      IBinder service) {
       mService = IInAppBillingService.Stub.asInterface(service);
getPurchasedSubs ();
   }

};  

hope this helped you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM