Search code examples
androidrunnableschedule

android fatal execption Timer-0


I need a timer for my program.in my android application i'm using Timer schedule.but getting Timer-0 fatal exception as below.What am I doing wrong?

Logcat:

      08-20 20:18:29.246: E/AndroidRuntime(25999): FATAL EXCEPTION: Timer-0
      08-20 20:18:29.246: E/AndroidRuntime(25999): Process: com.example.ancam, PID: 25999
      08-20 20:18:29.246: E/AndroidRuntime(25999): java.lang.NullPointerException
      08-20 20:18:29.246: E/AndroidRuntime(25999):  at com.example.ancam.MainActivity$1.run(MainActivity.java:191)
      08-20 20:18:29.246: E/AndroidRuntime(25999):  at java.util.Timer$TimerImpl.run(Timer.java:284)

Java Code:

        mTimer.schedule( new TimerTask(){

            @Override
            public void run() {



                 try{
                     mediarecorder.prepare();
                     mediarecorder.start();
                 }catch(IllegalStateException e){
                     e.printStackTrace();
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
                             try{
                                 Thread.sleep(10000);
                             }catch(InterruptedException e){
                                 e.printStackTrace();
                             }

                             mediarecorder.stop();

                             new Thread(new Runnable() {
                                    public void run() {
                                         uploadFile(mm);

                                    }
                                  }).start();        
                             mediarecorder.reset();  

             }
        }, 0, 500);      

Solution

  • You are getting null pointer exception on line 191. That means object on that line is null and you are trying to call its method.

    line 191 has mediarecorder.stop(); and mediarecorder is null thats the reason you are getting this exception.

    Make sure to initialize mediarecorder object before using it.

    Please refer below link in order to properly intialize mediarecorder.

    http://developer.android.com/reference/android/media/MediaRecorder.html