0

I would like to create a trigger to activate when using the Battle class and its battle percentage method, but it gives me a "activate percentage: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Class.Battle. Battle percentage: line 5, column 1 Trigger. Activate percentage: line 4, column 1 "What could be the solution? thank you

My class:

public class Batalla {

public List porcentajebatalla(Map batallas) { //List ListBattle = [SELECT Id,Oponente2__c, Poder_ejercito1__c, Puntos_poder_ejercito2__c, Porcentaje_batalla__c FROM Batalla__c]; List ListBattle = batallas.values();

    Double high = 0.0;
    Double high2 = 0.0;
    Double low = 0.0;
    Double low2 = 0.0;
    Double combatP1 = 0.0;
    Double combatP2 = 0.0;
   // Double resultBattle = 0.0;
    Double result = 0.0;
    Double result2 = 0.0;
    Integer QuerySize = ListBattle.size();
    System.debug('QuerySize = ' + QuerySize);
    if(QuerySize > 0 ) {
       for(Batalla__c battle : ListBattle){
            combatP1 = battle.Poder_ejercito1__c;
            System.debug('combatP1 = ' + combatP1);
            combatP2 = battle.Puntos_poder_ejercito2__c;
            System.debug('combatP2 = ' + combatP2);
            high = combatP1 * 0.4;
             low = combatP1  * 0.2;
            high2 = combatP2 * 0.4;
             low2 = combatP2  * 0.2;
            System.debug('high = ' + combatP1 * 0.4);
             System.debug('low = ' + combatP1  * 0.2);
            System.debug('high2 = ' + combatP2 * 0.4);
              System.debug('low2 = ' + combatP2  * 0.2);
            result = (math.random() * (high-low) + low);
           result2 = (math.random() * (high2-low2) + low2);
            battle.Porcentaje_batalla__c = result;
           battle.Porcentaje_2__c = result2;
            /*System.debug('battle.Porcentaje_batalla__c = ' + battle.Porcentaje_batalla__c);
             resultBattle = combatP2 - result;
            System.debug('resultBattle = ' + resultBattle);*/
             System.debug('result = ' + result);
           System.debug('result2 = ' + result2);
        }
    }
      upsert ListBattle;
    return ListBattle;    }  

}

And my trigger is:

  trigger activarporcentaje on Batalla__c (before insert) {
            Batalla bata = new Batalla();    bata.porcentajebatalla(Trigger.newMap);
          }
1
  • Please edit your post and describe the specific line in porcentajebatalla that is causing the error. Oct 10, 2019 at 15:16

1 Answer 1

0

The reason for your error is that your trigger is a beforeInsert trigger and you are passing Trigger.newMap which is only available in an after insert trigger

Trigger.newMap - A map of IDs to the new versions of the sObject records. This map is only available in before update, after insert, after update, and after undelete triggers.

Hence List<Batalla__c> ListBattle = batallas.values(); has a null value for batallas.

You should pass Trigger.new to the trigger helper class and there's no reason to requery it because all of the fields in your SOQL will implictly be in Trigger.new (aside from the fact that the record won't for querying in a before insert context.

BTW - as a favor to the community; you should really edit your post and use proper formatting of code - see the How to Ask link and formatting help links

1
  • edited, thank you very much for your help !!
    – Raúl
    Oct 11, 2019 at 6:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .