0

In my constructor I'm declaring as

pagenm = ApexPages.currentPage().getUrl().substringbetween('apex/','?'); 

And in the action button I'm writing these lines

if(pagenm = 'Page1'){
   // some code here
}else if(pagenm = 'Page2'){
  // some code here
}

Now I need to cover these lines in Apex class.I'm getting

System.NullPointerException: Attempt to de-reference a null object

at pagenm in constructor. How should I put the url in test class.

3

In Test Class you need to set the current page and put the parameters that you want in the url.

For e.g.

Test.startTest();
   PageReference myVfPage = Page.YOUR_VF_PAGE;
   myVfPage.getParameters().put('id',SOME_ID);
   myVfPage.getParameters().put('param2','TestContact123');
   Test.setCurrentPage(myVfPage); 

   String id = ApexPages.currentPage().getParameters().get('id');
Test.stopTest();

In above case add the parameter that you require in your code to successfully execute the business logic

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.