The following code makes me mad:
private String blahBlah(){
return null;
}
@Test
public void myTest(){
System.out.println(blahBlah()); //Good, output "null"
Object obj = blahBlah();
System.out.println(obj.toString()) //Good as above
//System.out.println(blahBlah().toString()); //Bad, NullPointerException
//System.out.println(((Object)blahBlah()).toString()); //Bad as above
}
Can anyone explain the above behavior?
UPDATE:
The above code is NOT the truth. What I actually experienced is that I received NullPointerException and I track back to the call of toString(), and I tried different workarounds including in-statement casting but it does'nt work. But after I use seperated cast I accidentally removed the toString() call so it WORKED.
myTestdoesn't throw an exception?