title: NullPointerException in new Intent
body: <p>I'm trying to send a message from the <code>Service</code> and I receive an error</p>
  
  <blockquote>
    <p>Error at com.spynetstation.MediaService.notif(MediaService.java:162)  </p>
  </blockquote>
  
  <p>which points to this line in the <code>Service</code>:</p>
  
  <pre><code>Intent notificationIntent = new Intent(ctx, MediaService.class);
  </code></pre>
  
  <p><strong>Service:</strong></p>
  
  <pre><code>public class MediaService extends Service implements OnPreparedListener,OnCompletionListener{
  
      static MediaPlayer mediaPlayer;
      static NotificationManager nm;
      private static NotificationCompat.Builder mBuilder;
      static Context ctx;
  
      static String dataPlaying = null;
      static String soursetrack = null;
      static TimerTask repeatTask;
      static Timer t;
      Node node;
      static String filePath;
      static File file;
      String titleNotif = "Station";
      String contentNotif = "Running";
  
      public IBinder onBind(Intent paramIntent)  {
          return null;
      }
  
      public static void initMP(String urlStream){
          try {
              mediaPlayer = new MediaPlayer();
              mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
              mediaPlayer.setDataSource(urlStream);
          } catch (IOException e) {
              e.printStackTrace();
          }
          mediaPlayer.prepareAsync();
          Log.d("MediaService", "prepareAsync");
      }
  
      public static void startMP(){
          mediaPlayer.setOnPreparedListener(new OnPreparedListener() {  
              @Override  
              public void onPrepared(MediaPlayer mp) {
                  mp.start();
                  Log.d("MediaService", "start");
                  MainActivity.visible();
                  if (MainActivity.track) {} else {doRepeatTask();}
              }
          });   
      }
  
      public static void stopMP() {
          if (mediaPlayer != null) {
              try {
                  mediaPlayer.stop(); 
                  Log.d("MediaService", "stop");
                  if (MainActivity.track) {} else {stopRepeatTask();}
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      }
  
      public static void releaseMP() {
          if (mediaPlayer != null) {
              try {
                  mediaPlayer.release();
                  Log.d("MediaService", "release"); 
                  mediaPlayer = null;
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      }
  
      @Override
      public void onCompletion(MediaPlayer mp) {
          Log.d("MediaService", "onCompletion"); 
      }
  
      @Override
      public void onPrepared(MediaPlayer mp) {
          Log.d("MediaService", "onPrepareed");
      }
  
      public void onCreate()  {
          super.onCreate();
          // tried the following ctx=getBaseContext(); did not help
          ctx = getApplicationContext();
          this.nm = ((NotificationManager)getSystemService("notification"));
      }
  
      public void onDestroy()  {
          this.nm.cancelAll();
          stopForeground(true);
      }
  
      public int onStartCommand(Intent paramIntent, int paramInt1, int paramInt2)  {
          try {
              TimeUnit.SECONDS.sleep(0);
              notif(titleNotif, contentNotif);
              return super.onStartCommand(paramIntent, paramInt1, paramInt2);
          } catch (InterruptedException localInterruptedException) {
              for (;;) {
                  localInterruptedException.printStackTrace();
              }
          }
      }
  
      public static void notif(String titleNotif, String contentNotif){
          //building the notification
          mBuilder = new NotificationCompat.Builder(ctx)
              .setSmallIcon(R.drawable.spy)
              .setContentTitle(titleNotif)
              .setTicker(contentNotif);
  
          Intent notificationIntent = new Intent(ctx, MediaService.class);
          notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
          PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, 
              notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
          mBuilder.setContentIntent(pendingIntent);
          Notification n = mBuilder.build();
          nm.notify(1, n);
          // startForeground(12345, mBuilder.build());
      }
  
      public static void doRepeatTask(){
          t = new Timer();
          repeatTask = new TimerTask() {
              public void run() {
                  new Task().execute();
              }
          };
          t.schedule(repeatTask, 300, 3000); 
      }
  
      public static void stopRepeatTask(){
          if(repeatTask!=null){
              repeatTask.cancel();
              Log.d("TIMER", "timer canceled");
          }
          MainActivity.titleMusic.setText("");
          MainActivity.titleMusic.setVisibility(View.GONE);
      }
  
      public static class Task extends AsyncTask&lt;Void, Void, Void&gt;{
          @Override
          protected Void doInBackground(Void... arg0) {
              try {
                  URL url = new URL(soursetrack);
                  URLConnection connection = url.openConnection();
                  InputStream in = connection.getInputStream();
                  filePath = Environment.getExternalStorageDirectory().getAbsolutePath()
                      + "/cashe.xml";
                  file = new File (filePath);
                  CreateFileFromInputStream(in,  filePath) ;
                  // Parse it with document builder factory
                  DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                  DocumentBuilder dBuilder = null;
                  dBuilder = dbFactory.newDocumentBuilder();
                  Document doc = null;
                  doc = dBuilder.parse(file);
                  doc.getDocumentElement().normalize();
                  // The root element is 
                  doc.getDocumentElement().getNodeName();
                  NodeList nList = doc.getElementsByTagName("Name");
  
                  for ( int i = 0 ; i &lt; nList.getLength() ; i++ ) {
                      Element element = (Element) nList.item(i) ;
                      dataPlaying = getCharacterDataFromElement(element);
                  }
              } catch (MalformedURLException e) {
                  e.printStackTrace(); 
              } catch (ParserConfigurationException e) {
                  e.printStackTrace(); 
              } catch (SAXException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace(); 
              }
  
              return null;
          }
  
          @Override
          protected void onPostExecute(Void result) {
              if (dataPlaying.equals(MainActivity.currentlyPlaying)){
                  if (MainActivity.replay == true){
                      MainActivity.replay = false;
                      MainActivity.titleMusic.setText("" + MainActivity.currentlyPlaying); 
                  }
              } else {
                  Log.d("MainActivity","Change track");
                  MainActivity.currentlyPlaying = dataPlaying;
                  MainActivity.titleMusic.setText("" + MainActivity.currentlyPlaying);
                  send();
              }
              super.onPostExecute(result);  
          }
      }
  
      public static void send(){
          notif(MainActivity.currentlyPlaying,MainActivity.currentlyPlaying);
      }
  
      public static String getCharacterDataFromElement(Element e) {
          Node node = e.getFirstChild();
          if (node instanceof CharacterData) {
              CharacterData cd = (CharacterData) node;
              return cd.getData();
          }
          return "";
      }
  
      public static void CreateFileFromInputStream(InputStream inStream, String path)
              throws IOException {
          // write the inputStream to a FileOutputStream
          OutputStream out = new FileOutputStream(new File(path));
  
          int read = 0;
          byte[] bytes = new byte[1024];
  
          while ((read = inStream.read(bytes)) != -1) {
              out.write(bytes, 0, read);
          }
  
          inStream.close();
          out.flush();
          out.close();
      }
  }
  </code></pre>
  
  <p><strong>LogCat:</strong> </p>
  
  <pre><code>03-12 06:16:21.324: D/MediaService(3475): Change track
  03-12 06:16:21.347: D/AndroidRuntime(3475): Shutting down VM
  03-12 06:16:21.347: W/dalvikvm(3475): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
  03-12 06:16:21.394: E/AndroidRuntime(3475): FATAL EXCEPTION: main
  03-12 06:16:21.394: E/AndroidRuntime(3475): java.lang.NullPointerException
  03-12 06:16:21.394: E/AndroidRuntime(3475):     at android.content.ComponentName.&lt;init&gt;(ComponentName.java:75)
  03-12 06:16:21.394: E/AndroidRuntime(3475):     at android.content.Intent.&lt;init&gt;(Intent.java:3122)
  03-12 06:16:21.394: E/AndroidRuntime(3475):     at com.spynetstation.MediaService.notif(MediaService.java:162)
  03-12 06:16:21.394: E/AndroidRuntime(3475):     at com.spynetstation.MediaService.send(MediaService.java:250)
  03-12 06:16:21.394: E/AndroidRuntime(3475):     at com.spynetstation.MediaService$Task.onPostExecute(MediaService.java:241)
  03-12 06:16:21.394: E/AndroidRuntime(3475):     at com.spynetstation.MediaService$Task.onPostExecute(MediaService.java:1)
  03-12 06:16:21.394: E/AndroidRuntime(3475):     at android.os.AsyncTask.finish(AsyncTask.java:602)
  03-12 06:16:21.394: E/AndroidRuntime(3475):     at android.os.AsyncTask.access$600(AsyncTask.java:156)
  03-12 06:16:21.394: E/AndroidRuntime(3475):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
  03-12 06:16:21.394: E/AndroidRuntime(3475):     at android.os.Handler.dispatchMessage(Handler.java:99)
  03-12 06:16:21.394: E/AndroidRuntime(3475):     at android.os.Looper.loop(Looper.java:137)
  03-12 06:16:21.394: E/AndroidRuntime(3475):     at android.app.ActivityThread.main(ActivityThread.java:4424)
  03-12 06:16:21.394: E/AndroidRuntime(3475):     at java.lang.reflect.Method.invokeNative(Native Method)
  03-12 06:16:21.394: E/AndroidRuntime(3475):     at java.lang.reflect.Method.invoke(Method.java:511)
  03-12 06:16:21.394: E/AndroidRuntime(3475):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
  03-12 06:16:21.394: E/AndroidRuntime(3475):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
  03-12 06:16:21.394: E/AndroidRuntime(3475):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>