title: How to initialize mService for In-App Billing
body: <p>I am able to successfully purchase in-app billing items in my app, but I have not yet been able to successfully check which items the user has purchased, as I am getting a a null pointer exception on this line:</p>
  
  <pre><code>  ownedItems = mService.getPurchases(3, getPackageName(), "inapp", null);
  </code></pre>
  
  <p>I did see the other posts on this topic, but those solutions either did not work in my case, or I don't fully understand the solution:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/20930674/getpurchases-nullpointerexception-initializing-mservice">getPurchases() NullPointerException initializing mService</a><br></li>
  <li><a href="https://stackoverflow.com/questions/28261243/in-app-null-pointer-exception">In App Null Pointer Exception</a><br></li>
  <li><a href="https://stackoverflow.com/questions/10635512/android-in-app-billing-null-pointer-exception">Android In-App billing: Null Pointer Exception</a></li>
  </ul>
  
  <p>Here is the entire method:</p>
  
  <pre><code>private void checkOwnedItems() throws RemoteException {
  
      Bundle ownedItems;
      String sku = "";
  
      ownedItems = mService.getPurchases(3, getPackageName(), "inapp", null);
  
      int response = ownedItems.getInt("RESPONSE_CODE");
      if (response == 0) {
          ArrayList&lt;String&gt; ownedSkus = ownedItems.getStringArrayList("INAPP_PURCHASE_ITEM_LIST");
          ArrayList&lt;String&gt; purchaseDataList = ownedItems.getStringArrayList("INAPP_PURCHASE_DATA_LIST");
  
          if (purchaseDataList.size() &gt; 0) {
              //user owns 1 or more items
  
              for (int i = 0; i &lt; purchaseDataList.size(); ++i) {
                  sku = ownedSkus.get(i);
              }
              Toast.makeText(SettingsActivity.this, "You own these features: " + sku, Toast.LENGTH_LONG).show();
          } else {
              //user owns zero items, launch purchase flow
              MACAddress = UniqueID.getMACAddress("wlan0");
              int requestCode = 22222;
              mHelper.launchPurchaseFlow(SettingsActivity.this, productID, requestCode, mPurchaseFinishedListener, MACAddress);
  
          }
      }
  }
  </code></pre>
  
  <p>I ran the debugger, and it appears that mService is null.  Where and how am I supposed to initialize mService?</p>
  
  <p>Currently I am trying to initialize mService in the onServiceConnected method, but perhaps onServiceConnected is never getting called. When and how should I be calling the onServiceConnected method?</p>
  
  <pre><code>    ServiceConnection mServiceConn = new ServiceConnection()
  
  {
      @Override
      public void onServiceConnected(ComponentName name, IBinder service) {
          mService = IInAppBillingService.Stub.asInterface(service);
      }
  
      @Override
      public void onServiceDisconnected(ComponentName name) {
          mService = null;
      }
  };
  </code></pre>