File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cannot find NullPointerException

 
Pavel Kubal
Ranch Hand
Posts: 360
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, my application prints few NullPointerExceptions on console, but a don't know why it does not print path

instead of :


it prints only

java.lang.NullPointerException

it is very large application, so that it is impossible to put all methods in try...catch clause.
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could it be that those exceptions are caught and then simply printed to System.out, in stead of printStackTrace being invoked?

it is very large application, so that it is impossible to put all methods in try...catch clause.


So there are exceptions that are just not handled? That's not a good idea.
NullPointerException in particular can be prevented by checking whether an object is null before invoking methods on it. That's not very practical sometimes, but better than having to handle exceptions that you don't where they're coming from.
 
Pavel Kubal
Ranch Hand
Posts: 360
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ulf Dittmer:
Could it be that those exceptions are caught and then simply printed to System.out, in stead of printStackTrace being invoked?


No, I never do that.

Originally posted by Ulf Dittmer:

So there are exceptions that are just not handled? That's not a good idea.
NullPointerException in particular can be prevented by checking whether an object is null before invoking methods on it. That's not very practical sometimes, but better than having to handle exceptions that you don't where they're coming from.


Well, I'm not perfect human. But question was not how to check null reference, but how to locate NullPointerException.
 
Norm Radder
Ranch Hand
Posts: 707
3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a way to get that message:
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Ulf]: Could it be that those exceptions are caught and then simply printed to System.out, in stead of printStackTrace being invoked?

[Pavel]: No, I never do that.


Good. However the symptom you describe looks very much like that's what happened. I suspect the two main possibilities are (a) you invoked toString() unintentionally, or (b) you called someone else's code which did it. Something as simple as System.out.printn(ex) would cause the problem you describe.

Now if you've got too much code and/or too many catch blocks to find this easily, I recommend you add logging to your application. (If you've already got some logging, be sure the logging level is configured for maximum.) Start with logging the beginning and end of the highest-level methods in your application. You should be able to see output something (vaguely) like

starting methodA()
finished methodA()
starting methodB()
starting methodC()
finished methodC()
java.lang.NullPointerException

From this you can tell that the error occurred sometime during methodCB), after a call was made (and completed) to methodC(). So now you can add more logging to that part of methodB(), or to the beginning and end of other methods that were called from within method. Alternately you may prefer to use a debugger to step through the code. I don't know how much you know about logging or using debuggers; both could be very useful here, and are worth learning how to use well. Personally, I've usually gotten more use out of good logging than from a debugger - but others may have different experience.
[ September 10, 2005: Message edited by: Jim Yingst ]
 
Tony Morris
Ranch Hand
Posts: 1608
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://www.jtiger.org/articles/npe.html

You'll need the stack trace.
Granted, many poorly written applications (at least that I encounter) obscure this information, in which case, you'll need to fire up a debugger.
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Ulf]: Could it be that those exceptions are caught and then simply printed to System.out, in stead of printStackTrace being invoked?

[Pavel]: No, I never do that.[/b]

So what DO you do? Do you catch any exceptions in your application at the moment? How do you handle them? Please post some examples of the try...catch statements that you do have at the moment.

Layne
 
Pavel Kubal
Ranch Hand
Posts: 360
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I think you didn't understand. My application runs over 10 threads at the same moment, so logging is difficult.

I don't know where this excepton is thrown because it does not print class and package, like this



so that I can't add proper code in try...catch clause.

This application was developed by 10 programmers. And logging is not properly done.

Maybe is better to ask why it doesn't print place of occurence "at xxxxxx.migration.test.Test.main(Test.java:19)".

Tony : thank you, I will have a look at it

Debugging this kind of error is very very difficult. This error occures after few hours, so it would take days to find this error by debugging.
 
Jayesh Lalwani
Ranch Hand
Posts: 502
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pavel Kubal:


This application was developed by 10 programmers. And logging is not properly done.

Maybe is better to ask why it doesn't print place of occurence "at xxxxxx.migration.test.Test.main(Test.java:19)".


As it has been mentioned before, the reason why it doesn't print the stack trace is because someone must have put in System.out.println(ex) or logger.debug(ex) instead of ex.printStackTrace() or logger.error(ex)

Now, you cannot know why the NullPointerException is thrown unless you have the full stack trace.

There are several ways to solve this
1) Go through all of your code to find the place that is creating the improper log. You can search your code for println to narrow down the code to look at
2) Add additional logging in the code to figure out where exactly the code is breaking. Even if you have a multi-threaded application, and all your logs get mixed up so you can't make sense out of it, there are ways around it. You can turn on logging in each thread one at a time and reproduce the error. Even if you have 10 threads, you have to run your test a maximum of 10 times.
3) Debug each thread individually. You might have to debug your application 10 times.

Since, your application is large, doesn't have proper error handling and logging and this error is hard to reproduce, it will take time to figure out this problem. You might want to try 2) and 1) simultaneously. I would turn on logging in one thread, let the application run, and inspect my code while the application is running. If your log gives you the stack trace, then it's good, otherwise switch logging to next thread, and continue on your code inspection.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pavel Kubal:

Maybe is better to ask why it doesn't print place of occurence "at xxxxxx.migration.test.Test.main(Test.java:19)".


