Last active
March 13, 2017 19:02
-
-
Save enreeco/464e0c04db7d88ab188e5bfa1022e9ce to your computer and use it in GitHub Desktop.
Apex class behavior with Null instance of Sobject
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
MyController cnt = new MyController(); | |
cnt.tst.Name = 'test'; | |
cnt.tst = null; | |
system.debug('Test outside: '+(cnt.tst.Name == null)); //Test outside: true | |
system.debug('Test outside: '+(cnt.tst == null)); //Test outside: true | |
system.debug('Test outside: '+json.serializepretty(cnt.tst)); //Test outside: null | |
cnt.myMethod(); //Throws null pointer exceptions | |
*/ | |
public class MyController { | |
public Account tst{get;set;} | |
public MyController(){ | |
this.tst = [Select Id, Name From Account limit 1]; | |
} | |
public void myMethod(){ | |
system.debug('Test inside: '+(tst.Name == null)); //Throws null pointer exceptions | |
system.debug('Test inside: '+(tst == null)); | |
system.debug('Test inside: '+json.serializepretty(tst)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@logontokartik in this context
this.tst
andtst
refers to the same inner member, there are no other "tst" variables, how did you explain this regarding the Apex engine?