title: android remote service null pointer
body: <p>I'm not exactly a beginner in Java/Android anymore but this problem has me stuck.  I have a main project that calls a remote service.  That remote service (ROS) is installed on the phone before running the main project.  However, I keep getting a null pointer exception in my serviceManager when I call rosService.publishCommand (the line commented 'Release Emergency Brake').  I don't think this is a race condition because the Log files show a successful connection before publishCommand is called.  Also, you can see that the logs show that onBind() was entered in the remote Service.  Please let me know if you have any ideas!
  Thanks!</p>
  
  <pre><code>package org.ros.android.jaguar;
  
  import org.ros.android.jaguar.ROSService;
  import org.ros.android.jaguar.ROSServiceReporter;
  
  import com.darpa.uvade.Settings;
  
  import android.app.Activity;
  import android.app.Service;
  import android.content.ComponentName;
  import android.content.Context;
  import android.content.Intent;
  import android.content.ServiceConnection;
  import android.os.IBinder;
  import android.os.RemoteException;
  import android.os.SystemClock;
  import android.util.Log;
  
  
  public class ROSServiceManager {
  
      private boolean disconnected = false;
      private ROSService rosService;
      private static Intent intent = new Intent("com.darpa.ros");
      private OnConnectedListener onConnectedListener;
      private Service service;
      private Activity activity;
      private String tag = "ROS";
  
      public static interface OnConnectedListener {
          void onConnected();
          void onDisconnected();
      }
  
      public ROSServiceManager(Service service, OnConnectedListener onConnectedListener) {
          this.service = service;
          this.onConnectedListener = onConnectedListener;
          service.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
      }
      /*public ROSServiceManager(Activity activity, OnConnectedListener onConnectedListener) {
          this.activity = activity;
          this.onConnectedListener = onConnectedListener;
          activity.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
      }*/
  
  
      public void publishCommand(float velocity, float angle){
  
          String initString = "MMW !M ";
  
          if(velocity&gt;1){
              Log.e("UVADE","Error! Velocity too large");
              velocity=1;
          }
  
          //Log.d(tag, "velocity: "+velocity+", angle: "+angle+", maxPower: "+Settings.getJaguarMaxPower());
  
          int jaguarX = (int) ((int) Settings.getJaguarMaxPower() * Math.cos(angle - 45*Math.PI/180) * velocity);
          int jaguarY = (int) ((int) Settings.getJaguarMaxPower() * Math.sin(angle - 45*Math.PI/180) * velocity);
  
  
          String commandString = initString.concat(String.valueOf(jaguarX)+" "+jaguarY);
          Log.d(tag,commandString);
  
          SystemClock.sleep(1000);
  
          Log.d(tag, "rosService right before null Pointer: "+rosService);
  
          try {
              rosService.publishCommand(initString+"G");  //release emergency brake
              rosService.publishCommand(commandString);
          } catch (RemoteException e) {
              e.printStackTrace();
          } catch (NullPointerException e1){
              e1.printStackTrace();
          }
      }
  
      public synchronized void disconnect() {
          disconnected = true;
          if(service!=null)
              service.unbindService(serviceConnection);
          else
              activity.unbindService(serviceConnection);
      }
      public synchronized void add(ROSServiceReporter reporter) {
          if (disconnected)
              throw new IllegalStateException("Manager has been explicitly disconnected; you cannot call methods on it");
          try {
              rosService.add(reporter);
          } catch (RemoteException e) {
              Log.e(tag, "add reporter", e);
          }
      }
      public synchronized void remove(ROSServiceReporter reporter) {
          if (disconnected)
              throw new IllegalStateException("Manager has been explicitly disconnected; you cannot call methods on it");
          try {
              rosService.remove(reporter);
          } catch (RemoteException e) {
              Log.e(tag, "remove reporter", e);
          }
      }
  
      private ServiceConnection serviceConnection = new ServiceConnection() {
          @Override public void onServiceDisconnected(ComponentName name) {
              Log.d(tag, "RosService onServiceDisconnected");
              if (onConnectedListener != null)
                  onConnectedListener.onDisconnected();
              rosService = null;
          }
          @Override public void onServiceConnected(ComponentName name, IBinder service) {
              Log.d(tag,"RosService onServiceConnected");
              rosService = ROSService.Stub.asInterface(service);
              Log.d(tag, "rosService: "+rosService);
  
              if (onConnectedListener != null)
                  onConnectedListener.onConnected();
          }
      };
  
  
  }
  </code></pre>
  
  <p>When running, here is the Log output:</p>
  
  <pre><code>08-06 13:48:43.721: D/ROS(7540): ROS onCreate
  
  08-06 13:48:43.731: D/ROS(7540): ROS Service onBind
  
  08-06 13:48:43.731: D/ROS(7540): Returning Binder
  
  08-06 13:48:43.731: D/ROS(7483): RosService onServiceConnected
  
  08-06 13:48:43.731: D/ROS(7540): RosService onServiceConnected
  
  08-06 13:48:43.731: D/ROS(7540): Jaguar connected - adding reporter
  
  08-06 13:48:43.741: D/ROS(7483): rosService: org.ros.android.jaguar.ROSService$Stub$Proxy@431529e0
  
  08-06 13:48:46.613: D/ROS(7483): MMW !M 198 -671
  
  08-06 13:48:47.614: D/ROS(7483): rosService right before null Pointer: null
  
  08-06 13:48:47.715: D/ROS(7483): MMW !M 198 -671
  
  08-06 13:48:48.715: D/ROS(7483): rosService right before null Pointer: null
  
  08-06 13:48:48.766: D/ROS(7483): MMW !M 198 -671
  </code></pre>