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
Peter Guzman Garcia 22Peter Guzman Garcia 22 

System.NullPointerException: Attempt to de-reference a null object Trigger.NumberofUnitsPerTypeSelected: line 7, column 1

Hi;
I am hoping to get some help on this trigger. I am new to Apex programming. I have a custom object called Dues Calculator. For which I have a picklist depency, when the user selects one option "Unit Type" two additional picklist fields that need to be filled out are pre-populated with the correct options. Since I need to use those fields in formula. I created addtional fields that end in "selected". The purpose of my trigger is to automatically populate the "selected" fields with the options from the picklist depencies.
This is my trigger:
trigger NumberofUnitsPerTypeSelected on Dues_Calculator__c (before insert) {
    for(Dues_Calculator__c dues : Trigger.New){
        
       // Converting and Assigning the Picklist Value to the Selected Field to be used in a formula
        Decimal numberUnitTypeSelected   = Dues.Number_of_Units_Per_Type_Selected__c;
        String  numberUnitType           = Dues.Number__c;
        numberUnitType                     = numberUnitTypeSelected.format(); 
            
        // Converting and Assigning the Picklist Value to the Selected Field to be used in a formula
        Decimal percentageSelected       = Dues.Percentage_of_Ownership_Type_Selected__c;
        String  percentageOwnershipShare = Dues.Percentage_of_Ownership_Share__c;
        percentageOwnershipShare         = percentageSelected.format();
        
        insert dues;
        }
}

I get a Null Pointer Exception error, I tried to add the option to create a list. I am stuck here.

Any help is greatly appreciated;
Peter
Best Answer chosen by Peter Guzman Garcia 22
Moly DolyMoly Doly

Attempt to de-reference a null object” normally occurs when you try to reference an object which has not been initialized or has null values. To avoid this you need to make sure that all the sObjects in your class are initialized, preferably in the constructor. Hope this helps. 


For sone other information please visit: ( https://printerloop.com/ ).

Thank you.

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Peter,
Can you please clarify the below points.

which field you are selecting & what is the value?
In which field you need auto populate the two picklist values and what is the data type of that field?

Thanks!!
dhsjka shjkdhsjka shjk
There are so many amazing and interesting impressions that we can get from here (https://toolhutt.com/best-router-bit-set/) you can apply it on your issue especially on this one.
Peter Guzman Garcia 22Peter Guzman Garcia 22
Hi @Ankaiah,  
I need to autopopulate the value of: "Number_of_Units_Per_Type_Selected__c"  this field is a decimal.
from the selected value of: "Number__c" this field is a Picklist Value String.
I need to autopopulate the value of: "Percentage_of_Ownership_Type_Selected__c" this field is a percent.
from the selected value of: "Percentage_of_Ownership_Share__c" this is a Picklist Value String.

The API name for my custom object is: Dues_Calculator__c

It would be nice if it was a before insert trigger, an after insert works too.

Thank you;
Peter 
Moly DolyMoly Doly

Attempt to de-reference a null object” normally occurs when you try to reference an object which has not been initialized or has null values. To avoid this you need to make sure that all the sObjects in your class are initialized, preferably in the constructor. Hope this helps. 


For sone other information please visit: ( https://printerloop.com/ ).

Thank you.

This was selected as the best answer
Jessica WaltersJessica Walters

Thanks for sharing this informative and amazing post here. I found it very interesting and enjoy reading this blog. Will wait for more posts.

https://anhuijiuxinltd.com/product/disposable-tableware/

AnkaiahAnkaiah (Salesforce Developers) 
Hi Peter,

What are all the picklist values in Number__c & Percentage_of_Ownership_Share__c??

Thanks!!

 
Peter Guzman Garcia 22Peter Guzman Garcia 22
I figured it out, thank you everyone for your input.
Alice JennyAlice Jenny
Wow! Your coding skill is extremely impressive. Hope you will share more coding knowlegde in the future. 
wordle unlimited (https://wordleunlimited.io/)
mukesh devmukesh dev

No doubt, birthdays are special for everyone especially for kids. Additionally, their birthday party is more important for them than any other thing. Birthday World, being best birthday organizer company have a variety of new themes for birthdays.

 

Games on rent in Gurgaon

 

Birthday Party Organisers in Gurgaon | Birthday Decorators in Gurgaon | Birthday Planner in Gurgaon | Birthday Event Planners in Gurgaon

 

Artists for birthday party in Gurgaon


 
mukesh devmukesh dev
We provide the best Ambulance Service in ghansoli. ghansoli is one of the most populated suburbs in Navi Mumbai. It has a population of over 700,000 people. The area is well-connected to other parts of the city and state.