File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Ternary Operator

 
Jehan Jaleel
Ranch Hand
Posts: 196
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have been coding in Java for years but never used the Java Ternary Operator that much (always did it the hard way). Now I am playing with it but the following code is not working as I expect it to...

Right now it is throwing an Null Pointer Exception. But I am wondering why, I just want it to return the value from the Map if it finds it or return null if it doesn't.

Thanks in advance for any help.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47232
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to know a lot more details.
Are you sure the Exception is actually thrown inside that method?
What sort of structure is the map: does it behave like HashMap or like Hashtable with respect to nulls?
Which reference is null? Is it the map, or the argument to that method? If the argument is a long, it is hardly likely to be null. Does the map take Longs as its Ks?
 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If line 4 is the line throwing the exception, I think the only thing that could cause the problem is if adviceHeaderMap itself is null. How is this field initialized?
 
Jehan Jaleel
Ranch Hand
Posts: 196
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike,

You are right, the problem is that the HashMap itself was null. I was thinking that I was not using the Ternary operator correctly and trying to cast a null.

Thanks again for your help.
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One piece of advice: don't call methods twice if you can instead use a variable to store the results. Let's say your map was a TreeMap with millions of records. The get could take a bit of time. By calling it twice your method takes twice as long as needed.

So:

And in this case you will immediately see that the ternary operator isn't needed; if header != null you will return header, and otherwise (header == null) you still return the value of header.
 
Winston Gutkowski
Bartender
Pie
Posts: 9472
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:One piece of advice: ...

@Jehan: Another piece of advice: DontWriteLongLines.

I've broken yours up this time; but the only reason I needed to is that your method names - while certainly descriptive - are a case of "descriptive overkill".

Maybe you had no choice in this case, but I'd definitely advise a bit of moderation if you're writing methods in the future.

Winston
 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, but now my earlier comment about "line 4" should be modified to be "lines 3-5".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic