+ Start a Discussion
ThomasmThomasm 

System.NullPointerException: Attempt to de-reference a null object

When I run the test on my class i am getting the Attemp to de-reference a null object

@isTest
public class SV_Case{

static testMethod void createCase(){

sitevisit__c sVisit = new Sitevisit__c();

sVisit.RecordType.Name ='DM_of_Ops_Quality_Audit_Househeeping';
sVisit.baseboard_stairway__c = 'Fail';
sVisit.baseboard_hallways_corridors__c = 'Fail';
sVisit.baseboards_lobbies__c = 'Fail';
sVisit.baseboards_restroom__c = 'Fail';
sVisit.baseboards_conference__c = 'Fail';
sVisit.baseboards_office_area__c = 'Fail';
sVisit.baseboards_kitchen__c = 'Fail';
sVisit.switches_kitchen__c = 'Fail';
sVisit.switches_office__c = 'Fail';
sVisit.switches_lobbies__c = 'Fail';
sVisit.switches_conference_room__c = 'Fail';
sVisit.switches_hallways_corridors__c = 'Fail';
sVisit.switches_stairway__c = 'Fail';
sVisit.switches_kitchen__c = 'Fail';
sVisit.switches_warehouse__c = 'Fail';
sVisit.doors_office__c = 'Fail';
sVisit.doors_restrooms__c = 'Fail';
sVisit.doors_lobbies__c = 'Fail';
sVisit.doors_conference_room__c = 'Fail';
sVisit.doors_hallways_corridors__c = 'Fail';
sVisit.doors_stairway__c = 'Fail';
sVisit.doors_kitchen__c = 'Fail';
sVisit.doors_warehouse__c = 'Fail';
sVisit.walls_partitions_restroom_lockerroom__c = 'Fail';
sVisit.walls_partitions_hallways_partitions__c = 'Fail';
sVisit.walls_lobbies__c = 'Fail';
sVisit.walls_conference_room__c = 'Fail';
sVisit.walls_partitions_kitchen__c = 'Fail';
sVisit.walls_partitions_warehouse__c = 'Fail';
sVisit.walls_windows_partitions_office__c = 'Fail';
sVisit.walls_stairway__c = 'Fail';
sVisit.chair_office__c = 'Fail';
sVisit.chairs_lobbies__c = 'Fail';
sVisit.chairs_conference_room__c = 'Fail';
sVisit.chairs_kitchen__c = 'Fail';
sVisit.furniture_warehouse__c = 'Fail';
sVisit.furniture_conference_room__c = 'Fail';
sVisit.furnitures_desk_tables_office__c = 'Fail';
sVisit.furniture_warehouse__c = 'Fail';
sVisit.horizontal_surfaces_office__c = 'Fail';
sVisit.horizontal_surfaces_restroom_lockerroom__c = 'Fail';
sVisit.horizontal_surfaces_lobbies__c = 'Fail';
sVisit.horizontal_surfaces_conference_room__c = 'Fail';
sVisit.horizontal_surfaces_stairway__c = 'Fail';
sVisit.horizontal_surfaces_kitchen__c = 'Fail';
sVisit.horizontal_surfaces_warehouse__c = 'Fail';
sVisit.horizontal_surfaces_hallways_corridors__c = 'Fail';
sVisit.ceilings_office__c = 'Fail';
sVisit.ceiling_stairway__c = 'Fail';
sVisit.ceiling_lobbies__c = 'Fail';
sVisit.ceilings_restroom_lockerroom__c = 'Fail';
sVisit.ceilings_conference_room__c = 'Fail';
sVisit.ceilings_kitchen__c = 'Fail';
sVisit.ceilings_hallways_corridors__c = 'Fail';
sVisit.floors_drains_office__c = 'Fail';
sVisit.floors_lobbies__c = 'Fail';
sVisit.floors_conference_room__c = 'Fail';
sVisit.floors_drains_stairway__c = 'Fail';
sVisit.floors_drains_kitchen__c = 'Fail';
sVisit.floors_drains_warehouse__c = 'Fail';
sVisit.floors_hallways_corridors__c = 'Fail';
sVisit.floors_drains_restroom_lockerroom__c = 'Fail';
sVisit.corners_office__c = 'Fail';
sVisit.corners_restroom_lockerroom__c = 'Fail';
sVisit.corners_lobbies__c = 'Fail';
sVisit.corners_conference_room__c = 'Fail';
sVisit.corners_stairway__c = 'Fail';
sVisit.corners_kitchen__c = 'Fail';
sVisit.corners_warehouse__c = 'Fail';
sVisit.corners_hallways_corridors__c = 'Fail';
sVisit.vents_office__c = 'Fail';
sVisit.vents_restroom_lockerroom__c = 'Fail';
sVisit.vents_lobbies__c = 'Fail';
sVisit.vents_conference_room__c = 'Fail';
sVisit.vents_stairway__c = 'Fail';
sVisit.vents_kitchen__c = 'Fail';
sVisit.vents_hallways_corridors__c = 'Fail';
sVisit.trash_hallways_corridors__c = 'Fail';
sVisit.trash_office__c = 'Fail';
sVisit.trash_restroom_lockerroom__c = 'Fail';
sVisit.trash_conference_room__c = 'Fail';
sVisit.trash_kitchen__c = 'Fail';
sVisit.trash_warehouse__c = 'Fail';
sVisit.phones_office__c = 'Fail';
sVisit.phones_lobbies__c = 'Fail';
sVisit.phones_conference_room__c = 'Fail';
sVisit.phones_kitchen__c = 'Fail';
sVisit.baseboard_stairway__c = 'Fail';
sVisit.baseboard_hallways_corridors__c = 'Fail';
sVisit.baseboards_lobbies__c = 'Fail';
sVisit.baseboards_restroom__c = 'Fail';
sVisit.baseboards_conference__c = 'Fail';
sVisit.baseboards_office_area__c = 'Fail';
sVisit.baseboards_kitchen__c = 'Fail';
sVisit.appliances_vending_kitchen__c = 'Fail';
sVisit.drinking_fountains_eyewash_warehouse__c = 'Fail';
sVisit.safety_rails_fire_extinguisher_warehouse__c = 'Fail';
sVisit.stairs_risers_stairway__c = 'Fail';
sVisit.hand_rails_stairway__c = 'Fail';
sVisit.toilets_urinals_restrooms_lockerroom__c = 'Fail';
sVisit.sinks_restroom_lockerroom__c = 'Fail';
sVisit.mirrors_restrooms_lockerroom__c = 'Fail';
sVisit.tables_furniture_kitchen__c = 'Fail';
sVisit.dispensers_restroom__c = 'Fail';
sVisit.job__C='a013000000UHggN';

Test.startTest();

insert sVisit;

Test.stopTest();

}
}
Best Answer chosen by Thomasm
John De SantiagoJohn De Santiago
Sorry bug in my example:

