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
tantoniotantonio 

Cannot get test class to work -- null pointer exception

Hello All,

 

I need help figuring out why this test class is failing. Thanks for the help!!!

 

Error:

Class	listorgtest
Method Name	testorg
Pass/Fail	Fail
Error Message	System.NullPointerException: Attempt to de-reference a null object
Stack Trace	Class.listorgtest.testorg: line 13, column 1

 Test Class:

@istest
public class listorgtest{
    public static testmethod void testorg(){
        
        //instantiage a page
        pagereference pageref = page.loginhome;
        test.setcurrentpage(pageref);
        
        //construct controller class
        listorgs lo = new listorgs();
        
        //getting url of login
        string savepage = lo.save().geturl();
        
        //checking login url
        system.assertequals('apex/LoginHome?sfdc.tabName=01rd00000007LkS', savepage);
	}
}

 Controller class:

Public class listorgs  implements iterator<org__c>{
        public List<org__c> newlist {get; set;}
        Integer i {get; set;}
        public ApexPages.StandardController listorgs {get; set;}
        public listorgs(ApexPages.StandardController controller) {
            listorgs = controller;}

        
        public listorgs(){
            newlist = [select id, name , LoginURL__c , HRM_Installed__c, Description__c , Type__c, 
                       Pod__c, org_id__c, Version_Password__c, Deprecated__c
                       from org__c order by type__c asc];
            i = 0;
                }
        Public boolean hasnext(){
            if(i>= newlist.size()){
                return false;
            } else {
                return true;
            }
        }
        Public Org__c next(){
            if(i==100){return null;}
            i++;
            return newlist[i-1];
        }
        public string page{get;set;}
        public string openpageurl {get;set;}
                
        public void onload()
        {
            page = '';
            openpageurl = '';
        }
        //Below code is not being used for login functionality.
        //public pagereference redirect()
      //  {
          // if( page == 'login')
          //  {
          //      openpageurl = 'http://google.com';
          //  }
         //   return null;
      //  }
   	 //save button
            public pagereference save() { 
            update newlist; 
            return null; 
        }       

}

 

liron169liron169

Hello,

 

string savepage = lo.save().geturl();

 

The function save() return null in your case, so you cannot call function getUrl() on null.

 

change it to:

 

string savepage = lo.save();
tantoniotantonio

Good call liron169. I started this test class as a test for the a url link on my class but decided to start with the save because I thought it would be easier.

 

I've made the suggested change and now I am getting a problem: Illegal assignment from System.PageReference to string. I might need to change string [string savepage = lo.save()] to something else, but not sure what...?

 

@istest
public class listorgtest{
    public static testmethod void testorg(){
        
        //instantiage a page
        pagereference pageref = page.loginhome;
        test.setcurrentpage(pageref);
        
        //construct controller class
        listorgs lo = new listorgs();
        
        //getting url of save
        string savepage = lo.save();
        
        //checking login url
        system.assertequals('apex/LoginHome?sfdc.tabName=01rd00000007LkS', savepage);
	}
}

 

liron169liron169
Then you can write:

PageReference savePage = lo.save();