0

I am having a Service class which is a server receiving data from a java client and i am able to retrieve data but when i tried to broadcast the data from the service to the MainActivity, sendBroadcast(intent) gives a NPE in Service and it doesn't broadcast the message..I viewed several answers but i cant figure out the issue..so i posted this quetsion.. below is my service part

public class Service_ extends Service implements Runnable {
BroadcastReceiver broadcaster;
Intent intent;
static final public String BROADCAST_ACTION = "com.mes.broadcast";
private static ServerSocket server;
private static int port = 9800;
ObjectOutputStream oos = null;
ObjectInputStream ois = null;

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
    Log.d("sri", "onCreate---->Service");

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    Log.d("sri","onStartCommand(Intent intent, int flags, int startId)---->Service");
    Service_ s = new Service_();
    Thread t = new Thread(s);
    Log.d("sri","going to start thread---->Service");
    t.start();
    return super.onStartCommand(intent, flags, startId);

    }
   @Override
   public void run() {
   Log.d("sri","run()---->Service");
    try {

    }catch(Exception e){
      // code to connect to java client .......it works fine and  also   obtained data ..I WANTED THIS DATA TO BE SENT TO THE ACTIVITY
            intent = new Intent(BROADCAST_ACTION);Log.d("sri","sendResult----->putExtra");
            intent.putExtra("message", message);
            Log.d("sri", "sendResult---->SendBroadcast");
            sendBroadcast(intent);//THIS LINE GIVES ME NPE LINE:109

      }}

My Stacktrace is

     E/AndroidRuntime: FATAL EXCEPTION: Thread-11724
     java.lang.NullPointerException
    atandroid.content.ContextWrapper.sendBroadcast(ContextWrapper.java:338)
   at airhost.server_client_service_.Service_.run(Service_.java:109)
   at java.lang.Thread.run(Thread.java:838)

My MainActivity

      //imports....

     public class MainActivity extends AppCompatActivity {
     TextView result;
     Button button;
     BroadcastReceiver receiver;
     Intent serviceIntent;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    result = (TextView) findViewById(R.id.textView);
    button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            serviceIntent = new Intent(getApplicationContext(),
                    Service_.class);
            Log.d("sri", "onResume-->startService-----> Activity");
            startService(serviceIntent);
        }
    });

    receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d("sri", "BroadcastReceiver-->onReceive-----> Activity");
            String message = intent.getStringExtra("message");
            result.setText(message);
        }
    };

}

protected void onResume() {
    super.onResume();
    Log.d("sri", "onResume-->registerReceiver-----> Activity");
    registerReceiver(receiver, new IntentFilter(
            Service_.BROADCAST_ACTION));
}
@Override
protected void onPause() {
    super.onPause();
    Log.d("sri", "onPause-------> Activity");
    stopService(serviceIntent);
    unregisterReceiver(receiver);
}
}
adi
  • 984
  • 15
  • 33
  • Why is your onStartCommand creating - and throwing away - yet another instance of the service? – JHH Mar 14 '16 at 12:20
  • what shall i return there??, is that the issue ??? – adi Mar 14 '16 at 13:15
  • Yes it's most likely the issue. You should never create instances yourself of Service, Activity etc, the system does that for you. Furthermore, onStartCommand is an instance method of Service, meaning it's called on a Service object, namely your (inappropriately named!!!) Service_. What possible reason could there be to create yet another instance of your service from there? It just doesn't make sense. The reason for your crash is that since your Service object called "s" is not properly created, calling methods such as sendBroadcast on it will fail unpredictably. – JHH Mar 14 '16 at 13:41
  • Replace `Service_ s = new Service_(); Thread t = new Thread(s);` with `Thread t = new Thread(this);` – JHH Mar 14 '16 at 13:45
  • A MILLION THANKS BRO!!!!!!!!!!...works like a charm........in documentation it says for networking operation and to return something to activity we use bindService but here i used startService...but it works(i do perform networking and also return data)..how it works ?? – adi Mar 14 '16 at 14:34

0 Answers0