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
craskulineczcraskulinecz 

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

Ok, I'm getting the following error when using this Apex Trigger.

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger copyMSGIdCases caused an unexpected exception, contact your administrator: copyMSGIdCases: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.copyMSGIdCases: line 21, column 56

Here is the code I am using:

Code:
// This trigger assigns a value to the MSG_ID__c custom field on cases before the new case
// is saved to the database.

trigger copyMSGIdCases on Case (before insert, before update) {
 // Determine the account
 Set<ID> accIds = new Set<ID>();
 for (Case cas : Trigger.new)
  accIds.add(cas.accountid);
 // Query the Account to get the MSG_ID__c entry
 Map<Id, Account> entries = new Map<Id, Account>(
  [select MSG_ID__c, PAS_ID__c from Account
  where id in :accIds]);
 
 // Now set the Account_MSG_ID__c on the Contact to be the same as MSG_ID__c on the
 // related account
 // Now set the PAS_Account_ID__c on the Case to be the same as PAS_ID__c on the
 // related account
 for (Case cas : Trigger.new)
 cas.MSG_ID__c = entries.get(cas.AccountId).MSG_ID__c;
 for (Case cas : Trigger.new)
 cas.PAS_Account_ID__c = entries.get(cas.AccountId).PAS_ID__c;

}
Now, I'm sure this can be done in a different way, but I went this route because I wanted to learn more about Apex Triggers.

So here is the funny thing, I only get this error if I am trying to create a new case as an regular user in SFDC. If I am logged in as the System Administrator, it all executes fine with no errors and does what I expect it to do.

Let me clarify, I am logged in as a System Admin User and then from there, go to Manage Users->Users, and choose the user I want to login as and then press the login button.

 Has anyone run into this before?

Any suggestions?
 

Message Edited by craskulinecz on 10-16-2007 10:36 PM

Message Edited by craskulinecz on 10-16-2007 10:38 PM

TehNrdTehNrd
Give this a try and report back. Might not make a difference but it is a little different.

Code:
// This trigger assigns a value to the MSG_ID__c custom field on cases before the new case
// is saved to the database.

trigger copyMSGIdCases on Case (before insert, before update) {
  // Determine the account
  Set<ID> accIds = new Set<ID>();
 
 for (Case cas : Trigger.new){
   accIds.add(cas.accountid);
 }

 // Query the Account to get the MSG_ID__c entry
  Map<Id, Account> entries = new Map<Id, Account>([select MSG_ID__c, PAS_ID__c from Account where id in :accIds]);
 
  // Now set the Account_MSG_ID__c and PAS_Account_ID__c on the Contact to be the same as the related account
 
  for (Case cas : Trigger.new){
   cas.MSG_ID__c = entries.get(cas.AccountId).MSG_ID__c;
   cas.PAS_Account_ID__c = entries.get(cas.AccountId).PAS_ID__c;
 }
}

 

 

craskulineczcraskulinecz
Thanks for the help, unfortunately, it didn't make a difference.

Chris
mtbclimbermtbclimber
Is the non-admin just a standard user? Does that user have read access to account?

craskulineczcraskulinecz
The non-admin has Read-Write access to the account, opportunity, and case. But when I look at the reasons for access under the account, she has access because of 8 different reasons. 3 have read/write because of Account Sharing rules, the other 5 have Read Only because of associated record owner or sharing.
cogcog

I came across this a lot of times. I guess one of the fields that you are assigning might be null (most probably PAS_ID__c). Pls try to eliminate nulls and see (and i know how hard it is) :smileyindifferent:

nilanginilangi

apex code::

 

<apex:page controller="resulttab">
<apex:form >
<apex:pageBlock >
 <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!redirectToPage}" rerender="error"/>
            </apex:pageBlockButtons>
 <apex:inputField value="{!res.examid__c}"/>
 </apex:pageBlock>
 </apex:form>
</apex:page>

 

 

class code::

 

public class resulttab
{
   public result__c res {get; set;}
   public String a {get; set;}
   
   public List<result__c> getInfo()
   {
   a = res.examid__c;
   return [SELECT name__c,college_name__c,branch__c,year__c,semester__c,clgno__c,total__c,status__c,percent__c,marks__c,examid__c FROM result__c WHERE examid__c=: res.examid__c ];
   
   }
   
   public PageReference redirectToPage()
    {
        return new PageReference('https://c.ap1.visual.force.com/apex/detailres');
    }
}

 

 

error::

 

System.NullPointerException: Attempt to de-reference a null object
Class.resulttab.getInfo: line 8, column 1

 

 

Record present in result table then also null pointer exception occus...