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
Cody Sanders 33Cody Sanders 33 

Attempt to de-reference a null value in test class for record query

Hello, i'm an admin who is rather inexperienced at Apex but this project was kind of dumped in my lap by a developer who has other positions he's filling right now, and I am in need of help since I can't find an answer to my particular problem. I am trying to test this query class and the test class keeps bringing the "attempt to de-reference a null object" error and I'm super confused. It's referring to line 6 in the test class so something with invoking the class is off, it seems but my lack of knowledge isn't particularly helping me figure out what I need to do. I'll leave the code for the class and test class below:
Class
public class AlroOrderBoMQuery {
	public Production_Tracking_Record__c custObj;
    public string HsgMat { get; set; }
    public string ECMat { get; set; }
    public string WPMat { get; set; }
    public string ThrtMat { get; set; }
    public string LnrMat { get; set; }
    
public AlroOrderBoMQuery(){
    }    
    
    public AlroOrderBoMQuery(ApexPages.StandardController controller){
        custObj=(Production_Tracking_Record__c)controller.getRecord();
        
           }
		Production_Tracking_Record__c ptr = [Select Id, Name, Product__c from Production_Tracking_Record__c where id =: custObj.Id limit 1];
    	
    	Product2 prod = [Select Id, Housing_Assembly__c, Drive_Endcap_Assembly__c, Tail_Endcap_Assembly__c, Housing_Material__c, Liner_Material__c, Endcap_Material__c, Wearplate__c, Housing_Throat_Plates__c from Product2 where id =: ptr.Product__c limit 1];
    	{
        HsgMat = prod.Housing_Material__c;
        ECMat = prod.Endcap_Material__c;
        WPMat = prod.Wearplate__c;
        ThrtMat = prod.Housing_Throat_Plates__c;
        LnrMat = prod.Liner_Material__c;
    	}
		List<Housing_Assembly_Attachment__c> HsgAsmAtt = [SELECT Quantity__c, Name, Id, Housing_Assembly__c FROM Housing_Assembly_Attachment__c WHERE Housing_Assembly__c =: prod.Housing_Assembly__c];
    	public List<Housing_Assembly_Attachment__c> getHsgAsmAtt() {
        return HsgAsmAtt;
    	   }
    	List<Endcap_Assembly_Attachment__c> DECAsmAtt = [SELECT Quantity__c, Name, Id, Endcap_Assembly__C FROM Endcap_Assembly_Attachment__c WHERE Endcap_Assembly__c =: prod.Drive_Endcap_Assembly__c];
    	public List<Endcap_Assembly_Attachment__c> getDECAsmAtt() {
        return DECAsmAtt;
    	   }
    	List<Endcap_Assembly_Attachment__c> TECAsmAtt = [SELECT Quantity__c, Name, Id, Endcap_Assembly__C FROM Endcap_Assembly_Attachment__c WHERE Endcap_Assembly__c =: prod.Tail_Endcap_Assembly__c];
    	public List<Endcap_Assembly_Attachment__c> getTECAsmAtt() {
        return TECAsmAtt;
    	}

    	
}

