unbelievable NullPointerException

P

Pif

Hello, I've encountered a NullPointerException that I've not successed
to explain.

I've a NullPointerException if the following final line of my stack trace :

Service service = new Service().getObject(int id);

For me this can only occur if the left part of the method call is null,
but new Service() cannot be null...

The exception does not occur in the body of the getObject method.

Environment is : JDK 1.6.0, SpringFramework.

Does somebody have an idea ?

Thanks a lot for your help.


Regards.
 
M

markspace

Pif said:
Hello, I've encountered a NullPointerException that I've not successed
to explain.

I've a NullPointerException if the following final line of my stack trace :

Service service = new Service().getObject(int id);

For me this can only occur if the left part of the method call is null,
but new Service() cannot be null...


class Service {
public Service {
throw new NullPointerException();
}
}


The constructor itself could be throwing an error...
 
P

Pif

I didn't say that I've written the Service class :

public class CenterService {

public CenterDAO centerDao = null;
public CenterService(){
centerDao = new CenterDAO();
}

...
}

1) so this class does not extend any other
2) if the body of the constructor would throw this exception, in debug
mode I would be able to "step into" the constructor... but I can't..

markspace a écrit :
 
A

Arne Vajhøj

Eric said:
Of course you can't: The calling code invokes `new Service()',
so the CenterService constructor you've shown here isn't used at all.

I think it is a rather general rule for troubleshooting: if the problem
is in code X then look at code X not code Y.

:)

Arne
 
A

Arne Vajhøj

Patricia said:
Arne Vajhøj wrote:
...
...

I don't totally agree with this rule.

If you think the problem is in code X, but does not make sense, it may
be time to ask whether it is really somewhere else.

Often, problems that are sufficiently intractable to get posted here are
really a case of fixating on one interpretation, and not considering
alternatives.

True.

believe the problem is in code X != the problem actually is in code X

And when people post their problem somewhere it is very frequently
the assumptions that are the problem and not the analysis of same.

Arne
 
R

Roedy Green

Service service = new Service().getObject(int id);

split it up into two lines.

And put in some explicit asserts to check for null. You may be
misreading the stack trace or looking at obsolete code that does not
match up with your current source.

Try a clean compile.
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Out of 135 criminals, including robbers and rapists, 118 admitted that when they were children they burned, hanged and stabbed domestic animals."
~ Ogonyok Magazine 1979.
 
Q

Qu0ll

Roedy Green said:
split it up into two lines.

And put in some explicit asserts to check for null. You may be
misreading the stack trace or looking at obsolete code that does not
match up with your current source.

Try a clean compile.

Excellent advice Roedy!

--
And loving it,

-Qu0ll (Rare, not extinct)
_________________________________________________
(e-mail address removed)
[Replace the "SixFour" with numbers to email me]
 
P

Pif

hi again, so here is a more details description of my problem:



Here is the code of my class :

public class CenterService {

public CenterDAO centerDao = null;
public CenterService(){
centerDao = new CenterDAO();
}

public List<Center> getCenters() throws DAOException{
return centerDao.getCenters();
}

public Center getCenter(int centerId) throws DAOException{
for (Center center: getCenters()){
if(center.id.intValue()==centerId) return center;
}
return null;
}
}

When I do following code :


CenterService service = new CenterService();
Center center = service.getCenter(5); --> Null pointer exception is
thrown at this line (not in line of body of the getCenter(int) method

If I use following code, the result is the same:
Center center = new CenterService().getCenter(5); --> same
NullPointException.

I've also tried to use class variable with same result.

More over, in fact, in the code, I create a conference object, and when
I want to fill it, the gets the center from DB. The I call the method
that contains this code the first time, this works well. When I call it
the second time with from the same instance of conference, this throws
the exception. If the second Time I use a new conference to fill it,
this does not throw the NullPointerException.

We have rebuid build the code on a production-like workstation used by
tester, the problem also appears. The problem appears when I launch
JUnit test to create my conferences that call the getCEnter() method,
or also in the global code of the server.

THanks.
 
P

Pif

Thanks a lot for you help... so I've found the explanation.


Integer centerId = conf.getCenterId();
new CenterService().getCenter(centerId);

This throws an exception that does not appear in the debug mod because
this is internally managed by the JDK. Exactly, the exception is
thrown by the Autoboxing : NullPointerException is throws when trying
to convert null to int !!!!


Thank you !
 
T

Tom Anderson

Thanks a lot for you help... so I've found the explanation.

Integer centerId = conf.getCenterId();
new CenterService().getCenter(centerId);

This throws an exception that does not appear in the debug mod because
this is internally managed by the JDK. Exactly, the exception is thrown
by the Autoboxing : NullPointerException is throws when trying to
convert null to int !!!!

This is exactly what i was going to suggest - glad you've found it. The
NPE from unboxing is a potent source of 'impossible' exceptions.

I would point out that *none* of the code you posted before this would
actually have allowed us to diagnose this problem. First, you posted:

Service service = new Service().getObject(int id);

Which isn't even syntactically correct - that 'int' is out of place. Then
it was:

public class CenterService {

etc, which was the first we'd heard of the CenterService class. And then:

Center center = service.getCenter(5);

In which you used a literal 5, not a variable of type Integer, thus
eliminating the key quirk through which your problem was occurring.

How on earth do you expect to get help with bugs in your code if you don't
show us the code? It's no use telling us what you *think* your code says -
if you were right, you wouldn't have a bug, would you?

tom
 
R

Roedy Green

CenterService service = new CenterService();
Center center = service.getCenter(5); --> Null pointer exception is
thrown at this line (not in line of body of the getCenter(int) method

So it looks like you think service is the null pointer. You could
verify that with some asserts as I wrote earlier. It is important to
get solid proof for any assumptions, especially ones you are REALLY
sure of. Otherwise you can chase your tail for hours when the problem
is not where you are looking. Nearly always when the computer is doing
something "impossible" the problem is not where you are looking or one
of your assumptions is false.

Using an IDE with debug trace might solve this quickly. Just insert a
breakpoint just ahead of this code and single step it watching for
anything weird.


see http://mindprod.com/jgloss/trace.html
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Out of 135 criminals, including robbers and rapists, 118 admitted that when they were children they burned, hanged and stabbed domestic animals."
~ Ogonyok Magazine 1979.
 
R

Roedy Green

This throws an exception that does not appear in the debug mod because
this is internally managed by the JDK. Exactly, the exception is
thrown by the Autoboxing : NullPointerException is throws when trying
to convert null to int !!!!
None of us saw that because we did not have the complete program,
which would perhaps have let someone check some method signatures.

See http://mindprod.com/jgloss/sscce.html


With a complete little program, we can each pop it into our favourite
IDE, and use all manner of tools to track problems. Without that, we
have just our eyeballs, and past experience with similar strange
behaviour.

--
Roedy Green Canadian Mind Products
http://mindprod.com

"Out of 135 criminals, including robbers and rapists, 118 admitted that when they were children they burned, hanged and stabbed domestic animals."
~ Ogonyok Magazine 1979.
 
R

Roedy Green

M

Mike Schilling

Peter said:
Of course, someone who takes the time to construct a SSCCE may often
find that the exercise requires them to examine the problem well
enough to figure it out themselves.

Very true. When someone comes to my office to talk over a problem, I
try to get them to describe the situation in detail, and often that's
enough for them to see the solution.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,829
Messages
2,569,737
Members
45,525
Latest member
RosalindSa

Latest Threads

Top