function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Vishnu NVishnu N 

Hi I'm Getting an error while trying to Execute an array in Anonymous Window

Apex class:

public class Arrays_Example {
    
    public void Example1(){
        Account[] accounts = new Account[4];
        Account a1 = new Account();
        a1.Name = 'TCS';
        a1.Phone = '1111';
        a1.Rating = 'Hot';
        accounts[0]=a1;
        Account a2 = new Account();
        a2.Name = 'Dell';
        a2.Phone = '2222';
        a2.Rating = 'Warm';
        accounts[1]=a2;
        Account a3 = new Account();
        a3.Name='CapG';
        a3.Phone='3333';
        a3.Rating='Cold';
        accounts[3]=a3;
        for (Integer i = 0; i<accounts.size();i++){
            
            System.debug('Name: '+ accounts[i].Name);
            System.debug('Phone: '+accounts[i].Phone);
            System.debug('Rating: '+ accounts[i].Rating);
        }
    }

}

And Trying to executute with the statement.

Arrays_Example acc = new Arrays_Example();
acc.Example1();

But getting error :

System.NullPointerException: Attempt to de-reference a null object.


There is no error in class while executing getting error. Can someone run the code and help me with the answer. Thanks
 
SwethaSwetha (Salesforce Developers) 
HI Vishnu,
Few corrections suggested for this code are highlighted in bold. These changes will help fix the error
public class Arrays_Example {
    
    public void Example1(){
        Account[] accounts = new Account[4];
        Account a1 = new Account();
        a1.Name = 'TCS';
        a1.Phone = '1111';
        a1.Rating = 'Hot';
        accounts[0]=a1;
        Account a2 = new Account();
        a2.Name = 'Dell';
        a2.Phone = '2222';
        a2.Rating = 'Warm';
        accounts[1]=a2;
        Account a3 = new Account();
        a3.Name='CapG';
        a3.Phone='3333';
        a3.Rating='Cold';
        accounts[2]=a3;
        for (Integer i = 0; i<accounts.size()-1;i++){
            
            System.debug('Name: '+ accounts[i].Name);
            System.debug('Phone: '+accounts[i].Phone);
            System.debug('Rating: '+ accounts[i].Rating);
        }
    }

}

The for loop iterates in sequence. As there was no accounts[2] after accounts[0],accounts[1] it would throw null exception. 

Also, when you add system.debug(accounts.size()) it would give 4.  System.debug( accounts[i]) will have a null value in this scenario. When trying accounts[i].Name , Null. Name will result in null exception again.

If this information helps, please mark the answer as best. Thank you
 
Romeo SergioRomeo Sergio
Hi, we are also having the same script but don't know where we are lagging. Due to which we are also facing the same issue and unable to post on https://apkreservoir.com/top-drives-mod-apk/ . Even our core developer have also checked the script and according to him the script is correct but it still shows the issue.
mukesh guptamukesh gupta
Hi Vishnu,

When you execute your code then you will see, one null value there. that's why error happened  
 
accounts-->>(Account:{Name=TCS, Phone=1111, Rating=Hot}, Account:{Name=Dell, Phone=2222, Rating=Warm}, null, Account:{Name=CapG, Phone=3333, Rating=Cold})
Reason : you skip one Index of array that's why this error
 
Account[] accounts = new Account[4];
       Account a1 = new Account();
        a1.Name = 'TCS';
        a1.Phone = '1111';
        a1.Rating = 'Hot';
        accounts[0]=a1;
        Account a2 = new Account();
        a2.Name = 'Dell';
        a2.Phone = '2222';
        a2.Rating = 'Warm';
        accounts[1]=a2;
        Account a3 = new Account();
        a3.Name='CapG';
        a3.Phone='3333';
        a3.Rating='Cold';
        //accounts[3]=a3; // your Indexed value 
        accounts[2]=a3; // correct Index

        for (Integer i = 0; i<accounts.size()-1;i++){
          System.debug('Name: '+ accounts[i].Name);
            System.debug('Phone: '+accounts[i].Phone);
            System.debug('Rating: '+ accounts[i].Rating);
        }

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh