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
Michael M.Michael M. 

System.currentpagereference() returns null

Hello Community,

 

i have a confusing issue with the system.currentpagereference() method. First of all, my question is: is there any situation of condition that has to be considered to ensure that system.currentpagereference() does not return null.

 

I use this in a own visualforce page with custom controller in connection with sites.

 

Best regards,

Michael

AmitSahuAmitSahu

What exactly you want to do ? system.currentpagerefernce() gives you null.... but what if you try .getparameters('id') with that or any member methods for that matter?

Michael M.Michael M.

Thank you for your quick response.

 

I use this method to set the startURL for the login mechanism for Sites. The point is, if I receives null from currentPageReference(), I will get a NPE when I call a method on the null value. That was my experience.

 

Here my login method which is called via commandbutton on a visualforce page via Sites:

global PageReference login() {
        String startUrl = Page.page.getUrl()+'?pageid=' + System_Settings.homepageid;

        if (!system.currentPageReference().getParameters().isEmpty() && system.currentPageReference().getParameters().containsKey('pid')) {
            startUrl=Page.page.getUrl() + '?pageid=' + System.currentPageReference().getParameters().get('pid');
        }
        PageReference loginPageRef=Site.login(username, password, startUrl);
        
        if (ApexPages.hasMessages()) {
            handleLoginErrorMessage=true;
            return null;
        }
        return loginPageRef;
    }         

 The if Condition causes the NullPointerException... Anyone an idea why this happens? Maybe following information helps: I checked in the controller the value of system.currentPageReference() and there it is not null, but in the whole login method it returns null...

Michael M.Michael M.

It seems that the problem is the combination of using system.currentPageReference() with global class declarations. The exception shows following: System.currentPageReference called without context

 

Why my visualforce page lose its context?

CrocketCrocket

Did you ever resolve this? I am suddently seeing it and have not been able to determine what was changed to cause it.