Java Forum / General / November 2007
println inside catch throwing Nullpointerexception?
ohaya@cox.net - 12 Nov 2007 18:47 GMT Hi,
We are running a problem with some existing code that was compiled under JDK 1.42 under 1.50_05, where it looks like the println inside of a catch is throwing a Nullpointerexception, i.e., we have something like:
try { . . . } catch (Exception e) { println("The error was: " + e); }
As a test, we added a try inside the catch, surrounding the println, where we just printed out a fixed msg (i.e., not using the Exception:
try { . . . } catch (Exception e) { try { println("The error was: " + e); } catch (Exception x){ println("An exception was thrown inside the catch"); } }
And we did see that "An exception was thrown inside the catch" output.
This problem doesn't occur when we run this same code under an older JRE, e.g., 1.42.
Has anyone run across this problem before? Is there some known behavior change with the Exception class or with println that would cause println to throw an error like that?
Thanks, jim
Dundonald - 12 Nov 2007 18:49 GMT On Nov 12, 6:47 pm, oh...@cox.net wrote:
> Hi, > [quoted text clipped - 34 lines] > behavior change with the Exception class or with println that would > cause println to throw an error like that? First of all it's not idea to be banging an exception object in to a String output stream.
Why don't you try something like
println("The error was: " + e.toString());
Lew - 12 Nov 2007 18:57 GMT > First of all it's not idea to be banging an exception object in to a > String output stream. > > Why don't you try something like > > println("The error was: " + e.toString()); This is semantically identical to println("The error was: " + e );
No difference at all, since String concatenation is defined to invoke the toString() method of a (non-null) non-String argument. <http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#39990>
More useful would be to deliver the e.getMessage() and to identify the type of e itself.
 Signature Lew
Martin Gregorie - 12 Nov 2007 22:43 GMT > On Nov 12, 6:47 pm, oh...@cox.net wrote: >> Hi, [quoted text clipped - 42 lines] > > println("The error was: " + e.toString()); I normally use println("The error is: " + e.getMessage()); except when the String returned by getMessage() is empty or meaningless, which is the case in a minority of cases. The ClassNotFoundException thrown by Class.forName() is one where getMessage() returns an empty String and you end up building your own error message.
 Signature martin@ | Martin Gregorie gregorie. | Essex, UK org |
Lew - 12 Nov 2007 18:53 GMT > Hi, > [quoted text clipped - 34 lines] > behavior change with the Exception class or with println that would > cause println to throw an error like that? There is no change in Exception or in try...catch, so the problem must be elsewhere.
I note that you have not captured any information about the Exception that would help diagnose it. Why is that?
What is the type of the Exception? What is its message (as in e.getMessage())?
 Signature Lew Three questions were asked in this post, seeking answers.
Lew - 12 Nov 2007 19:08 GMT ohaya@cox.net wrote:
>> println("The error was: " + e); Incidentally, what println() method is this? You don't show us enough of the class to see how println() is defined. Undoubtedly the error is in the implementation of the println() method.
Inevitably, the problem is in the part of the code that you don't show, unless you provide an SSCCE: <http://www.physci.org/codes/sscce.html>
Please provide an SSCCE.
 Signature Lew This post contains a request for an SSCCE to make it possible to answer the OP's question.
ohaya@cox.net - 12 Nov 2007 19:57 GMT > oh...@cox.net wrote: > >> println("The error was: " + e); [quoted text clipped - 13 lines] > This post contains a request for an SSCCE to make it possible to answer the > OP's question. Hi,
Sorry, I am trying to help out a colleague with this problem. Here's a (hopefully) more complete snippet which I got:
try { // outer try System.out.println("Connecting to [" + dsType + "] server: [" + dsHost + ":" + ("" + dsPort) + "]"); ld.connect(dsHost, dsPort);
LDAPSearchResults results = ld.search(baseDN, searchScope, searchFilter, getAttributes, false);
if (!results.hasMoreElements()) { return ""; }
while (results.hasMoreElements()) // loop, looking for all returned entities { LDAPEntry findEntry = null; try { // inner try findEntry = results.next(); // get next entity returned by LDAP server LDAPAttribute attribute = findEntry.getAttribute(dsUserAtr); Enumeration enumValues = attribute.getStringValues(); // put attribute values into an Enumeration if (!enumValues.hasMoreElements()) { System.out.println("** ERROR **: enumValues.hasMoreElements() is false ==> found match to certificate Subject and found desired attribute, but attribute had no values..."); System.out.println("** ERROR **: -> So, will now return indication that authentication failed..."); return ""; } returnString = (String)enumValues.nextElement(); // move 1st attribute value into returnString numMatches++; // increment count of matches for this search System.out.println("found match, returned string #(" + numMatches + ") = [" + returnString + "]"); } // end inner try catch (LDAPReferralException e) { System.out.println("LDAP search failed ..."); System.out.println("LDAPReferralException = [" + e + "]"); <<--We think Nullpointerexception being thrown here... return ""; } } // end while System.out.println("LDAP search found [" + numMatches + "] matches"); System.out.println("Disconnecting from LDAP server"); ld.disconnect(); } // end outer try catch (LDAPException e) { System.out.println("** ERROR **: Failed ..."); System.out.println("** ERROR **: -> LDAPException = [" + e + "]"); return ""; }
Lew - 12 Nov 2007 20:02 GMT Lew wrote:
>> you provide an SSCCE: >> <http://www.physci.org/codes/sscce.html> [quoted text clipped - 5 lines] >> This post contains a request for an SSCCE to make it possible to answer the >> OP's question.
> Sorry, I am trying to help out a colleague with this problem. Here's > a (hopefully) more complete snippet which I got: Not an SSCCE. Read the link.
Please do not embed TAB characters in Usenet listing.
Please provide an SSCCE.
 Signature Lew This post contains a request for an SSCCE to make it possible to answer the OP's question.
Lew - 12 Nov 2007 20:13 GMT > catch (LDAPReferralException e) > { > System.out.println("LDAP search failed ..."); > System.out.println("LDAPReferralException = [" + e + "]"); <<--We > think Nullpointerexception being thrown here... Unlikely. But we won't be able to tell until you answer the three questions I asked earlier about the exception, along with at least the first few lines of the stack trace.
You're aware that the exception message tells what's wrong and the stack trace tells where, right?
 Signature Lew
ohaya@cox.net - 12 Nov 2007 22:49 GMT > oh...@cox.net wrote: > > catch (LDAPReferralException e) [quoted text clipped - 12 lines] > -- > Lew Lew,
The reason that we think that that is where the problem is occurring is that we see the output line:
LDAP search failed ...
But we don't see an output line like:
LDAPReferralException = [xxxxxxxxxxxxxxxx]
after that...
Jim
Lew - 13 Nov 2007 00:15 GMT oh...@cox.net wrote:
>>> catch (LDAPReferralException e) >>> { >>> System.out.println("LDAP search failed ..."); >>> System.out.println("LDAPReferralException = [" + e + "]"); <<--We >>> think Nullpointerexception being thrown here... Lew wrote:
>> Unlikely. But we won't be able to tell until you answer the three questions I >> asked earlier about the exception, along with at least the first few lines of >> the stack trace. >> >> You're aware that the exception message tells what's wrong and the stack trace >> tells where, right?
> The reason that we think that that is where the problem is occurring > is that we see the output line: [quoted text clipped - 6 lines] > > after that... How does this indicate a NullPointerException?
> We're not getting a stack trace :( , otherwise I'd have posted some of > it (we can't get files directly off of this system). Huh? Don't you control the code that catches the exception? Have it print a stack trace as part of its message, along with the e.getMessage() output.
If you don't answer our questions, we aren't able to answer yours.
Good luck.
 Signature Lew
Andreas Leitgeb - 12 Nov 2007 20:25 GMT >> Inevitably, the problem is in the part of the code that you don't show, unless >> you provide an SSCCE: [quoted text clipped - 6 lines] > return ""; > } You could find out more about it if you had a closer look at the stack-dump that comes with the nullpointerexception. It should tell you, where the exception really happened.
It's quite impossible, that e itself was null, but theoretically, the exception's .toString() implementation could also be the culprit (e.g. trying to use some internal null-value)
Is the LDAPReferralException from some library or your own creation? If the latter, did you overwrite its .toString() and did you perhaps unconditionally use the rootCause-element inside it?
ohaya@cox.net - 12 Nov 2007 22:43 GMT On Nov 12, 12:25 pm, Andreas Leitgeb <a...@gamma.logic.tuwien.ac.at> wrote:
> oh...@cox.net <oh...@cox.net> wrote: > >> Inevitably, the problem is in the part of the code that you don't show, unless [quoted text clipped - 19 lines] > If the latter, did you overwrite its .toString() and did you perhaps > unconditionally use the rootCause-element inside it? Andreas (and Lew),
We're not getting a stack trace :(, otherwise I'd have posted some of it (we can't get files directly off of this system).
Also, FYI, the LDAPReferralException is from the LDAP JDK (ldapjdk.jar), and we compared the ldapjdk.jar files from the system where this is working vs. this system, and they're byte-for-byte the same.
Jim
RedGrittyBrick - 13 Nov 2007 10:43 GMT > On Nov 12, 12:25 pm, Andreas Leitgeb <a...@gamma.logic.tuwien.ac.at> > wrote: [quoted text clipped - 32 lines] > where this is working vs. this system, and they're byte-for-byte the > same. After catch (LDAPReferralException e) { Can you not insert e.printStackTrace(); System.out.println(e.getMessage()); ?
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|