This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
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?
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.
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.