• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Why is the string null?  RSS feed

 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have the following code snippet


Total and Odd are double variables

Output:
String2 = null;

Why is String2 null ?

Bob M
 
Marshal
Posts: 24461
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bob Matthews wrote:Why is String2 null ?



That would be because you never assigned it a value different from null, wouldn't it?

Your posted code assigns it a non-null value if certain conditions are true. So can you figure out whether those conditions are true or not?
 
Bob Matthews
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul

The code falls over in Line 1

Prior to the code String1 = "Help"

Can't see what is wrong with Line 1 ??

Bob M
 
Bob Matthews
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have also tried the following


but still it is not working ??

Bob M
 
Marshal
Posts: 5980
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are Total and Odd classes or primitives?  If they are classes, the above won't work unless the two variables point to the exact same class.
 
Bob Matthews
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Knute

It is line 1 where I have a coding question (problem)

Line 2 works !

I replaced line 1 with integer variables and my code works..............

but I would still like to know how to correctly code the line using string variables

Bob M
 
Knute Snortum
Marshal
Posts: 5980
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Total.equals(Odd) will work as long as the equals() method is overridden for the type of Total and Odd.  If they're both Strings, then you're fine.
 
Bob Matthews
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Knute

you are commenting on Line 2 but that line works OK

I am stuck on Line 1

Bob M
 
Knute Snortum
Marshal
Posts: 5980
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if(String1.equals("Help")) should work.  Or even better, "Help".equals(String1)
 
Marshal
Posts: 64479
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way that is written it can be reduced toNote that putting the String literal on the left obviates any risk of an exception if the str1 reference points to null. That is why Knute called that construct better. If however str1 points to null or any text other than "help", it will return false, and the subexpression to the right of && is never evaluated, and the block is never entered.
I was taught to beware of == and similar on floating point numbers. The tiny imprecisions intailed by floating point arithmetic mean that comparisons are likely to give unexpected results. Try this:-Remembering that this constructor tells you the exact value of 0.1, I challenge you to pedict what that code will print. Integer arithmetic is accurate, and the tiny differences between similar calculations do not exist, so you should always use integers for such comparisons. That is probably why your problems reduced when you stopped using doubles. You should also use descriptive names for your variables; “Odd” is very difficult to understand. And don't start variable names with Capital Letters.
 
Master Rancher
Posts: 4072
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If line 1 is the issue then you would need to show more code from prior to this.
For a NullPointerException we would need to see the code around this to see where objects are assigned.
Simply saying "I assigned XYZ earlier" doesn't help since you clearly missed something, otherwise you wouldn't be getting the NPE.

Also, if you get an exception it is usually a good idea to post the full stack trace.
There might be something in there you missed.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!