4

I am trying to create a test class, and I'm using this function to generate data. It creates the account properly, and my system debug shows that I am successfully querying and returning values with toRef. BUt then when I try to assign the returned id to nd1.Account__c it tosses the title error.

As far as I can tell, nothing should be wrong with the code. Can anyone explain why it's not working? Thanks!

public static void genData()
{
    System.debug('Generating Data...::');
    Account ac = new Account();
    VS_Design__c nd1, nd2 = new VS_Design__c();

ac.Name = 'Test Account';
    insert ac;
    System.debug('Account Inserted.. ::');

    Account toRef = [SELECT Id, Name 
                    FROM Account 
                    WHERE Name = 'Test Account' 
                    LIMIT 1];
                    System.debug('toRef Results :: ' + toRef.Id + ' ' + toRef.Name); //this returns the ID and Name properly

    //Design Insertion 1
    nd1.Account__c= toRef.Id; \\error happens here
    nd1.Status__c = 'Closed';
    nd1.Approved_By__c = toRef.Id;
    nd1.Approved_On__c = Date.today();
    insert nd1;

    //Design Insertion 2
    nd2.Account__c = toRef.Id;
    nd2.Status__c = 'New';
    insert nd2;

    //Update Design 2 to run Trigger, then run test
    nd2.Status__c = 'Closed';
    nd2.Approved_By__c = toRef.Id;
    nd2.Approved_On__c = Date.today();
    update nd2;

}
6
  • 1
    VS_Design__c nd1, nd2 = new VS_Design__c(); Seems this line causing an issue. You need to separately initialize the object here.
    – Devendra
    Aug 8, 2016 at 17:49
  • 2
    You shouldn't need to re-query it back it, why are you? ac.ID should be fine
    – EricSSH
    Aug 8, 2016 at 17:50
  • I have executed those lines in my org and got the following result: Account nd1, nd2 = new Account(); System.debug('nd1##'+nd1); System.debug('nd2##'+nd2); nd1##null nd2##Account:{}. In this case, nd1.Account__c will throw an error being it is null.
    – Devendra
    Aug 8, 2016 at 17:53
  • @Devendra: Just debugging the null variable won't throw error. Try debugging nd1.name or assign nd1.name Aug 8, 2016 at 17:55
  • @EricSSH I figured it was because I am inserting a record that doesn't have an existing ID, until it has been successfully inserted. At that point, it has been created, and now has an ID to return. Would it work otherwise to just use ac.ID?
    – Genko
    Aug 8, 2016 at 19:10

2 Answers 2

9

The line

VS_Design__c nd1, nd2 = new VS_Design__c();

does not initialize nd1

From the Apex doc

All variables are initialized to null if they aren’t assigned a value. For instance, in the following example, i, and k are assigned values, while the integer variable j and the boolean variable b are set to null because they aren’t explicitly initialized.

Integer i = 0, j, k = 1;

Boolean b;

1
  • 1
    Thanks for the explanation! It makes sense now after I thought about how I tried to do things, and then looked at your example and the documentation.
    – Genko
    Aug 8, 2016 at 19:20
6

The problem is with with initialization

VS_Design__c nd1, nd2 = new VS_Design__c();

It declares nd1 as VS_Design__c but doesn't initialize it. The initialization happens only for nd2 as it is on the left hand side of the = of the initialization.

Just ran this code snippet in Execute anonymous window and it throw error on line 3

Account nd1, nd2 = new Account();
nd2.name = 'test';
nd1.name = 'test again';

If you separate them in two lines it will work.

VS_Design__c nd1 = new VS_Design__c();
VS_Design__c nd2 = new VS_Design__c();

Hope it helps.

5
  • great minds think alike
    – cropredy
    Aug 8, 2016 at 17:54
  • @cropredy You took time to search for document and i spent running code snippet :D Aug 8, 2016 at 17:56
  • 1
    Thanks for this. I know you and @cropredy provided roughly the same information, but I chose his as the answer since it also links out to the official docs. I really appreciate the explanation! I'm passed the error and am getting things working now.
    – Genko
    Aug 8, 2016 at 19:21
  • @Genko: Glad our answers helped you to resolve your issue. Happy to help. Aug 8, 2016 at 19:23
  • 1
    You can still combine them with the comma operator: VS_Design__c nd1 = new VS_Design__c(), nd2 = new VS_Design__c(); This is marginally more efficient when you have a large number of variables with the same data type.
    – sfdcfox
    Aug 9, 2016 at 5:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .