I have already asked one of the contradiction question here Why is this not throwing a NullPointerException?
But this is one of different type and behavior I want to know about, please look for my example below
package com;
public class test {
public static void main(String[] args) {
Abc abc = null;
//Scenario 1
System.out.println("ERROR HERE " + abc!=null?abc.getS1():""); //This is throwing null pointer exception
//Scenario 2
String s1 = abc!=null?abc.getS1():"";
System.out.println("This is fine " + s1);
}
}
class Abc {
String s1;
public String getS1() {
return s1;
}
public void setS1(String s1) {
this.s1 = s1;
}
}
So here Scenario 2 will work fine but why it is not working when I am trying it with other string concatenation in Scenario 1?