+ Start a Discussion
GYAN ANDRUSGYAN ANDRUS 

I am getting errpr attempt to derefence null object error while using map..Please help Urgent


List <peer__Investment_Booking__c> TEMPInvestmentBookingList = new List<peer__Investment_Booking__c>();
            peer__Investment_Booking__c TEMPInvestmentBooking1;
            bookingOrderList = [SELECT Id, peer__Investor__c,Insured_Amount__c,Total_Insured_Amount__c FROM peer__Booking_Order__c WHERE peer__Investor__c =: account.Id];
          

          
          Map<String,List<peer__Investment_Booking__c>> mapBookingOrder = new Map<String,List<peer__Investment_Booking__c>>();
          
          for(peer__Investment_Booking__c peer : [SELECT peer__Investment_Amount__c,Insured_Percentage__c,Insured_Amount__c,Premium_Component__c,peer__Loan_Application__c
                                         FROM peer__Investment_Booking__c WHERE peer__Booking_Order__c in: bookingOrderList]) {
            if(!mapBookingOrder.containsKey(peer.peer__Booking_Order__c)) {
                List<peer__Investment_Booking__c> lstinvestBooking =  new List<peer__Investment_Booking__c>();
                mapBookingOrder.put(peer.peer__Booking_Order__c,lstinvestBooking);
            }
            mapBookingOrder.get(peer.peer__Booking_Order__c).add(peer);
          }
                
          for(i=0;i<bookingOrderList.size();i++)
            {
                bookingOrder = bookingOrderList.get(i);
                investmentBookingList = mapBookingOrder.get(bookingOrder.Id);//[SELECT peer__Investment_Amount__c,Insured_Percentage__c,Insured_Amount__c,Premium_Component__c,peer__Loan_Application__c
                                        // FROM peer__Investment_Booking__c WHERE peer__Booking_Order__c =: bookingOrder.Id];
                for(j=0;j<investmentBookingList.size();j++)
                {
                    investmentBooking = investmentBookingList.get(j);
                    TEMPInvestmentBookingList.add(investmentBooking);
                }
            }
            for(i=0;i<TEMPInvestmentBookingList.size();i++)
            {
                investmentBooking = TEMPInvestmentBookingList.get(i);
                j=i+1;
                while(j<TEMPInvestmentBookingList.size())
                {
                    TEMPInvestmentBooking1 = TEMPInvestmentBookingList.get(j);
                    if(investmentBooking.peer__Loan_Application__c == TEMPInvestmentBooking1.peer__Loan_Application__c)
                    {
                        investmentBooking.peer__Investment_Amount__c = investmentBooking.peer__Investment_Amount__c + TEMPInvestmentBooking1.peer__Investment_Amount__c;
                        TEMPInvestmentBookingList.remove(j);
                    }
                    else{
                        j++;
                    }
                }
                }
Pankaj MehraPankaj Mehra
Hi 

There may be case where in line
  investmentBookingList = mapBookingOrder.get(bookingOrder.Id);//[SELECT 

you have no peer__Investment_Booking__c record for booking Id

So just above the this line add code :

if(!mapBookingOrder.containsKey(bookingOrder.Id)) {
  continue;
}

Thanks