How to check if the putExtra exist?
<p>I have a diagram as follows</p>

<pre><code>Activity 1 --send data--&gt; Service1
                            ^
                            |
Open app--------------------
                            ^
                            |
Activity 2------------------
</code></pre>

<p>It means the activity 1 will send the data to a Service 1. The service 1 is a running background service with return in <code>onStartCommand()</code> is <code>START_STICKY</code>. Currently, I am using <code>putExtra()</code> function to exchange the data. And in the service I will getExtra data as follows steps:</p>

<p>In <code>Activity 1</code></p>

<pre><code>Intent start_service = new Intent(getContext(), ConnectService.class);
start_service.putExtra("data", "123");
getContext().startService(start_service);
</code></pre>

<p>In onStartCommand() of <code>Service 1</code></p>

<pre><code> String data=null;
 if(intent.hasExtra("data")){
    Bundle bundle = intent.getExtras();
    if(!bundle.getString("data").equals(null)){
        data= bundle.getString("data");
    }
  }
  else
       data="0";
</code></pre>

<p>I used the <code>hasExtra</code> function to check whether <code>data</code> is set or not. In my purpose, the case 1 is that the Service 1 can receive data from the <code>Activity 1</code> . In the case 2, if we do not start from the Activity 1, for example, when I clean all running app, the phone will be open the Service 1 automatically due to <code>START_STICKY</code>, the Service 1 will set data equal "0". Hence, my issue is from second case, the application is crash because it cannot check the Extra exist or not. </p>

<blockquote>
  <p>Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.content.Intent.hasExtra(java.lang.String)' on a null object reference   </p>
</blockquote>

<p>How could I fix the issue? Thank all     </p>