Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Puzzled over NullPointerException

Reply
Thread Tools

Puzzled over NullPointerException

 
 
ohaya
Guest
Posts: n/a
 
      10-21-2005
Hi,

I originally had some code (for doing an LDAP search using ldapjdk.jar)
that looked something like:

while (results.hasMoreElements())
{
LDAPEntry findEntry = null;
try
{
findEntry = results.next();
LDAPAttribute attribute = findEntry.getAttribute("cn");
Enumeration enumValues = attribute.getStringValues();
String commonName = (String)enumValues.nextElement();
System.out.println("Common name: " + commonName);
}
catch (LDAPReferralException e)
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      10-21-2005
On Fri, 21 Oct 2005 07:52:38 -0400, ohaya <(E-Mail Removed)> wrote or
quoted :

>But, when I did that, I started getting a NullExceptionPointer error, I
>believe on the line where commonName was being assigned.
>
>Can anyone explain why?


see
http://mindprod.com/jgloss/runerrorm...INTEREXCEPTION


Whenever you ask for help about an exception on a newsgroup make sure
you include the following:

The complete error message and complete stack trace.

The source code for at least the lowest level class you wrote
mentioned in the stack trace with the relevant lines in the stack
trace marked in the source listing.


--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      10-21-2005
On Fri, 21 Oct 2005 07:52:38 -0400, ohaya <(E-Mail Removed)> wrote or
quoted :

>findEntry = results.next();
> LDAPAttribute attribute = findEntry.getAttribute("cn");
> Enumeration enumValues = attribute.getStringValues();
> commonName = (String)enumValues.nextElement();
> System.out.println("Common name: " + commonName);


presumably any of this code might fail returning null, and you do
nothing about it:

e.g.
> LDAPAttribute attribute = findEntry.getAttribute("cn");

could be null
> Enumeration enumValues = attribute.getStringValues();

could be null
> commonName = (String)enumValues.nextElement();

could be null

you should code defensively to handle the null case. The line number
in your stack trace will tell you exactly there the problem is.

--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
Reply With Quote
 
ohaya@cox.net
Guest
Posts: n/a
 
      10-21-2005

Roedy Green wrote:
> On Fri, 21 Oct 2005 07:52:38 -0400, ohaya <(E-Mail Removed)> wrote or
> quoted :
>
> >findEntry = results.next();
> > LDAPAttribute attribute = findEntry.getAttribute("cn");
> > Enumeration enumValues = attribute.getStringValues();
> > commonName = (String)enumValues.nextElement();
> > System.out.println("Common name: " + commonName);

>
> presumably any of this code might fail returning null, and you do
> nothing about it:
>
> e.g.
> > LDAPAttribute attribute = findEntry.getAttribute("cn");

> could be null
> > Enumeration enumValues = attribute.getStringValues();

> could be null
> > commonName = (String)enumValues.nextElement();

> could be null
>
> you should code defensively to handle the null case. The line number
> in your stack trace will tell you exactly there the problem is.



Roedy,

I forgot to mention that I had put in a bunch of printlns, to track
down which line was blowing up, and it was the "commonName =
(String)...." line.

If I'm understanding your post, you're implying that the nextElement()
might have returned a null instead of an object, which then caused the
NullPointerException.

I agree with that, but what's really puzzling me is why the
NullPointerException only occurs if I declare commonName inside the
try???

Thanks,
Jim

 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      10-21-2005

"ohaya" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> Hi,
>
> I originally had some code (for doing an LDAP search using ldapjdk.jar)
> that looked something like:
>
> while (results.hasMoreElements())
> {
> LDAPEntry findEntry = null;
> try
> {
> findEntry = results.next();
> LDAPAttribute attribute = findEntry.getAttribute("cn");
> Enumeration enumValues = attribute.getStringValues();
> String commonName = (String)enumValues.nextElement();
> System.out.println("Common name: " + commonName);
> }
> catch (LDAPReferralException e)
> .
> .
>
> The above code seemed to work ok, but then I wanted to have access to
> the commonName outside of the try (for some println's, etc.), so I
> changed it to:
>
> String commonName = "";
> .
> .
> while (results.hasMoreElements())
> {
> LDAPEntry findEntry = null;
> try
> {
> findEntry = results.next();
> LDAPAttribute attribute = findEntry.getAttribute("cn");
> Enumeration enumValues = attribute.getStringValues();
> commonName = (String)enumValues.nextElement();
> System.out.println("Common name: " + commonName);
> }
> catch (LDAPReferralException e)
> .
> .
>
> But, when I did that, I started getting a NullExceptionPointer error, I
> believe on the line where commonName was being assigned.
>
> Can anyone explain why?


I'm not familiar with Java's LDAP API, but I think the answer to your
question depends critically on the code that you did NOT post. Specifically,
on the code inside the catch clause. I recommend you construct and SSCCE and
post it. (http://www.physci.org/codes/sscce.jsp)

Given that you're using the LDAP API, I'm thinking a lot of people won't
actually bother to run your code as they might not have easy access to an
LDAP service. However, if you can replicate the problem in a small snippet
about one page long, then you avoid the risk of leaving out code that might
be important for answering the question.

- Oliver


 
Reply With Quote
 
ohaya
Guest
Posts: n/a
 
      10-21-2005

> I'm not familiar with Java's LDAP API, but I think the answer to your
> question depends critically on the code that you did NOT post. Specifically,
> on the code inside the catch clause. I recommend you construct and SSCCE and
> post it. (http://www.physci.org/codes/sscce.jsp)
>
> Given that you're using the LDAP API, I'm thinking a lot of people won't
> actually bother to run your code as they might not have easy access to an
> LDAP service. However, if you can replicate the problem in a small snippet
> about one page long, then you avoid the risk of leaving out code that might
> be important for answering the question.



Oliver,

I've modified the original code (that had the problem) by now, and,
unfortunately, I didn't keep an exact copy of the code when the
exception was occurring.

I've been trying to undo the changes that I think I've made since then,
but now, I can't seem to make the problem happen ...

I guess I'll have to live with the code that I have now, which is
working...

Thanks,
Jim
 
Reply With Quote
 
Ross Bamford
Guest
Posts: n/a
 
      10-21-2005
On Fri, 21 Oct 2005 19:58:06 +0100, ohaya <(E-Mail Removed)> wrote:

> I've been trying to undo the changes that I think I've made since then,
> but now, I can't seem to make the problem happen ...
>
> I guess I'll have to live with the code that I have now, which is
> working...
>


IMHO that is one of the most dangerous attitudes in software development -
"It just fixed itself so I left it".

(that's not a dig at you, OP, just a general observation).

--
Ross Bamford - http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
ohaya
Guest
Posts: n/a
 
      10-21-2005


Ross Bamford wrote:
>
> On Fri, 21 Oct 2005 19:58:06 +0100, ohaya <(E-Mail Removed)> wrote:
>
> > I've been trying to undo the changes that I think I've made since then,
> > but now, I can't seem to make the problem happen ...
> >
> > I guess I'll have to live with the code that I have now, which is
> > working...
> >

>
> IMHO that is one of the most dangerous attitudes in software development -
> "It just fixed itself so I left it".
>
> (that's not a dig at you, OP, just a general observation).



Ross,

I agree, and I was a little worried that that might be the reaction to
my post.

To be clear, I have working code now, including changes that I put in
specifically to eliminate the problem, and which seem to work, and in
the newer code, I've added additional checks for nulls, etc., as someone
else suggested earlier.

I spent about half a day today so far, trying to get my code back to
where the problem was occurring, to try to reproduce the problem,
because it seemed like a strange problem, but failed in that.

I'm not saying that the problem fixed itself... it's more like I can
make my now-working code replicate the problem.

I guess that if I had not been so anxious to get things working, I
might've taken a snapshot, but I generally only do snapshots when I'm at
a point that I have working code ...

Jim
 
Reply With Quote
 
Ross Bamford
Guest
Posts: n/a
 
      10-21-2005
On Fri, 21 Oct 2005 21:43:23 +0100, ohaya <(E-Mail Removed)> wrote:

>
>
> Ross Bamford wrote:
>>
>> On Fri, 21 Oct 2005 19:58:06 +0100, ohaya <(E-Mail Removed)> wrote:
>>
>> > I've been trying to undo the changes that I think I've made since

>> then,
>> > but now, I can't seem to make the problem happen ...
>> >
>> > I guess I'll have to live with the code that I have now, which is
>> > working...
>> >

>>
>> IMHO that is one of the most dangerous attitudes in software
>> development -
>> "It just fixed itself so I left it".
>>
>> (that's not a dig at you, OP, just a general observation).

>
>
> Ross,
>
> I agree, and I was a little worried that that might be the reaction to
> my post.
>
> [ ... ]
>
> I'm not saying that the problem fixed itself... it's more like I can
> make my now-working code replicate the problem.
>
> I guess that if I had not been so anxious to get things working, I
> might've taken a snapshot, but I generally only do snapshots when I'm at
> a point that I have working code ...
>


And who needs a broken snapshot anyway ...

You don't expect to have to roll back to the problem I guess, so why would
you ? As I say it was just a reactionary observation, brought on by the
memory of a million (or not, probably) tiny bugs that 'went away', only to
return when least expected or desired.

And that's just in stuff I wrote

(Aside: If you use Eclipse, you might find the 'Local History' feature
useful - RMB -> Compare with -> Local History. Other IDEs now have similar
features. I was pleasantly surprised by it recently myself.)

--
Ross Bamford - (E-Mail Removed)
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-21-2005
On 21 Oct 2005 08:46:49 -0700, (E-Mail Removed) wrote or quoted :

>I forgot to mention that I had put in a bunch of printlns, to track
>down which line was blowing up, and it was the "commonName =
>(String)...." line.


that is a very good clue.

String commonName = (String)enumValues.nextElement();

could return null unless you did a enumValues.hasMoreElements() first
that returned true.

Hint: why would code work outside a (try) block but fail inside?

The most common source of that problem is:

Did you redeclare any variables? The variable inside the block is not
the same as the variable outside.

You did not quote enough code to see if that applies.

I need to see your whole class and also the exact code you used
without the try.


--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
VOIP over VPN over TCP over WAP over 3G Theo Markettos UK VOIP 2 02-14-2008 03:27 PM
Puzzled over rgexp IanW Perl Misc 16 02-21-2006 04:26 PM
Very Puzzled, Please Help schiefaw ASP .Net 3 07-22-2005 07:03 PM
VHDL: puzzled beginner Mark Andrew VHDL 5 06-30-2004 04:06 AM
still puzzled Daniel ASP .Net 11 06-09-2004 07:16 AM



Advertisments