0

I am trying to call add method of remote service in my Activity.In Activity remote service method call is causing nullpointerexception:

//This where i am calling service method in my activity
initService();
service.add();  //this call causing null pointer exception

///My .aidl file

interface IAdditionService {

void add();
}

//my service where add method is defined
@Override
 public IBinder onBind(Intent intent) {

 return new IAdditionService.Stub() {
  /**
   * Implementation of the add() method
   */
  public void add() throws RemoteException {
      Log.i(TAG,"INSIDE add method of  IAdditionService.Stub()  before addition  "); 
   }

 };
}

Please help me out with this.Implementing service for the first time.

My Entire Code:- My Activity Code:-

public class AIDLDemo extends Activity {
  private static final String TAG = "AIDLDemo";
  IAdditionService service;
  IBinder boundService;
  AdditionServiceConnection connection;

  /**
   * This class represents the actual service connection. It casts the bound
   * stub implementation of the service to the AIDL interface.
   */
   class AdditionServiceConnection implements ServiceConnection {

  public void onServiceConnected(ComponentName name, IBinder boundService) {
  service = IAdditionService.Stub.asInterface((IBinder) boundService);
  Log.d(AIDLDemo.TAG, "onServiceConnected() connected");
  Toast.makeText(AIDLDemo.this, "Service connected", Toast.LENGTH_LONG)
      .show();
  }

   public void onServiceDisconnected(ComponentName name) {
   service = null;
   Log.d(AIDLDemo.TAG, "onServiceDisconnected() disconnected");
   Toast.makeText(AIDLDemo.this, "Service connected", Toast.LENGTH_LONG)
      .show();
   }
  }

  /** Binds this activity to the service. */
 private void initService() {
 connection = new AdditionServiceConnection();
 Intent i = new Intent();
 i.setClassName("com.marakana", com.marakana.AdditionService.class.getName());
 boolean ret = bindService(i, connection, Context.BIND_AUTO_CREATE);
 Log.d(TAG, "initService() bound with " + ret);
 }

  /** Unbinds this activity from the service. */
 private void releaseService() {
  unbindService(connection);
  connection = null;
  Log.d(TAG, "releaseService() unbound.");
 }

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);

 initService();

  // Setup the UI
  Button buttonCalc = (Button) findViewById(R.id.buttonCalc);

  buttonCalc.setOnClickListener(new OnClickListener() {
  TextView result = (TextView) findViewById(R.id.result);
  EditText value1 = (EditText) findViewById(R.id.value1);
  EditText value2 = (EditText) findViewById(R.id.value2);

  public void onClick(View v) {
    int v1, v2, res = -1;

    service = IAdditionService.Stub.asInterface((IBinder) boundService);
    v1 = Integer.parseInt(value1.getText().toString());
    v2 = Integer.parseInt(value2.getText().toString());

     try {
      service.add();
     } catch (RemoteException e) {
      Log.d(AIDLDemo.TAG, "onClick failed with: " + e);
      e.printStackTrace();
     }
      result.setText(new Integer(res).toString());
     }
   });
  }

 /** Called when the activity is about to be destroyed. */
 @Override
 protected void onDestroy() {
 releaseService();
 }

}

My Service:-

public class AdditionService extends Service {
private static final String TAG = "AdditionService";

@Override
public void onCreate() {
 super.onCreate();
 Log.d(TAG, "onCreate()");
 }

 @Override
 public IBinder onBind(Intent intent) {

 return new IAdditionService.Stub() {
   /**
   * Implementation of the add() method
   */
  public void add() throws RemoteException {
    Log.d(TAG, String.format("AdditionService.add(%d, %d)"));
  //        return value1 + value2;
  }

  };
  }

  @Override
  public void onDestroy() {
  super.onDestroy();
  Log.d(TAG, "onDestroy()");
  }
  }
Rauf
  • 620
  • 1
  • 8
  • 20
  • 4
    have you initialized variable **service**? – nandeesh Aug 24 '12 at 13:06
  • yes have a look :- IAdditionService service = IAdditionService.Stub.asInterface((IBinder) boundService); – Rauf Aug 24 '12 at 13:26
  • I agree with nandeesh from the code you have supplied you have not initialised the variable service. Your method onBind might return an object of a type that service should represent but it certainly is not being initialised in the code shown above. – David Hirst Aug 24 '12 at 14:05
  • thank u nandeesh and david for your observation,now initialized 'service' in onBind() like: service = IAdditionService.Stub.asInterface((IBinder) boundService); then also getting null pointer exception :( – Rauf Aug 24 '12 at 14:20
  • @DavidHirst can u help me further as initialization also didn't solve my problem – Rauf Aug 24 '12 at 14:32
  • Could you post the full code? Especially the method InitService() – David Hirst Aug 24 '12 at 14:33
  • @DavidHirst i have posted entire code in my edits – Rauf Aug 24 '12 at 14:43
  • @nandeesh have a look if you can find any mistake..will be of great boost – Rauf Aug 24 '12 at 14:52
  • are you getting the toast "Service connected"? and have you added AdditionService to manifest? – nandeesh Aug 24 '12 at 14:57
  • @nandeesh yes i am getting "Service Connected" toast and added service in manifest also – Rauf Aug 25 '12 at 05:48

2 Answers2

0

In Onclick function remove below line. Since as soon as OnserviceConnected is called , you are initializing service.

  service = IAdditionService.Stub.asInterface((IBinder) boundService);

You have not initialized boundservice anyewhere yet you are assigning it

nandeesh
  • 24,740
  • 6
  • 69
  • 79
0

I Think it is better to directly using ServicConnection rather than creating a class of it. Please refer this Hope it helpfuls developer.samsung.com/android/technical-docs/Services-with-AIDL-in-Android

Akshay Mukadam
  • 2,388
  • 1
  • 27
  • 40