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
Siva SakthiSiva Sakthi 

Null pointer exception

Hi, 
 
This is my code while creating a portal user i have to checking that user information its showing the error null pointer exception . i highlighted that line. how can i fix this .

public with sharing class SiteRegisterController {
     
   // private static Id PORTAL_ACCOUNT_ID = '001x000xxx35tPN';
    private static Id PORTAL_ACCOUNT_ID;
    public SiteRegisterController () {
    }

    public String username {get; set;}
    public String email {get; set;}
    public String password {get; set {password = value == null ? value : value.trim(); } }
    public String confirmPassword {get; set { confirmPassword = value == null ? value : value.trim(); } }
    public String communityNickname {get; set { communityNickname = value == null ? value : value.trim(); } }
     
    private boolean isValidPassword() {
        return password == confirmPassword;
    }
   
    public PageReference registerUser() {
        // it's okay if password is null - we'll send the user a random password in that case
        if (!isValidPassword()) {
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, Label.site.passwords_dont_match);
            ApexPages.addMessage(msg);
            return null;
        }   
        User u = new User();
        u.Username = username;
        u.Email = email;
        u.CommunityNickname = communityNickname;
       
        String accountId = PORTAL_ACCOUNT_ID;

        // lastName is a required field on user, but if it isn't specified, we'll default it to the username
        String userId = Site.createPortalUser(u, accountId, password);   //*******  This line showing error null pointer exception.
        if (userId != null) {
            if (password != null && password.length() > 1) {
                return Site.login(username, password, null);
            }
            else {
                PageReference page = System.Page.SiteRegisterConfirm;
                page.setRedirect(true);
                return page;
            }
        }
        return null;
    }
}

Advance Thanks
Maheshwar
sfdc_ninjasfdc_ninja
It looks like you commented out the line where you are setting your static accountId variable.  Line 2
Siva SakthiSiva Sakthi
Because this is a hard code id right, no need to hard code that id.then how we can solve this.
Vinita_SFDCVinita_SFDC
Hello,

In String accountId = PORTAL_ACCOUNT_ID;

you are assigning PORTAL_ACCOUNT_ID, but there is no value in PORTAL_ACCOUNT_ID. You can also check the same with debug statements.