title: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Intent android.content.Intent.putExtra
body: <p>What is the wrong that i have done in here. I couldn't figure it out. I have a input method service and my keyboard has images when i click on a image i am trying share the image to other application.</p>
  
  <h1>Service Code:</h1>
  
  <p>under onCreateInputView Method</p>
  
  <pre><code>mActivityManager =(ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE);
  
  
  
          if(Build.VERSION.SDK_INT &gt; 20){
              mPackageName = this.mActivityManager.getRunningAppProcesses().get(0).processName;
           //Toast.makeText(this,mPackageName, Toast.LENGTH_LONG).show();
          }
          else{
              mPackageName = this.mActivityManager.getRunningTasks(1).get(0).baseActivity.getPackageName();
          }
  
  
          shareIntent = new Intent(Intent.ACTION_SEND);
  
          shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
          shareIntent.setType("image/png"); 
          shareIntent.setPackage(mPackageName);
  </code></pre>
  
  <p>Method that is called from onPost of my asynctask</p>
  
  <pre><code>public void sendImage(int position)
      {
  
          File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath().toString()+"/FecapsImage");
          //folder.mkdirs();
  
          if(folder.exists())
          {
            //Save the path as a string value
            String extStorageDirectory = folder.toString();
  
  
  
              String fileName = MainActivity.imageName.get(position)+".png";
  
              File dataFile = new File(extStorageDirectory,fileName);
  
              //Intent shareIntent = new Intent(Intent.ACTION_SEND);
  
  
              shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(dataFile));
              //Intent new_intent = Intent.createChooser(shareIntent, "Share via");
              //new_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
              startActivity(shareIntent);
  
  
  
          }
          else
          {
              Toast.makeText(this, "Not Found", Toast.LENGTH_LONG).show();
  
          }
  
  
      }
  </code></pre>
  
  <h1>Async task:</h1>
  
  <pre><code>@Override
      protected String doInBackground(String... params) {
  
          SaveImage(this.url);
  
  
          return null;
      }
  
         private String SaveImage(String urlstr) {
  
              String filepath = null;
              try
              {   
                URL url = new URL(urlstr);
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setDoOutput(false);
                urlConnection.setRequestMethod("GET");
                //urlConnection.setDoOutput(true);
                urlConnection.setInstanceFollowRedirects(false);
                urlConnection.connect();                  
  
                File SDCardRoot = new File(Environment.getExternalStorageDirectory().getAbsolutePath().toString()+"/FecapsImage");
                SDCardRoot.mkdirs();
                String filename=this.name+".jpg";   
                Log.i("Local filename:",""+filename);
                File file = new File(SDCardRoot,filename);
                if(file.createNewFile())
                {
                  file.createNewFile();
                }                 
                FileOutputStream fileOutput = new FileOutputStream(file);
                InputStream inputStream = urlConnection.getInputStream();
                int totalSize = urlConnection.getContentLength();
                int downloadedSize = 0;   
                byte[] buffer = new byte[1024];
                int bufferLength = 0;
                while ( (bufferLength = inputStream.read(buffer)) &gt; 0 ) 
                {                 
                  fileOutput.write(buffer, 0, bufferLength);                  
                  downloadedSize += bufferLength;                 
                  Log.i("Progress:","downloadedSize:"+downloadedSize+"totalSize:"+ totalSize) ;
                }             
                fileOutput.close();
                if(downloadedSize==totalSize) filepath=file.getPath();    
              } 
              catch (MalformedURLException e) 
              {
                e.printStackTrace();
              } 
              catch (IOException e)
              {
                filepath=null;
                e.printStackTrace();
              }
              Log.i("filepath:"," "+filepath);
  
              return filepath;
          }
  
  
         @Override
      protected void onPreExecute() {
          // TODO Auto-generated method stub
          super.onPreExecute();
          if(ct!=null)
          {
  //      dialog = new ProgressDialog(ct);
  //      dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);  
  //      dialog.setMessage("loading...");
          //dialog.show();
          }
  
  
  
      }
  
      @Override
      protected void onPostExecute(String result) {
          // TODO Auto-generated method stub
          super.onPostExecute(result);
          //dialog.dismiss();
  
          this.ct.sendImage(this.pos);
  
      }   
  </code></pre>
  
  <p>it give's me exception at sendImage Method in</p>
  
  <p>shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(dataFile));</p>
  
  <pre><code>    startActivity(shareIntent);
  </code></pre>
  
  <h1>logcat</h1>
  
  <pre><code>java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Intent android.content.Intent.putExtra(java.lang.String, android.os.Parcelable)' on a null object reference
  at MainActivity.sendImage(MainActivity.java:550)
  at 
  
      ImageDownload.onPostExecute(ImageDownload.java:118)
      ImageDownload.onPostExecute(ImageDownload.java:1)
      at android.os.AsyncTask.finish(AsyncTask.java:632)
      at android.os.AsyncTask.access$600(AsyncTask.java:177)
      at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:135)
      at android.app.ActivityThread.main(ActivityThread.java:5312)
      at java.lang.reflect.Method.invoke(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:372)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
  </code></pre>