views:

123

answers:

1

Hello

I have implemented a timer that parses a URL every 15min (the Timer task). An Object that i have created gets that data .I use it afterwards to display the data on the screen .

Now , whenever i try to retrieve that Object/a String=Object.toString() out of the runnable I get null pointer exception and fatal errors .

My question is whether it is possible to do using some other technique to do it , or The Object cease to exist out of the runnable and there is not much we can do about it ;if that is the case can anybody tell me if there is another way of implementing a timer/runnable ?

Thanks a Lot

here is most of my code where i have a problem

 @Override
    public void onCreate(Bundle icicle) {
            super.onCreate(icicle)
           final  TextView tv = new TextView(this);
            TimerTask scanTask;
            final Handler handler = new Handler();
            Timer t = new Timer();                  
                scanTask = new TimerTask() {
                    public void run() {
                            handler.post(new Runnable() {
                                    public void run() {
                                        URL url = null;
                                        try {
                                            url = new URL("http://www.eurosport.fr/");
                                        } catch (MalformedURLException e3) {

                                            e3.printStackTrace();
                                        }

                                        /* Get a SAXParser from the SAXPArserFactory. */
                                        SAXParserFactory spf = SAXParserFactory.newInstance();
                                        SAXParser sp;
                                        try {
                                            sp = spf.newSAXParser();
                                        } catch (ParserConfigurationException e2) {

                                            e2.printStackTrace();
                                        } catch (SAXException e2) {

                                            e2.printStackTrace();
                                        }

                                        /* Get the XMLReader of the SAXParser we created. */
                                        XMLReader xr = null;
                                        try {
                                            sp = spf.newSAXParser();
                                            xr = sp.getXMLReader();
                                        } catch (SAXException e1) {

                                            e1.printStackTrace();
                                        } catch (ParserConfigurationException e) {

                                            e.printStackTrace();
                                        }
                                        /* Create a new ContentHandler and apply it to the XML-Reader*/
                                        ExampleHandler myExampleHandler = new ExampleHandler();
                                        try {
                                            sp = spf.newSAXParser();
                                        } catch (ParserConfigurationException e1) {
                                            // TODO Auto-generated catch block
                                            e1.printStackTrace();
                                        } catch (SAXException e1) {
                                            // TODO Auto-generated catch block
                                            e1.printStackTrace();
                                        }
                                        xr.setContentHandler(myExampleHandler);

                                        /* Parse the xml-data from our URL. */
                                        try {
                                            xr.parse(new InputSource(url.openStream()));
                                        } catch (IOException e) {

                                            e.printStackTrace();
                                        } catch (SAXException e) {

                                            e.printStackTrace();
                                        }
                                        /* Parsing has finished. */

                                        /* Our ExampleHandler now provides the parsed data to us. */
                                        ParsedExampleDataSet parsedExampleDataSet =
                                                                                        myExampleHandler.getParsedData();

                                       System.out.println(parsedExampleDataSet.toString());

                                        tv.setText(parsedExampleDataSet.toString());


                                     Context context = this.getBaseContext(); 

 // I also dont understand why inside the runnable getBaseContext() does not exist ???

    Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),
            R.raw.nature1)
        context.setWallpaper(mBitmap);


                                    }


                           });

                    }  };
                    // I want to retrieve ParsedExampleDataSEt here in order to use it  is it Possible ????



                    this.setContentView(tv);



                   long temps=1*15*1000;

                t.scheduleAtFixedRate(scanTask, 300,temps ); 
+1  A: 

The potentially ugly method: Extend TimerTask and make an abstract method such as

public abstract void onUrlRetrivalFinished(String data);

When you create the TimerTask object you can now make a anonymous implementation of the method, and in that method handle the retrieved data.

The (in my opinion) less ugly method:

Make an interface such as:

public interface UrlRetrivalListener {
    public void onUrlRetrivalFinished(String data);
}

Sub-class TimerTask and make a field such as:

private UrlRetrivalListener listener;

Now make an implementation of the above mentioned listener interface, in which you handle the retrieved String. Pass the listener as a parameter to your TimerTask, or even let the TimerTask have more than one listener, and upon retrieving/parsing the data needed, you simply call the listeners onUrlRetrivalFinished() method.

This should do the trick, but some more information would be nice.

Jes
I just added most of my code . I can get the data and display it on the screen . but i cant use it outside the runnable . If you feel that my code is not good enough feel free to edit and give me some feedback as i am new to android ! CheersI added my main questions as comments in the code ;)
Amine