sVisit.RecordType = new RecordType();
sVisit.RecordType.Name = '';

All Answers

KodiKodi
Hi,
check your code some values are not passed sVisit. 
GlynAGlynA
Your test code doesn't seem to be the problem.  The exception must be getting thrown from the code you are testing.  Can you post that please?

-Glyn
ThomasmThomasm
trigger SV_Cases on SiteVisit__c (before insert)
{
     List<Case>cases=new List<Case>();
         for(sitevisit__C b:Trigger.new)
         {
            if(b.baseboard_stairway__c == 'Fail'&& b.recordtype.name=='DM_of_Ops_Quality_Audit_Househeeping' )
                {         
                    Cases.add(new Case(subject='joe Site Visit Failed:  Baseboard Stairway '+ b.location_stairway__C,Status='New',Origin='email', recordTypeID=b.Case_ID__C));
                }
            if(b.switches_kitchen__c == 'Fail' && b.recordtype.name=='DM_of_Ops_Quality_Audit_Househeeping'  )
                {
                    Cases.add(new Case(subject='Site Visit Failed:  switches kitchen ' + b.Location_kitchen__c ,Status='New',Origin='email',recordtypeID=b.Case_ID__C));
                }
            if(b.doors_kitchen__c == 'Fail' && b.recordtype.name=='DM_of_Ops_Quality_Audit_Househeeping'  )
                {
                    Cases.add(new Case(subject='Site Visit Failed:  doors kitchen ' + b.Location_kitchen__c,Status='New',Origin='email',recordtypeID=b.Case_ID__C));
                }
            if(b.walls_partitions_kitchen__c == 'Fail'&& b.recordtype.name=='DM_of_Ops_Quality_Audit_Househeeping'  )
                {
                    Cases.add(new Case(subject='Site Visit Failed:  walls partitions kitchen ' + b.Location_kitchen__c,Status='New',Origin='email',recordtypeID=b.Case_ID__C));
                }
            if(b.chairs_kitchen__c == 'Fail'&& b.recordtype.name=='DM_of_Ops_Quality_Audit_Househeeping'  )
                {
                    Cases.add(new Case(subject='Site Visit Failed:  chairs kitchen ' + b.Location_kitchen__c,Status='New',Origin='email',recordtypeID=b.Case_ID__C));
                }
            if(b.tables_furniture_kitchen__c == 'Fail'&& b.recordtype.name=='DM_of_Ops_Quality_Audit_Househeeping'  )
                {
                    Cases.add(new Case(subject='Site Visit Failed:  tables furniture kitchen ' + b.Location_kitchen__c,Status='New',Origin='email',recordtypeID=b.Case_ID__C));
                }
            if(b.horizontal_surfaces_kitchen__c == 'Fail' && b.recordtype.name=='DM_of_Ops_Quality_Audit_Househeeping' )
                {
                    Cases.add(new Case(subject='Site Visit Failed:  horizontal surfaces kitchen ' + b.Location_kitchen__c,Status='New',Origin='email',recordtypeID=b.Case_ID__C));
                }
            if(b.ceilings_kitchen__c == 'Fail' && b.recordtype.name=='DM_of_Ops_Quality_Audit_Househeeping' )
                {
                    Cases.add(new Case(subject='Site Visit Failed:  ceilings kitchen ' + b.Location_kitchen__c,Status='New',Origin='email',recordtypeID=b.Case_ID__C));
                }
           
            if(b.switches_office__c == 'Fail' && b.recordtype.name=='DM_of_Ops_Quality_Audit_Househeeping' )
                {
                    Cases.add(new Case(subject='Site Visit Failed:  switches office ' + b.Location_office__c,Status='New',Origin='email',recordtypeID=b.case_ID__C));
                }
            if(b.doors_office__c == 'Fail' && b.recordtype.name=='DM_of_Ops_Quality_Audit_Househeeping' )
                {
                    Cases.add(new Case(subject='Site Visit Failed:  doors office ' + b.Location_office__c,Status='New',Origin='email',recordtypeID=b.case_ID__C));
                }
            if(b.furnitures_desk_tables_office__c == 'Fail' && b.recordtype.name=='DM_of_Ops_Quality_Audit_Househeeping' )
                {
                    Cases.add(new Case(subject='Site Visit Failed:  furnitures desk tables office ' + b.Location_office__c,Status='New',Origin='email',recordtypeID=b.case_ID__C));
                }
            if(b.chair_office__c == 'Fail' && b.recordtype.name=='DM_of_Ops_Quality_Audit_Househeeping' )
                {
                    Cases.add(new Case(subject='Site Visit Failed:  chair office ' + b.Location_office__c,Status='New',Origin='email',recordtypeID=b.case_ID__C));
                }
            if(b.walls_windows_partitions_office__c == 'Fail' && b.recordtype.name=='DM_of_Ops_Quality_Audit_Househeeping' )
                {
                    Cases.add(new Case(subject='Site Visit Failed:  walls windows partitions office ' + b.Location_office__c,Status='New',Origin='email',recordtypeID=b.case_ID__C));
                }
            if(b.baseboards_office_area__c == 'Fail' && b.recordtype.name=='DM_of_Ops_Quality_Audit_Househeeping' )
                {
                    Cases.add(new Case(subject='Site Visit Failed:  baseboards office area ' + b.Location_office__c,Status='New',Origin='email',recordtypeID=b.case_ID__C));
                }
            if(b.horizontal_surfaces_office__c == 'Fail' && b.recordtype.name=='DM_of_Ops_Quality_Audit_Househeeping' )
                {
                    Cases.add(new Case(subject='Site Visit Failed:  horizontal surfaces office ' + b.Location_office__c,Status='New',Origin='email',recordtypeID=b.case_ID__C));
                }
            if(b.floors_drains_office__c == 'Fail' && b.recordtype.name=='DM_of_Ops_Quality_Audit_Househeeping' )
                {
                    Cases.add(new Case(subject='Site Visit Failed:  floors drains office ' + b.Location_office__c,Status='New',Origin='email',recordtypeID=b.case_ID__C));
                }
            if(b.corners_office__c == 'Fail' && b.recordtype.name=='DM_of_Ops_Quality_Audit_Househeeping' )
                {
                    Cases.add(new Case(subject='Site Visit Failed:  corners office ' + b.Location_office__c,Status='New',Origin='email',recordtypeID=b.case_ID__C));
                }
            if(b.vents_office__c == 'Fail' && b.recordtype.name=='DM_of_Ops_Quality_Audit_Househeeping' )
                {
                    Cases.add(new Case(subject='Site Visit Failed:  vents office ' + b.Location_office__c,Status='New',Origin='email',recordtypeID=b.case_ID__C));
                }
            if(b.phones_office__c == 'Fail' && b.recordtype.name=='DM_of_Ops_Quality_Audit_Househeeping' )
                {
                    Cases.add(new Case(subject='Site Visit Failed:  phones office ' + b.Location_office__c,Status='New',Origin='email',recordtypeID=b.case_ID__C));
                }
            if(b.switches_lobbies__c == 'Fail'&& b.recordtype.name=='DM_of_Ops_Quality_Audit_Househeeping'  ) 
                {
                    Cases.add(new Case(subject='Site Visit Failed:  switches lobbies ' + b.Location_lobbies__c,Status='New',Origin='email',recordtypeID=b.case_ID__C));
                }
            if(b.doors_lobbies__c == 'Fail' && b.recordtype.name=='DM_of_Ops_Quality_Audit_Househeeping' )
                {
                    Cases.add(new Case(subject='Site Visit Failed:  doors lobbies ' + b.Location_lobbies__c,Status='New',Origin='email',recordtypeID=b.case_ID__C));
                }
            if(b.furniture_lobbies__c == 'Fail' && b.recordtype.name=='DM_of_Ops_Quality_Audit_Househeeping' )
                {
                    Cases.add(new Case(subject='Site Visit Failed:  furniture lobbies ' + b.Location_lobbies__c,Status='New',Origin='email',recordtypeID=b.case_ID__C));
                }
            if(b.ceiling_lobbies__c == 'Fail' && b.recordtype.name=='DM_of_Ops_Quality_Audit_Househeeping' )
                {
                   Cases.add(new Case(subject='Site Visit Failed:  ceiling lobbies ' + b.Location_lobbies__c,Status='New',Origin='email',recordtypeID=b.case_ID__C));
                }
            if(b.horizontal_surfaces_lobbies__c == 'Fail' && b.recordtype.name=='DM_of_Ops_Quality_Audit_Househeeping' )
                {
                   Cases.add(new Case(subject='Site Visit Failed:  horizontal surfaces lobbies ' + b.Location_lobbies__c,Status='New',Origin='email',recordtypeID=b.case_ID__C));
                }
            if(b.baseboards_lobbies__c == 'Fail' && b.recordtype.name=='DM_of_Ops_Quality_Audit_Househeeping' )
                {
                    Cases.add(new Case(subject='Site Visit Failed:  baseboards lobbies ' + b.Location_lobbies__c,Status='New',Origin='email',recordtypeID=b.case_ID__C));
                }
            if(b.floors_lobbies__c == 'Fail' && b.recordtype.name=='DM_of_Ops_Quality_Audit_Househeeping' )
                {
                    Cases.add(new Case(subject='Site Visit Failed:  floors lobbies ' + b.Location_lobbies__c,Status='New',Origin='email',recordtypeID=b.case_ID__C));
                }
            if(b.corners_lobbies__c == 'Fail' && b.recordtype.name=='DM_of_Ops_Quality_Audit_Househeeping' )
                {
                    Cases.add(new Case(subject='Site Visit Failed:  corners lobbies ' + b.Location_lobbies__c,Status='New',Origin='email',recordtypeID=b.case_ID__C));
                }
            if(b.vents_lobbies__c == 'Fail' && b.recordtype.name=='DM_of_Ops_Quality_Audit_Househeeping' )
                {
                    Cases.add(new Case(subject='Site Visit Failed:  vents lobbies ' + b.Location_lobbies__c,Status='New',Origin='email',recordtypeID=b.case_ID__C));
                }
            if(b.trash_lobbies__c == 'Fail' && b.recordtype.name=='DM_of_Ops_Quality_Audit_Househeeping' )
                {
                    Cases.add(new Case(subject='Site Visit Failed:  trash lobbies ' + b.Location_lobbies__c,Status='New',Origin='email',recordtypeID=b.case_ID__C));
                }
            if(b.phones_lobbies__c == 'Fail' && b.recordtype.name=='DM_of_Ops_Quality_Audit_Househeeping' )
                {
                   Cases.add(new Case(subject='Site Visit Failed:  phones lobbies ' + b.Location_lobbies__c,Status='New',Origin='email',recordtypeID=b.case_ID__C));
                }
            if(b.swtiches_Restrooms_lockerRoom__c == 'Fail' && b.recordtype.name=='DM_of_Ops_Quality_Audit_Househeeping' )
                {
                    Cases.add(new Case(subject='Site Visit Failed:  switches Restroom lockerroom ' + b.Location_restroom_lockerroom__c,Status='New',Origin='email',recordtypeID=b.case_ID__C));
                }
            if(b.sinks_Restroom_lockerroom__c == 'Fail' && b.recordtype.name=='DM_of_Ops_Quality_Audit_Househeeping' )
                {
                    Cases.add(new Case(subject='Site Visit Failed:  sinks Restroom lockerroom ' + b.Location_restroom_lockerroom__c,Status='New',Origin='email',recordtypeID=b.case_ID__C));
                }
            if(b.mirrors_Restrooms_lockerroom__c == 'Fail' && b.recordtype.name=='DM_of_Ops_Quality_Audit_Househeeping' )
                {
                    Cases.add(new Case(subject='Site Visit Failed:  mirrors Restroom lockerroom ' + b.Location_restroom_lockerroom__c,Status='New',Origin='email',recordtypeID=b.case_ID__C));
                }
            if(b.toilets_urinals_restrooms_lockerroom__c == 'Fail' && b.recordtype.name=='DM_of_Ops_Quality_Audit_Househeeping' )
                {
                    Cases.add(new Case(subject='Site Visit Failed:  toilets urinals restroom lockerroom ' + b.Location_restroom_lockerroom__c,Status='New',Origin='email',recordtypeID=b.case_ID__C));
                }
            if(b.walls_partitions_Restroom_lockerroom__c == 'Fail' && b.recordtype.name=='DM_of_Ops_Quality_Audit_Househeeping' )
                {
                    Cases.add(new Case(subject='Site Visit Failed:  walls partitions Restroom lockerroom ' + b.Location_restroom_lockerroom__c,Status='New',Origin='email',recordtypeID=b.case_ID__C));
                }
            if(b.ceilings_Restroom_lockerroom__c == 'Fail' && b.recordtype.name=='DM_of_Ops_Quality_Audit_Househeeping' )
                {
                    Cases.add(new Case(subject='Site Visit Failed:  ceiling Restroom lockerroom ' + b.Location_restroom_lockerroom__c,Status='New',Origin='email',recordtypeID=b.case_ID__C));
                }


             

            }
   insert Cases;
}
John De SantiagoJohn De Santiago
Start with how you are setting the record type name. the RecordType object reference is likely null so when you attempt to do "RecordType.Name" RecordType is null. 

try:
sVisit = new RecordType();
sVisit.RecordType.Name = '';

If you get passed this and there is still an error then it is likely in a trigger associated with your visit object
John De SantiagoJohn De Santiago
Sorry bug in my example:

sVisit.RecordType = new RecordType();
sVisit.RecordType.Name = '';
This was selected as the best answer
GlynAGlynA
@Thomasm,

The trigger won't have the record "b.recordtype" (it will be null).  Instead of comparing b.recordtype.name with a string, do this:

Id desiredRecordTypeID = [SELECT Id FROM RecordType WHERE Name = 'DM_of_Ops_Quality_Audit_Househeeping'][0].Id;

then compare

... && b.recordtypeid == desiredRecordTypeID

-Glyn