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
TemesgenTemesgen 

An unexpected error has occurred: Attempt to de-reference a null object

Hello Everyone,

I am new to development, transitioning from Admin. I am tasked to make the Visualforce Page display content from a custom object called Class__c filtered by record type called cohort, so when I used this syntax in the class:

Id cohortRecordTypeId = Schema.SObjectType.class__c.getRecordTypeInfosByName().get('cohort').getRecordTypeId();

After that, the Visualforce page is not displaying and gave me this error:

Attempt to de-reference a null object

VF Page code:

<apex:page standardController="Class__c" extensions="CohortianSelectorController" standardStylesheets="false" sidebar="false" showHeader="false">
<style type="text/css">
html body {
font-family: 'Raleway', sans-serif;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
font-size: 14px;
text-align: center;
color: black;
background-image: url(https://geekwiseacademy.com/wp-content/uploads/2017/04/homepage-background.jpg);
}
.logo-img {
margin-top: 20px;
height: 70px;
}
.my-btn {
background: none;
background-color: rgb(162, 98, 97);
color: white;
font-size: 16px;
padding: 10px 20px 10px 20px;
border: none;
}
.my-btn:hover {
background-color: rgb(142, 62, 64);
}
.classContainer {
border: 2px solid #A11B12;
margin: 10px;
padding: 5px;
}
.colorText {
color: black;
}
</style>
<head>
<link href="https://fonts.googleapis.com/css?family=Raleway&display=swap" rel="stylesheet" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous" />
</head>
<div class="container-fluid">
<div class="row">
<img src="https://geekwiseacademy.com/wp-content/themes/geekwise_v3/img/geekwise_logo.png" alt="Logo" class="logo-img" />
</div>
<div class="row colorText">
<h2 class="title">Geekwise Academy Instructor Portal</h2>
<p class="sub-title">Select your Class</p>
</div>
<div class="row">
<apex:repeat value="{! class__c}" var="myClass">
<div class="col-md-6">
<div class="classContainer">
<h3>{! myClass.name}</h3>
<apex:outputText value="{0, date, MMMM d',' yyyy}">
<apex:param value="{! myClass.start_date__c}" />
</apex:outputText> -<br/>
<apex:outputText value="{0, date, MMMM d',' yyyy}">
<apex:param value="{! myClass.end_date__c}" />
</apex:outputText> <br/>
<p>{! myClass.class_nights__c}</p>
<apex:form >
<apex:commandLink action="{! newPage}">
<apex:param name="classId" value="{! myClass.Id}" assignTo="{! classId}"/>
<button class="my-btn">View Class</button>
</apex:commandLink>
</apex:form>
</div>
</div>
</apex:repeat>
</div>
</div>
</apex:page>

 

Class Code:

public class CohortianSelectorController {
public List<Class__c> classes{get; set;}
String dayFormat = 'MM/DD';
public Id classId{get; set;}
public string userEmail{get;set;}

public CohortianSelectorController(ApexPages.StandardController sc) {
userEmail = UserInfo.getUserEmail();
getClasses();
}
public void getClasses() {
Id cohortRecordTypeId = Schema.SObjectType.class__c.getRecordTypeInfosByName().get('cohort').getRecordTypeId();
if(userEmail == 'salesforcesupport@shift3tech.com' || userEmail == 'dakina@bitwiseindustries.com' || userEmail == 'agutierrez@shift3tech.com' || userEmail == 'smoreno@geekwiseacademy.com' || userEmail == 'bmily@bitwiseindustries.com' || userEmail == 'ggoforth@bitwiseindustries.com' || userEmail == 'tsolis@bitwiseindustries.com' || userEmail == 'dramos@geekwiseacademy.com' || userEmail == 'iolguin@bitwiseindustries.com' ) {
classes = [ SELECT
id,
name,
class_nights__c,
start_date__c,
end_date__c,
status__c,
class__c,
instructor__r.email
FROM class__c WHERE RecordTypeId = :cohortRecordTypeId AND status__c = 'Active'];
} else {
classes = [ SELECT
id,
name,
class_nights__c,
start_date__c,
end_date__c,
status__c,
class__c,
instructor__r.email
FROM class__c WHERE RecordTypeId = :cohortRecordTypeId AND status__c = 'Active' AND instructor__r.email = :userEmail];
}
}
public Pagereference newPage() {
// Pagereference pf = new Pagereference('/apex/classRoster?id='+ classId);
Pagereference pf = new Pagereference('https://bitwisecommunity.force.com/instructorportal/classroster?id=' + classId);
return pf;
}
}

 

Thank you for your help in Advance.

Best Answer chosen by Temesgen
ShirishaShirisha (Salesforce Developers) 
Hi,

Greetings!

Seems like "cohortRecordTypeId " is causing the issue here due to which the SOQL query is returning the 0 results.

I would suggest you to use System.debug(cohortRecordTypeId);

Also,I would suggest you to check the size of the list before using it.

Reference:https://help.salesforce.com/HTViewSolution?id=000063739

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri

All Answers

ShirishaShirisha (Salesforce Developers) 
Hi,

Greetings!

Seems like "cohortRecordTypeId " is causing the issue here due to which the SOQL query is returning the 0 results.

I would suggest you to use System.debug(cohortRecordTypeId);

Also,I would suggest you to check the size of the list before using it.

Reference:https://help.salesforce.com/HTViewSolution?id=000063739

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
This was selected as the best answer
TemesgenTemesgen

Thank you Shirisa!

That totally helps, what happened is "cohortRecordTypeId " was not referencing the right RecordTypeId, when changed to the correct one, it worked.