6

A similar question was asked here for two times and never there was any answer. Or the answer was: "it is impossible!" Sorry, it is possible too much:

try{
    ...
    // the line that causes the error
    LinearLayout cell = (LinearLayout) inflater.inflate(R.layout.channels_list_cell, column);
    ...
}
catch(Throwable e){
    Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show(); < breakpoint here!
}

At the breakpoint e is null. How can I seek for the error, please? Very possibly it is not the problem of java or Android, but of the Eclipse debugger, that itself needs debugging badly. But what have I to do, except changing to a different IDE? Any ideas? Beforehand grateful.

I have tried Throwable, Exception, RuntimeException. The result is the same.

An attempt to step over breakpoint causes NullPointerException, so, e seems really null at that moment already. Where could it be lost?

Edit: I bring my gratitude to everybody and +1 to every answerer. It was an Eclipse bug. After restart Eclipse the Exception is not null anymore, it is a normal RuntimeException: Binary XML file line #15: You must supply a layout_width attribute. It would be another problem to be solved, but that one is solved.

Gangnus
  • 24,044
  • 16
  • 90
  • 149
  • 2
    Why are you catching a Throwable instead of an Exception? – Rich Jan 17 '12 at 16:47
  • I have tried Throwable, Exception, RuntimeException. The result is the same. – Gangnus Jan 17 '12 at 16:49
  • 2
    I never use the debugger, myself, but from what I've seen when other people use it, what it shows for a given object seems to be the result of calling `String.valueOf(...)` on that object. So if the caught exception's `toString()` method returns either `null` or `'null'`, then the debugger can give the false impression that the caught exception itself is `null`. – ruakh Jan 17 '12 at 16:50
  • What is LogCat saying when your get the exception? – bschultz Jan 17 '12 at 16:51
  • you sure about that? Do you know for a fact that the exception is null, or is this an assumption based on some runtime factors? What if you change the toast code to a simple Log.d("", e.getMessage())? – Rich Jan 17 '12 at 16:52
  • I am not trying, I am getting a breakpoint. And where it is, is written in the code. Yes, I am using the debugger. I have mentioned the debugger twice, haven't I? Forgive me, if I am not expressing myself clearly enough. – Gangnus Jan 17 '12 at 16:53
  • @ruakh. No. The debugger inspects variables BEFORE fulfilling the line it breaks on. And it is e, that is null, not e.ToString(). BTW, that would be the same problem - the error without any message. – Gangnus Jan 17 '12 at 16:55
  • @Pheonixblade9. I usually try to manage my problems myself. So, I ask more difficult questions. As a result, I do not get answers often. You made me to recognize an "answer" that was not a real answer. – Gangnus Jan 17 '12 at 17:03
  • @Pheonixblade9. I haven't noticed that somebody even plused, let alone checked my "thoughts that could be useful". But ok, I'll try to be the kindest one. :-) – Gangnus Jan 17 '12 at 17:19
  • Okay. Back on topic, the breakpoint is likely null because that line hasn't been evaluated yet. Try putting a dummy line of code below, and setting the breakpoint there. It should work for you. – Codeman Jan 17 '12 at 17:20
  • @Pheonixblade9 your solution is inferring that eclipse is bugged in a so obvious way which is totally ridiculous!! when a line is reached, all previous related lines have been evaluated – Adel Boutros Jan 17 '12 at 17:28
  • Yes. The previous lines are evaluated. The current line is not until you step through it. – Codeman Jan 17 '12 at 17:29
  • @Gangnus: I think you must have misunderstood what I wrote. – ruakh Jan 17 '12 at 17:41
  • @Pheonixblade9. I'll never get there. The Toast line is causing NullPointerException, because I am trying to use a null object. – Gangnus Jan 17 '12 at 21:20
  • @Adel Boutros. Excuse me, but the whole problem coud arise only because some my error causes error in Android, Eclipse or its debugger. Whithout error in some of them e wouldn't become null. And Eclipse, expecially debugger, is bugged enough, I am afraid. – Gangnus Jan 17 '12 at 21:23
  • @ruakh. Sorry. But it is the same - When I see "inspect" on object, I do not see its "toString" result, but the whole structure of the instance with values. And here I see naked null, as is seen on uncreated objects. And after a step over toast I get the NullPointerException... – Gangnus Jan 17 '12 at 21:28
  • What device are you seeing this behavior on? What version of android is it running? I just wrote a quick example app that attempts to throw a null value, and an NPE occurred at the throw statement (like it should). – JesusFreke Jan 17 '12 at 23:59

