+ Start a Discussion
shaker shaikhshaker shaikh 

Unable to find bug in below code: on nullpointer exception

Hi developers,
i have designed a page and its code looks of for and got saved successfully. still i unable to find bug or where i am making mistake
it throws error  -  System.NullPointerException: Attempt to de-reference a null object - on line 28 column 1
  request you to look into this code and help me please :

public class TravelPortalSearchController {
  public String searchTerm{get;set;}
    public String fromPrice{get;set;}
    public String toPrice{get;set;}
    public List<Travel_Packages__c> travelPackages{get;set;}
    public List<TravelPackageWraper> travelPackageWrapper{get;set;}
    public Contact loggedInUser{get;set;}
    public List<User_Favorites__c> userFavList{get;set;}
    public String loggedInUserName{get;set;}
    public Integer favoriteCount{get;set;}
    public Set<Id> favoriteIds = new Set<Id>();
    
    public TravelPortalSearchController(){
        travelPackages = new List<Travel_Packages__c>();
        travelPackageWrapper = new List<TravelPackageWraper>();
        loggedInUser=(Contact)Cache.Session.get('LoggedInUser');
        userFavList=(List<User_Favorites__c>)Cache.Session.get('UserFavorites');
        if(loggedInUser!=null){
            loggedInUserName=loggedInUser.firstName+' '+loggedInUser.lastName;
        }else{
            loggedInUserName='';
        }
        if(userFavList!=null){
            favoriteCount=userFavList.size();
        }else{
            favoriteCount=0;
        }
        for(User_Favorites__c favObj: userFavList){
            favoriteIds.add(favObj.Travel_Packages__c);
        }
    }
    
    public PageReference searchTravelPackages(){
        travelPackageWrapper = new List<TravelPackageWraper>();
        Decimal fromPriceNum = 0;
        Decimal toPriceNum = 0;
        if(fromPrice!=null && fromPrice.isNumeric()){
            fromPriceNum=Decimal.valueOf(fromPrice);
        }
        if(toPrice!=null && toPrice.isNumeric()){
            toPriceNum=Decimal.valueOf(toPrice);
        }
        travelPackages = TravelPackageDAO.fetchTravelPackages(searchTerm, fromPriceNum, toPriceNum);
        for(Travel_Packages__c tPackage : travelPackages){
           Boolean isFavorites = favoriteIds.contains(tPackage.id);
           travelPackageWrapper.add(new TravelPackageWraper(tPackage,isFavorites));
        }
        return null;
    }
    
    public PageReference addOrRemoveFavorite(){
        System.debug('addToFavorite');
        System.debug('loggedInUser: '+loggedInUser);
        String packageId =  ApexPages.currentPage().getParameters().get('packageId');
        System.debug('packageId');
        if(!favoriteIds.contains(packageId)){
            UserFavoriteDAO.insertUserFavorite(loggedInUser.id, packageId);
            favoriteCount+=1;
        }else{
            UserFavoriteDAO.removetUserFavorite(loggedInUser.id, packageId);
            favoriteCount-=1;
        }
        userFavList = UserFavoriteDAO.fetchFavorites(loggedInUser);
        favoriteIds=new Set<Id>();
        for(User_Favorites__c favObj: userFavList){
            favoriteIds.add(favObj.Travel_Packages__c);
        }
        travelPackageWrapper = new List<TravelPackageWraper>();
        for(Travel_Packages__c tPackage : travelPackages){
           Boolean isFavorites = favoriteIds.contains(tPackage.id);
           travelPackageWrapper.add(new TravelPackageWraper(tPackage,isFavorites));
        }
        return null;
    }
    public class TravelPackageWraper{
        public Travel_Packages__c tPackage{get;set;}
        public Boolean isFavorite{get;set;}
        
        public TravelPackageWraper(){
            
        }
        
        public TravelPackageWraper(Travel_Packages__c tPackage,Boolean isFavorite){
            this.tPackage=tPackage;
            this.isFavorite=isFavorite;
        }
        
    }
}

VF Page - 

<apex:page showHeader="false" sidebar="false" Controller="TravelPortalSearchController">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection columns="2">
     
                <apex:image url="{!URLFOR($Resource.TPImages, 'logo1.jpg')}" width="100" height="100" />                   
                
                <font color='Blue' size='6'>Demo Travel Portal</font>
        
            </apex:pageBlockSection>
            <apex:pageBlockSection columns="3" id="headerSection">
                <font size='2'>Welcome {!loggedInUserName} !!!</font>
    
             <apex:image url="{!URLFOR($Resource.TPImages, 'empty.png')}" width="50" height="50" rendered="{!favoriteCount==0}"/>
             <apex:image url="{!URLFOR($Resource.TPImages, 'filled.png')}" width="50" height="50" rendered="{!favoriteCount>0}"/>

                  <font size='4'>Favorite Count: {!favoriteCount}</font>
    
            </apex:pageBlockSection>

            <apex:pageBlockSection columns="7">
                Search Term<apex:inputText value="{!searchTerm}" />
                From Price<apex:inputText value="{!fromPrice}" />
                To Price<apex:inputText value="{!toPrice}" />
                <apex:commandButton value="Search" action="{!searchTravelPackages}"/>       
            </apex:pageBlockSection>

            <apex:pageBlockSection columns="1" id="resultSection">
                <apex:pageBlockTable var="tp" value="{!travelPackageWrapper}">
                    <apex:column value="{!tp.tPackage.Location__c}"/>
                    <apex:column value="{!tp.tPackage.Number_of_Days__c}"/>
                    <apex:column value="{!tp.tPackage.Price__c}"/>
                    <apex:column >
                        <apex:commandLink action="{!addOrRemoveFavorite}" reRender="resultSection,headerSection">
                            <apex:image url="{!URLFOR($Resource.TPImages, 'empty.png')}" width="25" height="25"  rendered="{!(!tp.isFavorite)}"/>
                            <apex:image url="{!URLFOR($Resource.TPImages, 'filled.png')}" width="25" height="25"  rendered="{!tp.isFavorite}"/>

                            <apex:param name="packageId" value="{!tp.tPackage.id}"/>                                
                        </apex:commandLink>                            
                    </apex:column>

                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Appriciate for your time

thank you.




 
shaker shaikhshaker shaikh
Line Nu. 28 for above code - in TravelPortalSearchController 

for(User_Favorites__c favObj: userFavList){
            favoriteIds.add(favObj.Travel_Packages__c);
Abdul KhatriAbdul Khatri
based what you provided regarding the line number 28, just change the constructor code to this. If this doesn't work please send the log with the complete message for trouble  shoot. If helped please mark this best answer.
 
public TravelPortalSearchController(){
        travelPackages = new List<Travel_Packages__c>();
        travelPackageWrapper = new List<TravelPackageWraper>();
        loggedInUser=(Contact)Cache.Session.get('LoggedInUser');
        userFavList=(List<User_Favorites__c>)Cache.Session.get('UserFavorites');
        if(loggedInUser!=null){
            loggedInUserName=loggedInUser.firstName+' '+loggedInUser.lastName;
        }else{
            loggedInUserName='';
        }
        if(userFavList!=null){
            favoriteCount=userFavList.size();
        }else{
            favoriteCount=0;
        }
        
        if(userFavList != null)
        {
        	for(User_Favorites__c favObj: userFavList){
	            favoriteIds.add(favObj.Travel_Packages__c);
    	    }
        }
    }

 
shaker shaikhshaker shaikh
Thank you Abdul Khatri for looking into code, But it seems same code and paste code that you posted. but did not help to remove error
as you asked for message  here it is - 

System.NullPointerException: Attempt to de-reference a null object 
Class.TravelPortalSearchController.<init>: line 28, column 1


how can i send you log?
shaker shaikhshaker shaikh
the complete log

41.0 APEX_CODE,FINEST;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WAVE,INFO;WORKFLOW,INFO
11:07:47.0 (314224)|USER_INFO|[EXTERNAL]|0057F000000M2iu|shaker02@yahoo.com|Pacific Standard Time|GMT-07:00
11:07:47.0 (358245)|EXECUTION_STARTED
11:07:47.0 (363879)|CODE_UNIT_STARTED|[EXTERNAL]|0667F000003EaVC|VF: /apex/travelPortalSearchView
11:07:47.0 (59057339)|SYSTEM_MODE_ENTER|true
11:07:47.0 (59354655)|HEAP_ALLOCATE|[72]|Bytes:3
11:07:47.0 (59382986)|HEAP_ALLOCATE|[77]|Bytes:152
11:07:47.0 (59396626)|HEAP_ALLOCATE|[342]|Bytes:408
11:07:47.0 (59411669)|HEAP_ALLOCATE|[355]|Bytes:408
11:07:47.0 (59426140)|HEAP_ALLOCATE|[467]|Bytes:48
11:07:47.0 (59452169)|HEAP_ALLOCATE|[139]|Bytes:6
11:07:47.0 (59465784)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:19
11:07:47.0 (59470910)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:300
11:07:47.0 (59474394)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:27
11:07:47.0 (59478209)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
11:07:47.0 (59490653)|METHOD_ENTRY|[1]|01p7F00000ARvAN|TravelPortalSearchController.TravelPortalSearchController()
11:07:47.0 (59496632)|STATEMENT_EXECUTE|[1]
11:07:47.0 (59500612)|STATEMENT_EXECUTE|[1]
11:07:47.0 (59501858)|STATEMENT_EXECUTE|[75]
11:07:47.0 (59508204)|METHOD_EXIT|[1]|TravelPortalSearchController
11:07:47.0 (59754066)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:44
11:07:47.0 (59835322)|VARIABLE_SCOPE_BEGIN|[13]|this|TravelPortalSearchController|true|false
11:07:47.0 (59910482)|VARIABLE_ASSIGNMENT|[13]|this|{}|0x39dd4880
11:07:47.0 (59920004)|STATEMENT_EXECUTE|[1]
11:07:47.0 (59921346)|STATEMENT_EXECUTE|[2]
11:07:47.0 (59922316)|STATEMENT_EXECUTE|[3]
11:07:47.0 (59923642)|STATEMENT_EXECUTE|[4]
11:07:47.0 (59924596)|STATEMENT_EXECUTE|[5]
11:07:47.0 (59925484)|STATEMENT_EXECUTE|[6]
11:07:47.0 (59926413)|STATEMENT_EXECUTE|[7]
11:07:47.0 (59927334)|STATEMENT_EXECUTE|[8]
11:07:47.0 (59928230)|STATEMENT_EXECUTE|[9]
11:07:47.0 (59929131)|STATEMENT_EXECUTE|[10]
11:07:47.0 (59930191)|STATEMENT_EXECUTE|[11]
11:07:47.0 (59939125)|HEAP_ALLOCATE|[11]|Bytes:4
11:07:47.0 (60032144)|VARIABLE_ASSIGNMENT|[11]|this.favoriteIds|[]|0x39dd4880
11:07:47.0 (60045550)|HEAP_ALLOCATE|[11]|Bytes:6
11:07:47.0 (60100170)|STATEMENT_EXECUTE|[13]
11:07:47.0 (60101842)|STATEMENT_EXECUTE|[14]
11:07:47.0 (60121881)|HEAP_ALLOCATE|[14]|Bytes:4
11:07:47.0 (60204334)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this|{"favoriteIds":"0x1495cffe"}|0x39dd4880
11:07:47.0 (60216016)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4
11:07:47.0 (60223960)|VARIABLE_ASSIGNMENT|[EXTERNAL]|value|[]|0x2d1c8cd2
11:07:47.0 (60233985)|VARIABLE_ASSIGNMENT|[5]|this.travelPackages|[]|0x39dd4880
11:07:47.0 (60242505)|STATEMENT_EXECUTE|[15]
11:07:47.0 (60293906)|HEAP_ALLOCATE|[15]|Bytes:4
11:07:47.0 (60366142)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this|{"favoriteIds":"0x1495cffe","travelPackages":"0x2d1c8cd2"}|0x39dd4880
11:07:47.0 (60386906)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4
11:07:47.0 (60394231)|VARIABLE_ASSIGNMENT|[EXTERNAL]|value|[]|0x531e4450
11:07:47.0 (60404181)|VARIABLE_ASSIGNMENT|[6]|this.travelPackageWrapper|[]|0x39dd4880
11:07:47.0 (60412258)|STATEMENT_EXECUTE|[16]
11:07:47.0 (60415614)|HEAP_ALLOCATE|[16]|Bytes:12
11:07:47.0 (60448539)|VARIABLE_SCOPE_BEGIN|[13]|Session.MAX_TTL_SECS|Integer|false|true
11:07:47.0 (60461358)|HEAP_ALLOCATE|[16]|Bytes:52
11:07:47.0 (60466513)|HEAP_ALLOCATE|[16]|Bytes:72
11:07:47.0 (60477136)|SYSTEM_METHOD_ENTRY|[11]|Session.Session()
11:07:47.0 (60481164)|STATEMENT_EXECUTE|[11]
11:07:47.0 (60497114)|SYSTEM_METHOD_EXIT|[11]|Session
11:07:47.0 (60512298)|METHOD_ENTRY|[16]||cache.Session.get(String)
11:07:47.0 (119557046)|METHOD_EXIT|[16]||cache.Session.get(String)
11:07:47.0 (119664537)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this|{"favoriteIds":"0x1495cffe","travelPackages":"0x2d1c8cd2","travelPackageWrapper":"0x531e4450"}|0x39dd4880
11:07:47.0 (119688234)|VARIABLE_ASSIGNMENT|[EXTERNAL]|value|null|
11:07:47.0 (119704194)|VARIABLE_ASSIGNMENT|[7]|this.loggedInUser|null|0x39dd4880
11:07:47.0 (119718900)|STATEMENT_EXECUTE|[17]
11:07:47.0 (119726066)|HEAP_ALLOCATE|[17]|Bytes:13
11:07:47.0 (119743660)|METHOD_ENTRY|[17]||cache.Session.get(String)
11:07:47.0 (120507428)|METHOD_EXIT|[17]||cache.Session.get(String)
11:07:47.0 (120563362)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this|{"favoriteIds":"0x1495cffe","travelPackages":"0x2d1c8cd2","travelPackageWrapper":"0x531e4450"}|0x39dd4880
11:07:47.0 (120577555)|VARIABLE_ASSIGNMENT|[EXTERNAL]|value|null|
11:07:47.0 (120586415)|VARIABLE_ASSIGNMENT|[8]|this.userFavList|null|0x39dd4880
11:07:47.0 (120635632)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this|{"favoriteIds":"0x1495cffe","travelPackages":"0x2d1c8cd2","travelPackageWrapper":"0x531e4450"}|0x39dd4880
11:07:47.0 (120647545)|STATEMENT_EXECUTE|[20]
11:07:47.0 (120649060)|STATEMENT_EXECUTE|[21]
11:07:47.0 (120696938)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this|{"favoriteIds":"0x1495cffe","travelPackages":"0x2d1c8cd2","travelPackageWrapper":"0x531e4450"}|0x39dd4880
11:07:47.0 (120705977)|VARIABLE_ASSIGNMENT|[EXTERNAL]|value|""
11:07:47.0 (120714663)|VARIABLE_ASSIGNMENT|[9]|this.loggedInUserName|""|0x39dd4880
11:07:47.0 (120754409)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this|{"favoriteIds":"0x1495cffe","loggedInUserName":"","travelPackages":"0x2d1c8cd2","travelPackageWrapper":"0x531e4450"}|0x39dd4880
11:07:47.0 (120765314)|STATEMENT_EXECUTE|[25]
11:07:47.0 (120766589)|STATEMENT_EXECUTE|[26]
11:07:47.0 (120807318)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this|{"favoriteIds":"0x1495cffe","loggedInUserName":"","travelPackages":"0x2d1c8cd2","travelPackageWrapper":"0x531e4450"}|0x39dd4880
11:07:47.0 (120818289)|VARIABLE_ASSIGNMENT|[EXTERNAL]|value|0
11:07:47.0 (120823510)|HEAP_ALLOCATE|[10]|Bytes:4
11:07:47.0 (120829878)|VARIABLE_ASSIGNMENT|[10]|this.favoriteCount|0|0x39dd4880
11:07:47.0 (120863189)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this|{"favoriteCount":0,"favoriteIds":"0x1495cffe","loggedInUserName":"","travelPackages":"0x2d1c8cd2","travelPackageWrapper":"0x531e4450"}|0x39dd4880
11:07:47.0 (120920510)|HEAP_ALLOCATE|[28]|Bytes:41
11:07:47.0 (121106139)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object

Class.TravelPortalSearchController.<init>: line 28, column 1
11:07:47.200 (200731205)|CUMULATIVE_LIMIT_USAGE
11:07:47.200 (200731205)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

11:07:47.200 (200731205)|CUMULATIVE_LIMIT_USAGE_END

11:07:47.0 (200796615)|CODE_UNIT_FINISHED|VF: /apex/travelPortalSearchView
11:07:47.0 (201536120)|EXECUTION_FINISHED
 
Abdul KhatriAbdul Khatri
No Code is not the same please check below. Did you try?

User-added image
Abdul KhatriAbdul Khatri
Did you give a try? 
shaker shaikhshaker shaikh
Yes i tried, code got saved but i unable to fetch List of travel_packages__C i mean from Search Button 
<apex:commandButton value="Search" action="{!searchTravelPackages}"/>       
please let me know 
Abdul KhatriAbdul Khatri
So you are saying that when you click that button you get this null issue which from my perpective should not be coming now.

If you are not getting the results pelase make sure you have availabe information for the filter you are trying.

Please let me know what exactly happening.
shaker shaikhshaker shaikh
please go through the mentioned steps - 
1. there is form with 4 fields FirstName,Lastname, Email,and phone and a button which take me to 2nd page
    where there are 3 field Location , fromprice, and to price and one button 'SEARCH'
    now - when i click on SEARCH it suppose to give me all Travel package's , and if i put any city name in Location__C it should give that result by clicking on SEARCH thats requirment

and what happening here with provided code
i can fill the form ,
i can enter to 2nd page,
here if i click on SEARCH button it doesnt give any record even if put some record name in location field and try to search, its not happening as i expected .

please go through the above code once to get the detail of fiald names, or objects so you can get flow 

thank you again for looking into this 

LET ME KNOW IF YOU REQUIRE ANY CODE BLOCK OR INIFORMATION 
shaker shaikhshaker shaikh
@Abdul Khtri
 have you seen above mentioned steps, which i am going to and getting stucked.
please let me know for the more 
Abdul KhatriAbdul Khatri
Sorry I was caught up with the few things iincluding Dreamforce. Wante to confirm if everything fine now. If not let's handle later issue in a different thread. First let us know if your null issue gone or not? 
Abdul KhatriAbdul Khatri
Hey Shaker,

Please do me favor in getting this resolve so everyone can benefit from this. Is everything fixed. Was the solution helpful?