I think Jim, and several other people, have already explained this rather clearly: because some code, somewhere (in your app, or a library) is catching the NPE and printing the object, rather than calling printStackTrace() on it.

Since we're talking about a debugging exercise, one clever hack that may help you find this would be to replace the standard NullPointerException class with a replacement that calls printStackTrace() in its toString() method. If you call printStackTrace() and collect the result in a StringWriter, you could return the result from toString(); then you'd see that message replaced by the full stack trace. The JVM has a "-bootclasspath" switch which would let you put your own NullPointerException class in front of the system default one.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From what you describe, I think that it would also be helpful if your logged messages include the name of the thread you're logging from. Make sure your logging framework takes care of this for you (so you don't have to call Thread.currentThread().getName() every time you log something). I know log4j will do this for you. And then later you can use something like Chainsaw to view the logs and filter or sort by thread id or other attributes. In this manner you can go ahead and run all 10 threads as you normally do, and figure out what each one is doing.
 
Jayesh Lalwani
Ranch Hand
Posts: 502
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:


I think Jim, and several other people, have already explained this rather clearly: because some code, somewhere (in your app, or a library) is catching the NPE and printing the object, rather than calling printStackTrace() on it.

Since we're talking about a debugging exercise, one clever hack that may help you find this would be to replace the standard NullPointerException class with a replacement that calls printStackTrace() in its toString() method. If you call printStackTrace() and collect the result in a StringWriter, you could return the result from toString(); then you'd see that message replaced by the full stack trace. The JVM has a "-bootclasspath" switch which would let you put your own NullPointerException class in front of the system default one.



Damn good idea!!! I learn something new over here everyday!!! I would have saved so much of my time if I had figured this out myself
 
Pavel Kubal
Ranch Hand
Posts: 360
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everybody suggests logging. I know, but logging is not properly implemented in this application and there is still question which has nobody answered.

WHY it prints only



instead of place of occurence.



Do you know about some piece of code which prints this??

If nobody knows answer i will concentrate on logging. But it will take a month to rewrite logging in this application.

Thanks for all your replies!
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pavel,

We've told you many times!



prints

java.lang.NullPointerException

whereas



prints the class and method names and line numbers (the "stack trace.")
[ September 12, 2005: Message edited by: Ernest Friedman-Hill ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just about everybody has been answering that question. Try running this:

I know you said you don't do this (intentionally at least). But it is still extremely probable, in my opinion, that someone on your project has done this, somewhere. It may also be in some 3rd-party jar file you've included in the project.

EFH's suggestion is probably the best way to quickly locate this particular bug. Improving logging will help a great deal in general when problems emerge.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Pavel]: but logging is not properly implemented in this application

Well, I think it's time to start then. You don't have to put detailed logging in the entire application at once. But introduce a logging framework (such as log4j) into the progject, and start putting basic logging calls into the high-level methods. As you discover problem areas in your code, you can put more logging into the specific areas that are giving you problems.
 
Ken Blair
Ranch Hand
Posts: 1078
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe if one more person explains to him that System.out.println(exception) causes what he's talking about he'll figure it out!

So without further ado:



This WILL NOT PRINT A STACK TRACE. As has been said a dozen times, look for something like that. Your response to the original person who suggested this was "No I never do that." only to then say later that 10 different programmers wrote this and comment on how it's not done properly. Well maybe one of the OTHER nine programmers DID that (since you never do), in which case this is why it's not printing a trace.

Yeesh.
 
Pavel Kubal
Ranch Hand
Posts: 360
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This WILL NOT PRINT A STACK TRACE. As has been said a dozen times, look for something like that. Your response to the original person who suggested this was "No I never do that." only to then say later that 10 different programmers wrote this and comment on how it's not done properly. Well maybe one of the OTHER nine programmers DID that (since you never do), in which case this is why it's not printing a trace.

Yeesh.[/qb]<hr></blockquote>

I searched all the code and there's no catch block with NullPointerException.

I see that the only solution is logging

EDIT :

I searched through axis sources and found this (see the catch block) :



Great :roll: , maybe is problem with axis libraries.
[ September 13, 2005: Message edited by: Pavel Kubal ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pavel Kubal:

I searched all the code and there's no catch block with NullPointerException.


It doesn't have to be a catch block for NullPointerException. It could be a catch for any superclass of NullPointerException -- RuntimeException, Exception, or even Throwable.
 
Norm Radder
Ranch Hand
Posts: 707
3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great idea. I had to give it a try.
Sample output at the bottom.
 
Struan Kerr
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pavel,

I'm just curious why you seem so dead set against EFH's suggestion of just hacking a custom Null-pointer exception class? If it turns out that you're correct and its not just the exception being printed out then you've wasted maybe 10 minutes, but if it turns out that everybody else in this thread is right then you'll have have saved a lot of time not having to put logging in the app at this stage, though it may be an idea to try and get it in when an opportunity arises.

Struan
 
Ken Blair
Ranch Hand
Posts: 1078
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I searched all the code and there's no catch block with NullPointerException.


As Ernest mentioned, this doesn't exclude what we're talking about. You'd have to search for Throwable, Exception and RuntimeException as well. Furthermore, even if you don't find it that simply means you didn't find it, not that it doesn't exist. If the code written by ten different people perhaps calls something you don't have source for it could be in there. The best solution to finding this problem immediately is making your own NullPointerException as people have already mentioned.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic