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
developernewbiedevelopernewbie 

Trigger - System.nullpointer exception error

I am new to salesforce development and having an issue with this error. The code compiles fine and has no errors however, when trying to save a product i get this error:

 

" RevenueProject2: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object:

Trigger.RevenueProject2: line 58, column 1 "

 

Aim of project is to search for keywords ( all myString varaibles) in the product name and then copy the sales price to a a field in the RevenueObject. Along with that, I also have a field set up for all products that gives a running counter on the number of entries for each product.

 

The error is on this line: i = rO.FieldA__c;

 

Here is my code:

 

trigger RevenueProject2 on Opportunity (after insert, after update) {

String myString1 = '/test1';
String myString2 = '/test2';
String myString3 = 'test3';
RevenueObject__c rO;
double i;
decimal j= 0.00;

 // This is to create a list of all product names 
 List<OpportunityLineItem>   Product = 
        [Select CustomProductName__c, UnitPrice
         from OpportunityLineItem 
         where Opportunity.StageName != 'Closed Lost' 
         ]; 
        
    // The for loop is to go through each  product name and to categorize product types     
    for(OpportunityLineItem prdct: Product) {

		if(prdct.CustomProductName__c.contains(myString1)) 
        {
        	i = rO.FieldA__c; 
        	i++;                       //Updates Counter
        	rO.FieldA__c = i;           // Assigns counter to field  
        	j = prdct.UnitPrice;
        	rO.FieldB__c += j;
        	
        }
        else
        if(prdct.CustomProductName__c.contains(myString2))
        {
                i = rO.FieldC__c;  
        	i++;                        //Updates Counter
        	rO.FieldC__c = i;          // Assigns counter to field  
        	j = prdct.UnitPrice;
        	rO.FieldD__c += j;
        	
        }
        else
        if(prdct.CustomProductName__c.contains(myString3)) 
        {
        	i = rO.FieldE__c;
        	i++;                       //Updates Counter
        	rO.FieldE__c = i;         // Assigns counter to field  
        	j = prdct.UnitPrice;
        	rO.FieldF__c += j;
        	
        }
	else
        if(prdct.CustomProdName__c.contains(myString4)) 
        {
        	i = rO.FieldG__c;
        	i++;                        //Updates Counter
        	rO.FieldG__c = i;          // Assigns counter to field  
        	j = prdct.UnitPrice;
        	rO.FieldH__c += j;
        	
        }

    }
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
souvik9086souvik9086

Oops my bad. It will be like this

RevenueObject__c rO = new RevenueObject__c();

 

For that the complete code is like this

 

trigger RevenueProject2 on Opportunity (after insert, after update) {

String myString1 = '/test1';
String myString2 = '/test2';
String myString3 = 'test3';
RevenueObject__c rO = new RevenueObject__c();

List<RevenueObject__c> rOList = new List<RevenueObject__c>();
double i = 0.0;
decimal j= 0.00;

// This is to create a list of all product names
List<OpportunityLineItem> Product =
[Select CustomProductName__c, UnitPrice
from OpportunityLineItem
where Opportunity.StageName != 'Closed Lost'
];

// The for loop is to go through each product name and to categorize product types
for(OpportunityLineItem prdct: Product) {

if(prdct.CustomProductName__c.contains(myString1))
{
i = rO.FieldA__c;
i++; //Updates Counter
rO.FieldA__c = i; // Assigns counter to field
j = prdct.UnitPrice;
rO.FieldB__c += j;

}
else
if(prdct.CustomProductName__c.contains(myString2))
{
i = rO.FieldC__c;
i++; //Updates Counter
rO.FieldC__c = i; // Assigns counter to field
j = prdct.UnitPrice;
rO.FieldD__c += j;

}
else
if(prdct.CustomProductName__c.contains(myString3))
{
i = rO.FieldE__c;
i++; //Updates Counter
rO.FieldE__c = i; // Assigns counter to field
j = prdct.UnitPrice;
rO.FieldF__c += j;

}
else
if(prdct.CustomProdName__c.contains(myString4))
{
i = rO.FieldG__c;
i++; //Updates Counter
rO.FieldG__c = i; // Assigns counter to field
j = prdct.UnitPrice;
rO.FieldH__c += j;

}

rOList.add(rO);

}

if(rOList.size()>0){

update rOList;

}
}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

 

All Answers

SLockardSLockard

You are getting that error because you are trying to set i equal to the RevenueObject r0's FieldA value, which does not exist. You declare r0 up top as a Reveneue Object, but do not instantiate it as a new object or fill it in a query so it is null / empty. You either need to set i equal to another objects FieldA value, or set r0 equal to something before you try to access the fields it has.

souvik9086souvik9086

Try this

 

trigger RevenueProject2 on Opportunity (after insert, after update) {

String myString1 = '/test1';
String myString2 = '/test2';
String myString3 = 'test3';
RevenueObject__c rO = new RevenueObject__c;
double i = 0.0;
decimal j= 0.00;

// This is to create a list of all product names
List<OpportunityLineItem> Product =
[Select CustomProductName__c, UnitPrice
from OpportunityLineItem
where Opportunity.StageName != 'Closed Lost'
];

// The for loop is to go through each product name and to categorize product types
for(OpportunityLineItem prdct: Product) {

if(prdct.CustomProductName__c.contains(myString1))
{
i = rO.FieldA__c;
i++; //Updates Counter
rO.FieldA__c = i; // Assigns counter to field
j = prdct.UnitPrice;
rO.FieldB__c += j;

}
else
if(prdct.CustomProductName__c.contains(myString2))
{
i = rO.FieldC__c;
i++; //Updates Counter
rO.FieldC__c = i; // Assigns counter to field
j = prdct.UnitPrice;
rO.FieldD__c += j;

}
else
if(prdct.CustomProductName__c.contains(myString3))
{
i = rO.FieldE__c;
i++; //Updates Counter
rO.FieldE__c = i; // Assigns counter to field
j = prdct.UnitPrice;
rO.FieldF__c += j;

}
else
if(prdct.CustomProdName__c.contains(myString4))
{
i = rO.FieldG__c;
i++; //Updates Counter
rO.FieldG__c = i; // Assigns counter to field
j = prdct.UnitPrice;
rO.FieldH__c += j;

}

}
}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

developernewbiedevelopernewbie

Thanks souvik9086 for the reply. Defnitely helpful. However, for some reason, I am getting the "Unexpected token error; ";"" at line RevenueObject__c rO = new RevenueObject__c;

 

also, the end aim of my code is to be able to update and display the value of fieldA, fieldB .. in the layout in SF.

 

would this be the write code to type after the For loop:

 

Thanks once again!

 

update rO.FieldA__c;
update rO.FieldB__c

 

SLockardSLockard

To fix that first error that souvik9086 suggested, change it to

RevenueObject__c r0 = new RevenueObject__c();

 

But that still won't fix the other error of nullpointer exception, please look at my response to rethink your logic.

And at the end, if r0 is the object you want to update, you will only update it once as a whole instead of each of its field by doing:

update r0;

 But you still have to fill the object first ..

souvik9086souvik9086

Oops my bad. It will be like this

RevenueObject__c rO = new RevenueObject__c();

 

For that the complete code is like this

 

trigger RevenueProject2 on Opportunity (after insert, after update) {

String myString1 = '/test1';
String myString2 = '/test2';
String myString3 = 'test3';
RevenueObject__c rO = new RevenueObject__c();

List<RevenueObject__c> rOList = new List<RevenueObject__c>();
double i = 0.0;
decimal j= 0.00;

// This is to create a list of all product names
List<OpportunityLineItem> Product =
[Select CustomProductName__c, UnitPrice
from OpportunityLineItem
where Opportunity.StageName != 'Closed Lost'
];

// The for loop is to go through each product name and to categorize product types
for(OpportunityLineItem prdct: Product) {

if(prdct.CustomProductName__c.contains(myString1))
{
i = rO.FieldA__c;
i++; //Updates Counter
rO.FieldA__c = i; // Assigns counter to field
j = prdct.UnitPrice;
rO.FieldB__c += j;

}
else
if(prdct.CustomProductName__c.contains(myString2))
{
i = rO.FieldC__c;
i++; //Updates Counter
rO.FieldC__c = i; // Assigns counter to field
j = prdct.UnitPrice;
rO.FieldD__c += j;

}
else
if(prdct.CustomProductName__c.contains(myString3))
{
i = rO.FieldE__c;
i++; //Updates Counter
rO.FieldE__c = i; // Assigns counter to field
j = prdct.UnitPrice;
rO.FieldF__c += j;

}
else
if(prdct.CustomProdName__c.contains(myString4))
{
i = rO.FieldG__c;
i++; //Updates Counter
rO.FieldG__c = i; // Assigns counter to field
j = prdct.UnitPrice;
rO.FieldH__c += j;

}

rOList.add(rO);

}

if(rOList.size()>0){

update rOList;

}
}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

 

This was selected as the best answer
developernewbiedevelopernewbie

Thanks Souvik, the code modification helped Thanks for that. however, i still see the error -  Attempt to de-reference a null object when i try to save an opportunity.

 

I believe the error is because there is no value originally in the FieldA, FieldB fields. I should assign them to 0 or somehow tackle the null exception. Would you have any best practice recommendations that i could use.

 

Error is at:

 

for(OpportunityLineItem prdct: Product) {if(prdct.CustomProductName__c.contains(myString1))
{
i = rO.FieldA__c;
i++;   //Updates Counter   <-- Error at this line
rO.FieldA__c = i; // Assigns counter to field
j = prdct.UnitPrice;
rO.FieldB__c += j;

}

 

 

Thanks again!

souvik9086souvik9086

You can assign them to 0 at first and also check not NULL everywhere. Then there will be no scope for Null exception.

 

If the post helps you, please throw KUDOS.

Thanks

developernewbiedevelopernewbie

Thanks Souvik, that helped. So i did define them as 0 and checked for null, and that solved the null exception trigger. Now, i am getting a different error; MISSING_ARGUMENT, Id not specified in an update call.

 

Error is on line:- update rOList;

 

My aim is to dispplay the updated value for FieldA, FieldB etc on the Salesforce UI in those fields.

 

I get this error when trying to save the opportunity.

 

Thank you so much once again for all the help.

 

trigger RevenueProject2 on Opportunity (after insert, after update) {

String myString1 = '/test1';
String myString2 = '/test2';
String myString3 = '/test3';

RevenueObject__c rO = new RevenueObject__c();
List <RevenueObject__c> rOList = new List<RevenueObject__c>();

double i=0.0;
decimal j= 0.00;

 // This is to create a list of all product names 
 List<OpportunityLineItem>   Product = 
        [Select CustomProductName__c, UnitPrice
         from OpportunityLineItem 
         where (Opportunity.StageName != 'Closed Lost'
         ];
        
    // The for loop is to go through each  product name and to categorize product types       
    for(OpportunityLineItem prdct: Product) {

	if(prdct.CustomProductName__c.contains(myString1)) 
        {   
        if(rO.FieldA__c !=null || rO.FieldB__c !=null)
        	{ 
        	i = rO.FieldA__c; 
        	i++;                         //Updates Counter
        	rO.FieldA__c = i;   // Assigns counter to field  
        	j = prdct.UnitPrice;
        	rO.FieldB__c += j;
        	}
        	else{
                rO.FieldA__c = 0;  // To avoid null exception
        	rO.FieldB__c = 0;
        	i = rO.FieldA__c; 
        	i++;                       //Updates Counter
        	rO.FieldA__c = i; // Assigns counter to field  
        	j = prdct.UnitPrice;
        	rO.FieldB__c += j;
        	}
        }
        else
        if(prdct.CustomProductName__c.contains(myString2)) 
        {
            if(rO.FieldC__c !=null || rO.FieldD__c !=null)
            { 
                i = FieldC__c;  
        	i++;                        //Updates Counter
        	rO.FieldC__c = i;   // Assigns counter to field  
        	j = prdct.UnitPrice;
        	rO.FieldD__c += j;
      	    }
            else {
                rO.FieldC__C = 0;
                rO.FieldD__c = 0;
                i = FieldC__c;  
        	i++;                        //Updates Counter
        	rO.FieldC__c = i;   // Assigns counter to field  
        	j = prdct.UnitPrice;
        	rO.FieldD__c += j;

             }
        }
     
         rOList.add(rO);
    }
    
    // for updating field
      if(rOList.size()>0){
     	update rOList;
     
     }
}

 

souvik9086souvik9086

You are not fetching through query

RevenueObject__c rO = new RevenueObject__c();

 

Then you are using like this

rOList.add(rO);

 

Here rO is a new instance not contains the ID of a existing value. If you want to update the values then please fetch a record from database and use update on that.

 

If the post helps you, please throw KUDOS.

Thanks

developernewbiedevelopernewbie

That makes a lot of sense now that i think of. Thanks for the input on that. (you can see i am a begginer :) ) . Now the stupid question, how would you write a code to fetch the record. I am little confused when trying that.

souvik9086souvik9086

try like this

 

trigger RevenueProject2 on Opportunity (after insert, after update) {

String myString1 = '/test1';
String myString2 = '/test2';
String myString3 = 'test3';

List<RevenueObject__c> rOList = new List<RevenueObject__c>();
double i = 0.0;
decimal j= 0.00;

// This is to create a list of all product names
List<OpportunityLineItem> Product =
[Select CustomProductName__c, UnitPrice
from OpportunityLineItem
where Opportunity.StageName != 'Closed Lost'
];

// The for loop is to go through each product name and to categorize product types
for(OpportunityLineItem prdct: Product) {

RevenueObject__c rO = new RevenueObject__c(Id=//Your LookUp field on OpportunityLineItem);

if(prdct.CustomProductName__c.contains(myString1))
{
i = rO.FieldA__c;
i++; //Updates Counter
rO.FieldA__c = i; // Assigns counter to field
j = prdct.UnitPrice;
rO.FieldB__c += j;

}
else
if(prdct.CustomProductName__c.contains(myString2))
{
i = rO.FieldC__c;
i++; //Updates Counter
rO.FieldC__c = i; // Assigns counter to field
j = prdct.UnitPrice;
rO.FieldD__c += j;

}
else
if(prdct.CustomProductName__c.contains(myString3))
{
i = rO.FieldE__c;
i++; //Updates Counter
rO.FieldE__c = i; // Assigns counter to field
j = prdct.UnitPrice;
rO.FieldF__c += j;

}
else
if(prdct.CustomProdName__c.contains(myString4))
{
i = rO.FieldG__c;
i++; //Updates Counter
rO.FieldG__c = i; // Assigns counter to field
j = prdct.UnitPrice;
rO.FieldH__c += j;

}

rOList.add(rO);

}

if(rOList.size()>0){

update rOList;

}
}

 

Note: //Your LookUp field on OpportunityLineItem - Here you put your lookup field value.

 

If the post helps you please throw KUDOS.

Thanks