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
Logan Smith 10Logan Smith 10 

Trouble with PageReference testing

I have a very simple extension to a custom object that I use for three reasons: 1, so make sure that the user's custom profile is being utilized, 2, the classification is set on this specific VF page and 3, the command buttons are specific to the VF page being used.
My Controller:
public class EntryExtension {

public Journal_Entry__c entry{get; set;}

public EntryExtension (ApexPages.StandardController stdController){

List<Contact> users = [Select JournalProfileID__c From Contact Where Id In
(Select ContactId From User Where Id = :UserInfo.getUserId()) limit 1];

if (!users.isEmpty()) {
    entry = (Journal_Entry__c)stdController.getRecord();
    entry.Journal_Profile__c = users.get(0).JournalProfileID__c;
    entry.Classification__c = 'TestClass';
} 

    Id recordId = stdController.getId();
if(recordId == null){
    entry.ShowTrueFalse__c = 'ShowTrue';
    entry.ShowTrueFalse2__c = 'ShowFalse';
}
}
public PageReference saveID(){

    upsert entry;
    PageReference reRend = new PageReference('/TheEntry');
    reRend.getParameters().put('id',entry.Id);
    reRend.setRedirect(true);
    return reRend;        
}


public PageReference newSaveSelf(){

    upsert entry; 
    PageReference reRend = new PageReference('/SelfInterpretation');
    reRend.getParameters().put('journalEntry',entry.Id);
    reRend.setRedirect(true);
    return reRend;        
}
public PageReference saveSelf(){

String journalEntryID = ApexPages.currentPage().getParameters().get('id');
List<Journal_Entry__c> checkVis = [Select Self_Interp__c, SelfID__c From Journal_Entry__c Where Id = :journalEntryID limit 1];
String finalCheck =  checkVis.get(0).SelfID__c;

    upsert entry;
    PageReference reRend = new PageReference('/SelfInterpretation');
    reRend.getParameters().put('id',finalCheck);
    reRend.setRedirect(true);
    return reRend;        

}    

public PageReference SubmitForInterp(){

    upsert entry; 
    PageReference reRend = new PageReference('/SaleData');
    reRend.getParameters().put('journalEntry',entry.Id);
    reRend.setRedirect(true);
    return reRend;        

}
}

This is my Test Controller which is giving me a "System.NullPointerException: Attempted to upsert a null list" error
Class.EntryExtension.saveID: line 26, column 1 Class.TestEntryExtension.test: line 24, column 1
Obviously, I would want the other page references to be tested as well, but I can't even seem to get the one to work...
@isTest (seealldata=true)
public class TestEntryExtension{

public static testMethod void test() {

Journal_Entry__c entry = new Journal_Entry__c();
List<Contact> users = [Select JournalProfileID__c From Contact Where FirstName = 'Test' limit 1];

    entry.Journal_Profile__c = users.get(0).JournalProfileID__c;
    entry.Classification__c = 'TestClass';
    entry.Name = 'Test Tests';
    entry.ShowTrueFalse__c = 'ShowTrue';
    entry.ShowTrueFalse2__c = 'ShowFalse';
    insert entry;

PageReference pref = Page.TheEntry;
pref.getParameters().put('id', entry.id);
Test.setCurrentPage(pref);


ApexPAges.StandardController sc = new ApexPages.StandardController(entry);
EntryExtension testController = new EntryExtension(sc);

PageReference result = testController.saveID();
System.assertNotEquals(null, result);


//        testController.newSaveSelf();
//        testController.saveSelf();
//        testController.SubmitForInterp();

   system.assert(testController.entry.Id != null);
        apexPages.Currentpage().getParameters().put('Id',entry.id);

}
}


I greatly appreciate any help I can get!!
Best Answer chosen by Logan Smith 10
Logan Smith 10Logan Smith 10
OK, this was a very simple mistake, but one that someone on the Stack Exchange pointed out for me... This was their answer:

Notice that you have your instantiation inside an if block:
if (!users.isEmpty()) { 
entry = (Journal_Entry__c)stdController.getRecord(); 
// other logic 
}
The above means that sometimes entry will be null. It seems like you should make the instantiation unconditional and only keep the other logic in your if block.
entry = (Journal_Entry__c)stdController.getRecord(); 
if (!users.isEmpty()) { 
// other logic
 }

Hope this helps someone else...