3

I found a problem online and started doing it for fun. The question was to identify the method which causes a NullPointerException in a java application. Here is the code for the same.

Code:

public void theChainLinkFence() {       
    System.out.println(Constants.Fence.GREETING);        
    chain.is().only().as().strong().as().its().weakest().link();
    ChainFence.allowThrough(chain);
}

The third line throws the exception, but I am not sure how to debug a line with multiple method calls. Kindly provide your suggestion on how to approach this problem.

PS: Chain is a class present in an external jar file with no source given.

Update: A similar Question gave me an idea on how to approach the problem.

Community
  • 1
  • 1
nahomyaja
  • 202
  • 2
  • 12
  • 2
    Break it into individual calls. – user1803551 Sep 17 '16 at 23:39
  • I tried that. But, it didn't work. I mean, every method seems to be throwing an exception in that case. – nahomyaja Sep 17 '16 at 23:42
  • What didn't work, copy-paste? – user1803551 Sep 17 '16 at 23:44
  • No. Every function call threw an exception when I broke it down to individual calls. – nahomyaja Sep 17 '16 at 23:46
  • 1
    So? Catch or throw it. And if every call in the chain did it, then the whole chain will do it too. It means that you should be getting the same compiler message in your example too. A method throwing an exception is by design, it doesn't mean that it doesn't work. – user1803551 Sep 17 '16 at 23:47
  • I do understand the concept of exceptions. My question is, if every method call throws an exception, since there is no source given, is it possible to identify which method actually throws the exception? It doesn't make sense that every method throws an exception. Since I am not familiar with chained function calls, I wanted to know if the calls are interdependent. – nahomyaja Sep 17 '16 at 23:53
  • 1
    Possible duplicate of [How to trace a NullPointerException in a chain of getters](http://stackoverflow.com/questions/410890/how-to-trace-a-nullpointerexception-in-a-chain-of-getters) – user1803551 Sep 17 '16 at 23:57
  • Thank you. Let me check that post. – nahomyaja Sep 18 '16 at 00:00
  • 1
    "*if every method call throws an exception, since there is no source given, is it possible to identify which method actually throws the exception?*" See my first comment. "*It doesn't make sense that every method throws an exception.*" It can, depending on what they do. "*I wanted to know if the calls are interdependent.*" All calls are independent by definition. – user1803551 Sep 18 '16 at 00:01
  • @user1803551 Thank you – nahomyaja Sep 18 '16 at 00:06
  • If you are using your IDE, you can "step-in" at the line `chain.is().only().as().strong().as().its().weakest().link();` It will step-in one method at a time. If you know it is the thrid one, just step-out of the first two. All this works assuming you are using a debugger and you have the source code. – code4kix Sep 18 '16 at 01:56
  • You should mark your own question as a duplicate. – user1803551 Sep 18 '16 at 06:16

0 Answers0