2

when I develop android application,I want to make a CrashReport class and then use it send report to my server.

I make a class called CrashHandler which implement UncaughtExceptionHandler,and make a example to cause a NullPoint error in Activity in purpose,the class works well , but a little problem... in method uncaughtException:

    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
        ex.printStackTrace(); //**code line 1**

        ....//some other codes and variables

        StackTraceElement[] elements = ex.getStackTrace(); 
        for (StackTraceElement element : elements) {       // **code line 2**
            errorString = errorString + "<br/>" + element.toString();
    }
    }

code line 1 print the stack trace as:

java.lang.NullPointerException

my.app.ExceptionCaughtActivity$1.onClick(ExceptionCaughtActivity.java:25)
android.view.View.performClick(View.java:2486)
android.view.View$PerformClick.run(View.java:9130)
android.os.Handler.handleCallback(Handler.java:587)
android.os.Handler.dispatchMessage(Handler.java:92)
android.os.Looper.loop(Looper.java:130)
android.app.ActivityThread.main(ActivityThread.java:3703)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:507)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
dalvik.system.NativeStart.main(Native Method)

but the codeline only show:

my.app.ExceptionCaughtActivity$1.onClick(ExceptionCaughtActivity.java:25)
android.view.View.performClick(View.java:2486)
android.view.View$PerformClick.run(View.java:9130)
android.os.Handler.handleCallback(Handler.java:587)
android.os.Handler.dispatchMessage(Handler.java:92)
android.os.Looper.loop(Looper.java:130)
android.app.ActivityThread.main(ActivityThread.java:3703)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:507)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
dalvik.system.NativeStart.main(Native Method)

you notice it not showed the:

"java.lang.NullPointerException"

I konw I do not called the getCause() but when try to use getCause(), it return a null, not a throwable;

I tried to view the source code of throwable class...but got noting

can you tell me why the getCause is null but printStackTrace have a error cause to print out ??

daimajia
  • 967
  • 12
  • 15

4 Answers4

1

ex.printStackTrace() helpfully prints the class and message (if any) of the Throwable object ex as well as the trace information. See Throwable.toString().

The raw stack trace itself doesn't include this information but you can get it from the Throwable object, which may or may not have an message, and may or may not have a cause. It always has a class though! ;-)

Here's the source code from Throwable:

public void printStackTrace(PrintStream s) {
    synchronized (s) {
        s.println(this);
        StackTraceElement[] trace = getOurStackTrace();
        for (int i=0; i < trace.length; i++)
            s.println("\tat " + trace[i]);

        Throwable ourCause = getCause();
        if (ourCause != null)
            ourCause.printStackTraceAsCause(s, trace);
    }
}
DNA
  • 42,007
  • 12
  • 107
  • 146
0

getCause() returns null if the throwable is at the top of the exception chain. It is the cause so it can't have a cause.

You can read about exception chaining here.

Jivings
  • 22,834
  • 6
  • 60
  • 101
0

Have you tried getMesssage or getLocalizedMessage??

http://docs.oracle.com/javase/6/docs/api/java/lang/Throwable.html#getMessage()

Lucas Katayama
  • 4,445
  • 27
  • 34
0

First line is getting from this:

public String toString() {
    String s = getClass().getName();
    String message = getLocalizedMessage();
    return (message != null) ? (s + ": " + message) : s;
}

So if you want to add first line use:

errorString += ex.toString + "<br/>";
StackTraceElement[] elements = ex.getStackTrace(); 
for (StackTraceElement element : elements) {       // **code line 2**
    errorString = errorString + "<br/>" + element.toString();
}
kornero
  • 1,109
  • 8
  • 11
  • public Throwable(Throwable cause) { fillInStackTrace(); detailMessage = (cause==null ? null : cause.toString()); this.cause = cause; } this.cause = cause ; but why ex.getCause() is null????? – daimajia Feb 09 '12 at 14:45
  • How do you get throwable? new Throwable(null), for example will create null cause. – kornero Feb 09 '12 at 14:49