5 Answers5

2

If the exception you caught was a NullPointerException, the getMessage() method returns "null" which may be confusing. I know that this has sometimes confused me!

In the debugger, you should be able to select e and see a type and its fields. Also, another way to debug when things get really confusing is to go

 e.printStackTrace();

(note - I'm not an Android guru so if this works differently on Android somebody please comment!)

user949300
  • 15,364
  • 7
  • 35
  • 66
  • 4
    Dude, he said `e` is null. How can you access the method `printStackTrace()` if the object is null ?? – Adel Boutros Jan 17 '12 at 21:09
  • It is an interesting thought. But... When I see "inspect" on something, I do not see its "toString" result, but the whole structure of the instance with values. And here I see naked null, as is seen on uncreated objects. Maybe, Eclipse makes such error here, that it works as you say, I will check tomorrow. – Gangnus Jan 17 '12 at 21:10
  • @Adel Boutros. He thinks, that the object is not really null, it is only showing itself as null due to some error in debugger. He only left off that sentence as an implicite one. And it is highly probable. As for concrete attempts to reveal the hidden inside of e, sorry, I am not at work and can continue only tomorrow. – Gangnus Jan 17 '12 at 21:15
  • @Gangus if you see naked null then my guess is wrong. I have never seen a catch clause catch a null. Maybe Android is different? But double check again tomorrow. – user949300 Jan 18 '12 at 01:06
  • same prob with me . any solution. naked null – Rajiv yadav Jun 27 '13 at 11:28
2

Have you verified whether e is actually null or not? I.e. by adding something like if (e == null) Log.d("Exception is null"). I would then check if the log statement gets triggered both during normal execution and while debugging. If the results are different between the two, it would indicate a VM bug (unlikely, but possible). If the message doesn't get triggered in either case, then it's likely a debugger issue.

A few thoughts on further things you can try to debug the issue:

  • Try something like jdb and see if you get the same behaviour

  • You could get a dump of the jdwp communications between the debugger and the device, and see what's going on at that level. Maybe use wireshark or tcpdump and grab the data going over the usb bus to the device.

  • You could try adding some debug statements to dalvik itself. E.g. grab a copy of AOSP and build an emulator image, and then add some debugging statements to dalvik to try and track down what's going on.

  • You could attempt to do some sort of scripted jdwp session with the device

  • You could look at the bytecode (baksmali/dexdump/dedexer), to see if anything looks funny

JesusFreke
  • 19,784
  • 5
  • 65
  • 68
  • Thank you. It is funny, I was struggling with turning on the assert and the much more easy way (if..then) had escaped my attention. When I'll be back at work, I'll do it immediately. As for jd and jdwp, wireshark and tcpdump, AOSP, sorry, I am reading about them for the first time. As for bytecode, I can read it, but I don't know where to look for it for java and what is written there and how it is written. Sorry, level of these advices is too high to be useful for me. – Gangnus Jan 17 '12 at 22:12
  • Right, most of my advice would require a good working knowledge of the android platform. As to the last point in particular, you can run baksmali on the apk in order to get the dalvik bytecode. – JesusFreke Jan 17 '12 at 23:57
  • Really, I do not understand the need of so deep learning of the technology that is so buggy that the solution in 99% of variants is to restart or reload something or change the soft used (for having **other** bugs). It was useful 20 or 40 years before, when the descriptions were fit to the SW. – Gangnus Jan 18 '12 at 08:23
  • I'm not sure I understand what you mean. I am just trying to help you troubleshoot the issue :) – JesusFreke Jan 19 '12 at 00:27
  • And I am very grateful for it! I'll save your list of debudding techs to return to it later. ---- It was a merely sad thought about the state of our art. The more buggish our tools become, the less sense is in deeper understanding and we look more and more as a noobie, who simply tries many different tools, passing them so from one heap to another – Gangnus Jan 19 '12 at 08:29
1

I know this question was posted a while ago, and many times too! I fell into this trap yesterday and I thought I'll post what I found.

Problem definition: I used the following code

public class myAppActivity extends Activity
{
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      try { -- lots of code -- }
      catch (Exception ex) {
          Log.e ("eTutorPrism Error", "Caught this exception " + ex);
          ex.printStackTrace();
      }
   }
}