Test Class
@isTest
    private class TestAlroOrderBoMQuery {
        public static List<Opportunity> OpportunityList;
    
      static testMethod void TestAlroOrderBoMQuery() {
          AlroOrderBoMQuery BOM = new AlroOrderBoMQuery();

         
        Make__c make = new Make__c(Name='Kice');
        insert make;
        Model__c model = new Model__c(Name='Kice VJ 14x10x10',
                                     Make__c=make.Id,
                                     Product_Passage__c='Drop Thru');
        insert model;
        Housing_Assembly__c hsasm = new Housing_Assembly__c(Name='Kice Housing 1',
                                                            Model__c=model.Id,
                                                            Product_Passage__c='Drop-Through',
                                                            Housing_Type__c='Rebuild',
                                                            verified__c=true);
        insert hsasm;
        Housing_Assembly_Attachment__c hsasmatt = new Housing_Assembly_Attachment__c(Name='Drawing 1',
                                                                                    Housing_Assembly__c=hsasm.Id,
                                                                                    Quantity__c=1);
        insert hsasmatt;
        Attachment hsgatt = new Attachment(Name='Test Housing Attachment.jpg',
                                        ParentId=hsasmatt.Id,
                                        Body=Blob.valueOf('Test Housing Attachment'),
                                        IsPrivate=false);
        insert hsgatt;
        Endcap_Assembly__c ecasm = new Endcap_Assembly__c(Name='Kice Endcap 1',
                                                          Model__c=model.Id,
                                                          Endcap_Type__c='Rebuild',
                                                          Configuration__c='Inboard',
                                                          verified__c=true);
        insert ecasm;
        Endcap_Assembly_Attachment__c ecasmatt = new Endcap_Assembly_Attachment__c(Name='Drawing 2',
                                                                                    Endcap_Assembly__c=ecasm.Id,
                                                                                    Quantity__c=1);
        insert ecasmatt;
        Attachment ecatt = new Attachment(Name='Test Endcap Attachment.jpg',
                                        ParentId=hsasmatt.Id,
                                        Body=Blob.valueOf('Test Endcap Attachment'),
                                        IsPrivate=false);
        insert ecatt;
        Product2 prod = new Product2(Name='Test Product',
                                     Family='Kice',
                                     IsActive=true,
                                     Inboard_or_Outboard__c='Inboard',
                                     Rotor_Configuration__c='Open Fixed',
                                     Product_Passage__c='Drop Thru',
                                     Housing_Type__c='Rebuild',
                                     RecordTypeId='012G0000000qdfL',
                                     Make_Lookup__c=make.Id,
                                     Model_Lookup__c=model.Id,
                                     Housing_Assembly__c=hsasm.Id,
                                     Drive_Endcap_Assembly__c=ecasm.Id,
                                     Tail_Endcap_Assembly__c=ecasm.Id,
                                     Verified__c=true);
        insert prod;
        PricebookEntry PBE = new PricebookEntry(IsActive=true,
                                                UnitPrice=1000,
                                                UseStandardPrice=false,
                                                Pricebook2Id='01sG00000001zX0',
                                                Product2Id=prod.Id);
        insert PBE;
        Account acct = new Account(Name='Test Account',
                                   Type='Direct Customer',
                                   Shipping__c='Prepaid',
                                   Ship_Via__c='UTS',
                                   Sales_Discount__c=0);
        insert acct;
        Opportunity opp = new Opportunity(Name='12345',
                                          RA__c='12345',
                                          StageName='Quote Sent',
                                          CloseDate=System.today(),
                                          AccountId=acct.Id,
                                          Product__c=prod.Id,
                                          Make__c='Kice',
                                          Model__c='VJ 14x10x10',
                                          Max_Temp__c='120',
                                          Serial__c='N/A',
                                          Cold_Clr_Radial__c='.004-.007',
                                          Cold_Clr_Side__c='.004-.007');
        insert opp;
        Quote quote = new Quote(Name=opp.RA__c,
                                OpportunityId=opp.Id,
                                Pricebook2Id='01sG00000001zX0');
        insert quote;
        /*QuoteLineItem QLI = new QuoteLineItem(QuoteId=quote.Id,
                                              Product2Id=prod.Id,
                                              Quantity=1,
                                              PricebookEntryId=PBE.Id,
                                              UnitPrice=1000,
                                              Additional_Charges__c=0,
                                              Discounted_Amount__c=0,
                                              Core_Charge__c=0,
                                              Credit_Amount__c=0,
                                              Shipping_and_Handling__c=0,
                                              Rush_Charges__c=0);
        insert QLI; */
        
        
        Test.startTest();
        Production_Tracking_Record__c PTR = new Production_Tracking_Record__c(Name='12345',
                                                                              RA__c=opp.Id,
                                                                              Product__c=prod.Id,
                                                                              Housing_and_Endcaps_Order_Date__c=null,
                                                                              Shaft_Steps_on_Drive_End__c='1');
        insert PTR;
                  
        Test.stopTest();
        
        Production_Tracking_Record__c newptr = [ select id from Production_Tracking_Record__c where id =: BOM.custObj.Id];
        System.assertNotEquals('', BOM.custObj.Id, 'There is no ID');
        
        
    }
}
When I save there's not an error, but tests keep failing. If anyone could help, I'd greatly appreciate it! 
Cody Sanders 33Cody Sanders 33
Update: I forgot to mention while line 6 is being referenced in the test failures of the class, it's also referencing line 16 of the main class. So I'm trying to see if there's a problem with the lookup as well.
Sibasish PanigrahiSibasish Panigrahi
Hi Cody,

When you are inserting "Product2" record in test class, plz give a value to its "Housing_Throat_Plates__c" field and then try to run again.

I hope this will clear your "Attempt to de-reference a null value" issue.