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
Hari nadh babu EluruHari nadh babu Eluru 

Trigger Apex getting nullpoint exception

Hi there !
In this Trigger Apex, Nullpoint Exception was getting
The code that i was did is as below
trigger SignIn_Trigger on Sign_In__c (after insert) {
    list<Sign_In__c> i = [select Current_Password__c, New_Password__c,Student__r.Email__c, Password__c, Username__c from Sign_In__c where id IN:trigger.newMap.keySet()];
    for(Sign_In__c s : i){
        if(s.Student__c != trigger.oldMap.get(s.Id).Student__c){
            system.debug('Name is'+ s.Student__c);
            String userEmail = s.Student__r.Email__c;
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String[] toAddresses = new String[] {userEmail};
            mail.setToAddresses(toAddresses);
            mail.setSubject('Your Password is updated' + ' So#- '+ s.New_Password__c);
            String body = 'Your Password is' + '-' + s.New_Password__c;
            mail.setPlainTextBody(body);
            Messaging.sendEmail(new Messaging.SingleEMailMessage[]{mail});
        }
    }
}
While i'm inserting record the error was getting the image is error as below
Nullpoint Exception error
Apex trigger SignIn_Trigger caused an unexpected exception, contact your administrator: SignIn_Trigger: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.SignIn_Trigger: line 4, column 1
Please resolve the issue and provide the solution for it. Thank you !
Best Answer chosen by Hari nadh babu Eluru
AnkaiahAnkaiah (Salesforce Developers) 
Hi Hari,

Can you please try with below code.
 
trigger SignIn_Trigger on Sign_In__c (after insert) {
    list<Sign_In__c> i = [select Current_Password__c, New_Password__c,Student__r.Email__c, Password__c, Username__c,Student__c from Sign_In__c where id IN:trigger.newMap.keySet()];
    for(Sign_In__c s : i){
        if(s.Student__c != Null){
            system.debug('Name is'+ s.Student__c);
            String userEmail = s.Student__r.Email__c;
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String[] toAddresses = new String[] {userEmail};
            mail.setToAddresses(toAddresses);
            mail.setSubject('Your Password is updated' + ' So#- '+ s.New_Password__c);
            String body = 'Your Password is' + '-' + s.New_Password__c;
            mail.setPlainTextBody(body);
            Messaging.sendEmail(new Messaging.SingleEMailMessage[]{mail});
        }
    }
}

If this helps, please mark it as best answer.

Regards,
Ankaiah Bandi

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Hari,

Can you please try with below code.
 
trigger SignIn_Trigger on Sign_In__c (after insert) {
    list<Sign_In__c> i = [select Current_Password__c, New_Password__c,Student__r.Email__c, Password__c, Username__c,Student__c from Sign_In__c where id IN:trigger.newMap.keySet()];
    for(Sign_In__c s : i){
        if(s.Student__c != Null){
            system.debug('Name is'+ s.Student__c);
            String userEmail = s.Student__r.Email__c;
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String[] toAddresses = new String[] {userEmail};
            mail.setToAddresses(toAddresses);
            mail.setSubject('Your Password is updated' + ' So#- '+ s.New_Password__c);
            String body = 'Your Password is' + '-' + s.New_Password__c;
            mail.setPlainTextBody(body);
            Messaging.sendEmail(new Messaging.SingleEMailMessage[]{mail});
        }
    }
}

If this helps, please mark it as best answer.

Regards,
Ankaiah Bandi
This was selected as the best answer
Hari nadh babu EluruHari nadh babu Eluru
@Ankaiah, Thank you very much