unable to start activity from a service class in Android ( java.lang.NullPointerException )
<p>I have a service which i use to parse xml data (with sax) after what i want to start an activity to show the data parsed using an intent. But every time i launch the activity my service crashes, so i removed the intent and put a toast instead==> the service works fine. But i really need to start the activity from the service to make a query with xml rpc.  My little finger tells me that it has something to do with context but i don't know anything about context.</p>

<p>Here is my service: (the problem lies with the intent1)</p>

<pre><code> package tfe.rma.ciss.be;

import java.io.IOException;
import java.io.StringReader;
import java.net.MalformedURLException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class ParseFunction  extends Service{
    Server monServer;
    public String streamTitle = "",path="";
    public void onCreate() {

        Log.d("ParseFunction", "&gt;&gt;&gt;onCreate()");

    }

    @Override
    public void onStart (Intent intent, int startId) {
        Log.d("ParseFunction", "&gt;&gt;&gt;Started()"); 
        if (!mydata.equals("")){
         try {

             // URL rssUrl = new URL("http://thibault-koprowski.fr/feed");
              SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
              SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
              XMLReader myXMLReader = mySAXParser.getXMLReader();
              RSSHandler myRSSHandler = new RSSHandler();
              myXMLReader.setContentHandler(myRSSHandler);
              InputSource input = new InputSource(new StringReader(monServer.myData));
              myXMLReader.parse(input);
              Log.d("ParseFunction", "&gt;&gt;&gt;my data is parsed()" + streamTitle );

             } catch (MalformedURLException e) {
                 Log.d("ParseFunction", "&gt;&gt;&gt;exception()");  
              // TODO Auto-generated catch block
              e.printStackTrace();
              Log.d("ParseFunction", "&gt;&gt;&gt;exception()");
             } catch (ParserConfigurationException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
              Log.d("ParseFunction", "&gt;&gt;&gt;exception()");
             } catch (SAXException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
              Log.d("ParseFunction", "&gt;&gt;&gt;exception()");
             } catch (IOException e) {
                 Log.d("ParseFunction", "&gt;&gt;&gt;exception()");
              // TODO Auto-generated catch block
              e.printStackTrace();

             }
        }
        else {}
        if (!streamTitle.equals("")) {
            Log.d("ParseFunction", "&gt;&gt;&gt;splitting streamTitle"); 
            try {
             String delims = "[\n]+" ;
                String[] tokens = streamTitle.split(delims) ;
                path= tokens[1];
                Log.d("ParseFunction", "&gt;&gt;&gt;splitting done");

                Toast.makeText(this,path, Toast.LENGTH_SHORT).show();
                  }
            catch (Exception z) {
                Log.d("ParseFunction", "&gt;&gt;&gt;splitting error" + " " + z); 

            }
        } else 
        { path="n'a pas pu parser" + "/n" + "no path found"; }

        try {

            Log.d("ParseFunction", "&gt;&gt;&gt;lauching activity test but ...." );
        Intent intent1= new Intent (this, Test.class);
        intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent1.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);



        startActivity(intent1); } catch (Exception z) {

            Log.d("ParseFunction", "&gt;&gt;&gt;Activity said hell no, to screw myself " + z);

        }


    }
    private class RSSHandler extends DefaultHandler
      {
       final int stateUnknown = 0;
       final int stateTitle = 1;
       int state = stateUnknown;

       int numberOfTitle = 0;
       String strTitle = "";
       String strElement = "";

     @Override
     public void startDocument() throws SAXException {
      // TODO Auto-generated method stub

     }

     @Override
     public void endDocument() throws SAXException {
      // TODO Auto-generated method stub

      streamTitle =  strTitle;
     }

     @Override
     public void startElement(String uri, String localName, String qName,
       Attributes attributes) throws SAXException {
      // TODO Auto-generated method stub
      if (localName.equalsIgnoreCase("sensorType"))
      {
       state = stateTitle;
       strElement = "";
       numberOfTitle++;
      }
      else if (localName.equalsIgnoreCase("imagePath"))
      {
       state = stateTitle;
       strElement = "";
       numberOfTitle++;
      }
      else if (localName.equalsIgnoreCase("numThreats"))
      {
       state = stateTitle;
       strElement = "";
       numberOfTitle++;
      }

      else if (localName.equalsIgnoreCase("latitude"))
      {
       state = stateTitle;
       strElement = "";
       numberOfTitle++;
      }
      else if (localName.equalsIgnoreCase("longitude"))
      {
       state = stateTitle;
       strElement = "";
       numberOfTitle++;
      }
      else
      {
       state = stateUnknown;
      }
     }

     @Override
     public void endElement(String uri, String localName, String qName)
       throws SAXException {
      // TODO Auto-generated method stub
      if (localName.equalsIgnoreCase("sensorType"))
      {
       strTitle += strElement + "\n";
      }
      else if (localName.equalsIgnoreCase("imagePath"))
      {
       strTitle += strElement + "\n";
      }
      else if (localName.equalsIgnoreCase("numThreats"))
      {
       strTitle += strElement + "\n";
      }

      else if (localName.equalsIgnoreCase("latitude"))
      {
       strTitle += strElement + "\n";
      }
      else if (localName.equalsIgnoreCase("longitude"))
      {
       strTitle += strElement + "\n";
      }

      else {
      state = stateUnknown;}
     }

     @Override
     public void characters(char[] ch, int start, int length)
       throws SAXException {
      // TODO Auto-generated method stub
      String strCharacters = new String(ch, start, length);
      if (state == stateTitle)
      {
       strElement += strCharacters;
      }
     }

      }
    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }
     public static String mydata = "&lt;AvailableImageMsg&gt;&lt;imageData&gt;&lt;imageId&gt;1234&lt;/imageId&gt;&lt;sensorType&gt;2&lt;/sensorType&gt;&lt;imagePath&gt;http://localhost/some/path&lt;/imagePath&gt;&lt;timeStamp&gt;2011-10-12T12:36:20&lt;/timeStamp&gt;&lt;/imageData&gt;&lt;numThreats&gt;3&lt;/numThreats&gt;&lt;threatList&gt;&lt;ThreadPixelDataType&gt;&lt;threatData&gt;&lt;threatId&gt;3456&lt;/threatId&gt;&lt;latitude&gt;50.1234&lt;/latitude&gt;&lt;longitude&gt;4.5678&lt;/longitude&gt;&lt;altitude&gt;10.1&lt;/altitude&gt;&lt;threatType&gt;pepe&lt;/threatType&gt;&lt;timestamp&gt;2011-10-12T12:36:20&lt;/timestamp&gt;&lt;threatLevel&gt;70&lt;/threatLevel&gt;&lt;/threatData&gt;&lt;Rectangle&gt;&lt;PixelCoordinatesType&gt;&lt;x&gt;100&lt;/x&gt;&lt;y&gt;200&lt;/y&gt;&lt;/PixelCoordinatesType&gt;&lt;PixelCoordinatesType&gt;&lt;x&gt;100&lt;/x&gt;&lt;y&gt;200&lt;/y&gt;&lt;/PixelCoordinatesType&gt;&lt;PixelCoordinatesType&gt;&lt;x&gt;100&lt;/x&gt;&lt;y&gt;200&lt;/y&gt;&lt;/PixelCoordinatesType&gt;&lt;PixelCoordinatesType&gt;&lt;x&gt;100&lt;/x&gt;&lt;y&gt;200&lt;/y&gt;&lt;/PixelCoordinatesType&gt;&lt;/Rectangle&gt;&lt;/ThreadPixelDataType&gt;&lt;/threatList&gt;&lt;threatList&gt;&lt;ThreadPixelDataType&gt;&lt;threatData&gt;&lt;threatId&gt;3457&lt;/threatId&gt;&lt;latitude&gt;50.1234&lt;/latitude&gt;&lt;longitude&gt;4.5678&lt;/longitude&gt;&lt;altitude&gt;10.1&lt;/altitude&gt;&lt;threatType&gt;pepe&lt;/threatType&gt;&lt;timestamp&gt;2011-10-12T12:36:20&lt;/timestamp&gt;&lt;threatLevel&gt;70&lt;/threatLevel&gt;&lt;/threatData&gt;&lt;Rectangle&gt;&lt;PixelCoordinatesType&gt;&lt;x&gt;100&lt;/x&gt;&lt;y&gt;200&lt;/y&gt;&lt;/PixelCoordinatesType&gt;&lt;PixelCoordinatesType&gt;&lt;x&gt;100&lt;/x&gt;&lt;y&gt;200&lt;/y&gt;&lt;/PixelCoordinatesType&gt;&lt;PixelCoordinatesType&gt;&lt;x&gt;100&lt;/x&gt;&lt;y&gt;200&lt;/y&gt;&lt;/PixelCoordinatesType&gt;&lt;PixelCoordinatesType&gt;&lt;x&gt;100&lt;/x&gt;&lt;y&gt;200&lt;/y&gt;&lt;/PixelCoordinatesType&gt;&lt;/Rectangle&gt;&lt;/ThreadPixelDataType&gt;&lt;/threatList&gt;&lt;threatList&gt;&lt;ThreadPixelDataType&gt;&lt;threatData&gt;&lt;threatId&gt;3458&lt;/threatId&gt;&lt;latitude&gt;50.1234&lt;/latitude&gt;&lt;longitude&gt;4.5678&lt;/longitude&gt;&lt;altitude&gt;10.1&lt;/altitude&gt;&lt;threatType&gt;pepe&lt;/threatType&gt;&lt;timestamp&gt;2011-10-12T12:36:20&lt;/timestamp&gt;&lt;threatLevel&gt;70&lt;/threatLevel&gt;&lt;/threatData&gt;&lt;Rectangle&gt;&lt;PixelCoordinatesType&gt;&lt;x&gt;100&lt;/x&gt;&lt;y&gt;200&lt;/y&gt;&lt;/PixelCoordinatesType&gt;&lt;PixelCoordinatesType&gt;&lt;x&gt;100&lt;/x&gt;&lt;y&gt;200&lt;/y&gt;&lt;/PixelCoordinatesType&gt;&lt;PixelCoordinatesType&gt;&lt;x&gt;100&lt;/x&gt;&lt;y&gt;200&lt;/y&gt;&lt;/PixelCoordinatesType&gt;&lt;PixelCoordinatesType&gt;&lt;x&gt;100&lt;/x&gt;&lt;y&gt;200&lt;/y&gt;&lt;/PixelCoordinatesType&gt;&lt;/Rectangle&gt;&lt;/ThreadPixelDataType&gt;&lt;/threatList&gt;&lt;/AvailableImageMsg&gt;";


}
</code></pre>