Symptom was that 'ex' was always null and resume will give NullPointerException, although the actual exception was an IllegalArgumentException in a call made into another class from the code above.

ISSUE: onCreate() code does not display the exception caught. instead it shows exception = null.

Solution: do NOT use too much processing in onCreate(). Move as much as possible to another thread. So I changed the code to look like the following. voila, it works!!! I can see the actual exception displayed in the Logcat.

public class eTutorPrismAppActivity extends Activity
{
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState)
   {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);

       eTutorPrismTest myTest = new eTutorPrismTest (getApplicationContext());
       myTest.start();
   }
}

class eTutorPrismTest extends Thread
{
   private Context m_AppContext = null;

   public eTutorPrismTest (Context appContext)
   {
      m_AppContext = appContext;
   }

   public void run ()
   {   
      -- lots of code that needs appContext --
   }
}
edZilla
  • 19
  • 2
1

I am unsure of what causes this -- it could be an Eclipse bug as stated earlier. Regardless of the cause, I did find a workaround that seems to work. I hope it is useful to others as well.

After the exception is caught, assign it to another variable. The assigned variable should contain the correct Exception in the debugger.

SpecificException assignedVar = null;
try {
    ...
}
catch (SpecificException exc) {
    assignedVar = exc; // <-- exc comes up null in the debugger, but assignedVar will be the correct object.
}

Hope this works for others as a workaround.

Matthew
  • 11
  • 1
1

Android does not always throws exception in a Throwable. It actually drives all the exceptions to the catLog. There you will find details of your exceptions even if in the catch block your exception is null.

You can easily access the catlog console from eclipse and filter to view the errors only

UPDATE:

Your breakpoint should be inside the catch block

Adel Boutros
  • 10,205
  • 7
  • 55
  • 89
  • logcat doesn't catch it. (filters off, log cleaned just before the line causing the error) – Gangnus Jan 17 '12 at 16:59
  • There are many of them before the line that causes the exception. I go by step. And I have to have a break at the toast line - or there will be uncatched error nullPointer there. – Gangnus Jan 17 '12 at 17:05
  • The other sugnificant one. Sorry, I have superscribed it just now. – Gangnus Jan 17 '12 at 17:08
  • @Gangnus and what is inflater? please provide all the useful code. – Adel Boutros Jan 17 '12 at 17:11
  • I thank you for your questions, that allowed me to improve my original question. +1 to your answer. As for all code, I will be back to work in 11 hours and will do it then. – Gangnus Jan 17 '12 at 21:05
  • 1
    Android does not implicitly log exceptions. For example, if you catch an exception and eat it (don't print it, don't rethrow it), then nothing will be printed to logcat. (catLog? huh?) – JesusFreke Jan 17 '12 at 21:48
  • 1
    @JesusFreke catLog is better to the ear then logcat – Adel Boutros Jan 17 '12 at 21:51
  • @Adel Boutros: despite the fact that it's referred to as logcat nearly everywhere else? It doesn't make sense to arbitrarily rename something just because you don't like the name. It's not a big deal of course.. :) – JesusFreke Jan 17 '12 at 23:55
  • @AdelBoutros. Feel free to use the Catlog name for some log of your own :-) – Gangnus Jan 19 '12 at 08:32
  • @Gangnus guys this is not a chatting site. Please only post useful comments which are related to the question. – Adel Boutros Jan 19 '12 at 08:39
  • @JesusFreke guys this is not a chatting site. Please only post useful comments which are related to the question. – Adel Boutros Jan 19 '12 at 08:39