• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with a double  RSS feed

 
Ryan Waggoner
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Guys,
I apologize in advance for the vagueness of this message...

I have in my code this...



It was like that for a very long time, months, but not it is not working. It is setting the number of leaves to null.

When I changed it to...


All of the sudden we are groovy. There were no errors occurring, I was just getting a null value.

Thoughts?

Thanks!
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is really strange, because Double.parseDouble(...) returns a double (primitive type), which cannot be null - it is impossible that Double.parseDouble(...) returns a null value.
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is "setNumberOfLeaves"? A double or a Double?
I assume Double because you are complaining about "null".

Are you 100% sure there is no code later on that is setting it to null? The reason I ask is because of what Jesper said.
1) Double.parseDouble cannot return a null as it returns a primitive type
2) Even if it did manage to return a "null" primitive, you'd get a NullPointerException with autoboxing.

I take it you have stepped through the code/used logging to determine that it is that line causing you a problem?

Even if you had used "valueOf", and had passed a null in, then you'd still get a NPE.
 
Ryan Waggoner
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, logging is what brought me here.

I am very confused because, like you said it shouldn't happen, and no there is no other code playing with it.

Did I miss something about doing Double = double?
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Double" is an object.
"double" is a primitive.
"Double = double" or "double = Double" will involve autoboxing, which is why I was asking what type "setNumberOfLeaves" is.

If it was working in the past but now it is not, then logically something has changed to cause it to fail. The obvious answer is a change to either the "ToString()" (is that correct? Shouldn't it be "toString()"?), code following these lines, threading (could another thread be changing the value?) or not using the instance of "tree" that you think you are using (which would be in code external to what we have seen).

Are you really, really 100% sure there is no other code in play? Because if there isn't, the only other possibility is a fault in the JVM (did you change JVMs?). But this kind of behaviour is so critical to many programs (and so easy to unit test) I would be extremely wary of such a conclusion.

Have you tried isolating the line you think doesn't work into a unit test to see what happens?

With only one statement to work on, it's very hard to offer much advice I am afraid.
 
Ryan Waggoner
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You were right when you asked about the setter, I am doing Double = double. And ya, its .toString (darn c#)

The threading issue, could be an issue. This is being done in the seam framework, and it is in a conversation scope. The annoying part is that this used to work every time, now it never works. So I didn't think it would be a problem with conversations / threads.

But it sounds like this is a personal problem I need to overcome huh?
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Waggoner wrote:But it sounds like this is a personal problem I need to overcome huh?

I am sure you can find a support group.

If it is threading, surely your log show that? Well, they should if you are using log4j and have it output the thread id.
[tt]00:00:01 Thread-1 setNumberOfLeaves set to 2.
Thread-2 Clearing setNumberOfLeaves
Thread-1 ERROR! setNumberOfLeaves is null[tt]

If this were my problem, I would create a unit test (probably using JUnit) and hit the problem code with a single thread.
If that worked, then I'd hit it with multiple threads.
I'd then carry on increasing the complexity of the unit test until I got something that approximates Seams and shows the problem.
But do it within reason - you might be better off increasing logging and hitting the application with controlled inputs to see what happens.
If you can, running the application in debug with a breakpoint on that line could also help.

You might find you need to refactor some code to be able to run it within unit tests; but that may not be a big problem. During the refactoring you may have a sudden realisationas to what the underlying problem is. And if it's threading...my guess is that proper getters/setters and correct synchronization will sort it.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also note that the Java method is toString(), not ToString(). What that tells me is the the lines you're showing us are not cut-and-pasted from actual code, but rather typed as hypothetical examples. Therefore it's entirely possible that there's some subtle issue that you don't know about, and therefore have not reproduced in your examples.
 
Ryan Waggoner
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good point Ernest.

Maybe this will help...

This is what works now...


This is what used to work...

 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But be careful about using literal numbers for that substring method. It only takes one character out of place for the application to crash with a NumberFormatException . . . if you're lucky.
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK - that is totally different code to what you first posted.

What value is "setSiAmount" receiving? What type is its parameter? I doubt it is being passed null and that the fault lies in the method.

Can you show us the code for "setSiAmount"?

Personally I suspect threading and it's just luck that means the new code is working (and you logs should show evidence of that).
Like I said at the start - 2 lines is not a lot to go on.
 
Ryan Waggoner
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Jason,
I just figured that it seems to be a simple process, so not a lot of code would be needed. But here is my setter...



Here is a little more code... if it helps to have more of a context...


I am working on the unit tests now.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!