<p><strong>Here is my simple activity named test</strong></p>

<pre><code>package tfe.rma.ciss.be;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class Test extends Activity{

    ParseFunction parser; 

    @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.test);

          TextView result = (TextView)findViewById(R.id.result);




          result.setText(parser.path);}



}
</code></pre>

<p>can someone help me please?????!</p>

<p><strong>Logcat report</strong></p>

<pre><code>01-12 13:42:18.069 I/ActivityManager(  160): Start proc tfe.rma.ciss.be for service tfe.rma.ciss.be/.ParseFunction: pid=1576 uid=10065 gids={3003, 1015}
01-12 13:42:18.189 D/ParseFunction( 1576): &gt;&gt;&gt;onCreate()
01-12 13:42:18.189 D/ParseFunction( 1576): &gt;&gt;&gt;Started()
01-12 13:42:18.189 D/AndroidRuntime( 1576): Shutting down VM
01-12 13:42:18.189 W/dalvikvm( 1576): threadid=1: thread exiting with uncaught exception (group=0x40018578)
01-12 13:42:18.199 E/        (  160): Dumpstate &gt; /data/log/dumpstate_app_error
01-12 13:42:18.199 E/AndroidRuntime( 1576): FATAL EXCEPTION: main
01-12 13:42:18.199 E/AndroidRuntime( 1576): java.lang.RuntimeException: Unable to start service tfe.rma.ciss.be.ParseFunction@4051ecc8 with Intent { cmp=tfe.rma.ciss.be/.ParseFunction }: java.lang.NullPointerException
01-12 13:42:18.199 E/AndroidRuntime( 1576):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2056)
01-12 13:42:18.199 E/AndroidRuntime( 1576):     at android.app.ActivityThread.access$2800(ActivityThread.java:117)
01-12 13:42:18.199 E/AndroidRuntime( 1576):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:998)
01-12 13:42:18.199 E/AndroidRuntime( 1576):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-12 13:42:18.199 E/AndroidRuntime( 1576):     at android.os.Looper.loop(Looper.java:123)
01-12 13:42:18.199 E/AndroidRuntime( 1576):     at android.app.ActivityThread.main(ActivityThread.java:3687)
01-12 13:42:18.199 E/AndroidRuntime( 1576):     at java.lang.reflect.Method.invokeNative(Native Method)
01-12 13:42:18.199 E/AndroidRuntime( 1576):     at java.lang.reflect.Method.invoke(Method.java:507)
01-12 13:42:18.199 E/AndroidRuntime( 1576):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
01-12 13:42:18.199 E/AndroidRuntime( 1576):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
01-12 13:42:18.199 E/AndroidRuntime( 1576):     at dalvik.system.NativeStart.main(Native Method)
01-12 13:42:18.199 E/AndroidRuntime( 1576): Caused by: java.lang.NullPointerException
01-12 13:42:18.199 E/AndroidRuntime( 1576):     at tfe.rma.ciss.be.ParseFunction.onStart(ParseFunction.java:45)
01-12 13:42:18.199 E/AndroidRuntime( 1576):     at android.app.Service.onStartCommand(Service.java:428)
01-12 13:42:18.199 E/AndroidRuntime( 1576):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2043)
01-12 13:42:18.199 E/AndroidRuntime( 1576):     ... 10 more
01-12 13:42:20.709 D/BatteryService(  160): update start
01-12 13:42:20.709 D/BatteryService(  160): update start
01-12 13:42:20.709 D/BatteryService(  160): update start
01-12 13:42:22.079 D/VoldCmdListener(   87): asec list
01-12 13:42:22.089 D/VoldCmdListener(   87): CommandListener::AsecCmd::runCommand -&gt; com.aldiko.android-1
01-12 13:42:22.089 D/VoldCmdListener(   87): CommandListener::AsecCmd::runCommand -&gt; com.citc.ud-1
01-12 13:42:22.089 D/VoldCmdListener(   87): CommandListener::AsecCmd::runCommand -&gt; com.creativemobile.DragRacing-1
</code></pre>
