2

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 an error:

"System.NullPointerException: Attempted to upsert a null list"

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!!

1

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
}
2
  • Thank you so much! that was it! I can't believe it was something so simple... Thank you! May 16 '16 at 17:23
  • Glad I could help. :)
    – Adrian Larson
    May 16 '16 at 17:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.