+ Start a Discussion
SolidLucasSolidLucas 

NullPoiter Exception help

Hello guys, i'm doing a class test for a error screen that i've created, but it gives me the error NullPointer exception someone could tell me what i'm doing wrong. beacuse when i change my pageReference method to a html address the test covers 80% but when i use my variable retUrl it gives me the error.

My class
public with sharing class ErrorMessage_ctl {	
	
	/****************
	*Public Variables
	*****************/
	private String msg; 		
	private String retURL; 	
	public boolean showButton {get;set;}
	
	/******************
	*Constructor
	******************/

	public ErrorMessage_ctl(){
		msg = System.currentPageReference().getParameters().get('msg');
		retURL = System.currentPageReference().getParameters().get('retURL');
		
		if(functions.isNotEmptyOrNull(msg)){
			ApexPages.addmessage(new ApexPages.message( ApexPages.severity.ERROR, msg ));
		
		}else{
			ApexPages.addmessage(new ApexPages.message( ApexPages.severity.ERROR, 'Ocorreu um erro inesperado!'));
		
		}
		if(functions.isNotEmptyOrNull(retURL)){
			showButton = true;
		
		}else{
			showButton = false;
		
		}
	}
	
	//retorna a página principal
	public PageReference errorReturn(){
		PageReference page = new PageReference(retURL);
		page.setRedirect(true);
		return page;
	}
}

 my test class
 
@isTest
private class ErrorMessage_tst_ctl {

    static testMethod void myUnitTest() {
        ErrorMessage_ctl controlador = new ErrorMessage_ctl();
        
       Test.setCurrentPageReference(new PageReference('msg'));
	   System.currentPageReference().getParameters().get('msg');
		
        //retURL = System.currentPageReference().getParameters().get('retURL');
       
        controlador.showButton = true;
        controlador.showButton = false;
        controlador.errorReturn();
    }
}

 
Best Answer chosen by SolidLucas
MithunPMithunP
Hi SolidLucas,

Try this updated code
 
@isTest
public class ErrorMessage_tst_ctl {
public static testMethod void myUnitTest() {
        msg = System.currentPageReference().getParameters().put('msg', 'test');
        retURL = System.currentPageReference().getParameters().put('retURL', '/001/o');
        ErrorMessage_ctl controlador = new ErrorMessage_ctl();
                    
        controlador.showButton = true;
        controlador.showButton = false;
        controlador.errorReturn();
    }
}

 

All Answers

MithunPMithunP
Hi SolidLucas,

Try this updated code
 
@isTest
public class ErrorMessage_tst_ctl {
public static testMethod void myUnitTest() {
        msg = System.currentPageReference().getParameters().put('msg', 'test');
        retURL = System.currentPageReference().getParameters().put('retURL', '/001/o');
        ErrorMessage_ctl controlador = new ErrorMessage_ctl();
                    
        controlador.showButton = true;
        controlador.showButton = false;
        controlador.errorReturn();
    }
}

 
This was selected as the best answer
SolidLucasSolidLucas
thanks mithunP it works!
MithunPMithunP
I'm glad It helped.

Best Regards,
Mithun.