Search code examples
javaandroidlogginglogcat

Error using Log.e();


My application kept crashing, so I tried making a try and catch block to find the error

try{
        sm = (SensorManager) this.activity.getSystemService(this.activity.SENSOR_SERVICE);
        acceleration = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sm.registerListener(this, acceleration, SensorManager.SENSOR_DELAY_NORMAL);
    }
    catch(Exception f){
        Log.e("Error initializing sensors in GameScene", f.getLocalizedMessage());
    }

I then checked logcat for the error and found that the error it was giving me was actually an error from the Log method

12-26 12:43:52.094: E/AndroidRuntime(17183): FATAL EXCEPTION: AsyncTask #1
12-26 12:43:52.094: E/AndroidRuntime(17183): Process: com.magnusworks.justtic_tac_toe, PID: 17183
12-26 12:43:52.094: E/AndroidRuntime(17183): java.lang.RuntimeException: An error occured while executing doInBackground()
12-26 12:43:52.094: E/AndroidRuntime(17183):    at android.os.AsyncTask$3.done(AsyncTask.java:300)
12-26 12:43:52.094: E/AndroidRuntime(17183):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
12-26 12:43:52.094: E/AndroidRuntime(17183):    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
12-26 12:43:52.094: E/AndroidRuntime(17183):    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
12-26 12:43:52.094: E/AndroidRuntime(17183):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
12-26 12:43:52.094: E/AndroidRuntime(17183):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
12-26 12:43:52.094: E/AndroidRuntime(17183):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
12-26 12:43:52.094: E/AndroidRuntime(17183):    at java.lang.Thread.run(Thread.java:841)
12-26 12:43:52.094: E/AndroidRuntime(17183): Caused by: java.lang.NullPointerException: println needs a message
12-26 12:43:52.094: E/AndroidRuntime(17183):    at android.util.Log.println_native(Native Method)
12-26 12:43:52.094: E/AndroidRuntime(17183):    at android.util.Log.e(Log.java:334)
12-26 12:43:52.094: E/AndroidRuntime(17183):    at com.magnusworks.justtic_tac_toe.GameScene.create(GameScene.java:49)
12-26 12:43:52.094: E/AndroidRuntime(17183):    at com.magnusworks.justtic_tac_toe.SceneManager$1.doInBackground(SceneManager.java:37)
12-26 12:43:52.094: E/AndroidRuntime(17183):    at com.magnusworks.justtic_tac_toe.SceneManager$1.doInBackground(SceneManager.java:1)
12-26 12:43:52.094: E/AndroidRuntime(17183):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
12-26 12:43:52.094: E/AndroidRuntime(17183):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
12-26 12:43:52.094: E/AndroidRuntime(17183):    ... 4 more

I did some research and couldn't find anything similar.... am I using Log.e incorrectly?


Solution

  • The second argument to a log call must be non-null. getLocalizedMessage() can return null.

    Consider using the three-arg version of your log call to log the complete exception stacktrace, e.g.

    Log.e("GameScene", "Error initializing sensors in GameScene", f);
    

    The first argument is the log tag and by convention it's something short you can filter your logs with.