+ Start a Discussion
sreedharp286sreedharp286 

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

hi 

i am trying to send below code as package

 

public  class calculator1{

public integer num1{get;set;}
public integer num2{get;set;}
public integer num3{get;set;}

 
 
public void add(){
num3=num1+num2;
//EmailConformation1 E=new EmailConformation1();
//E.sendMail();
}
}

 

 

but getting  error 

Class.calculator1.add: line 10, column 1 Class.MyTestClass34.test11: line 13, column 1

 

 Class.calculator1.add: line 10, column 1 Class.MyTestClass34.test11: line 13, column 1

 

 

please help me quickly

jgaiottojgaiotto

Looks like to me that this error occurs because you are trying to access a non-set variables, so their values are null.

 

You can do this :

public Integer num1 {
    get {
        if(num1 == null) {
            return 0;
        }
        else {
            return num1;
        }
    }
set; }

 and the same for the other variables.

sony2503@gmail.comsony2503@gmail.com

Hi,

 

Please try the following code.

 

public  class calculator1{

public integer num1{get;set;}
public integer num2{get;set;}
public integer num3{get;set;}

public calculator1(){

        num1=0;

        num2 = 0;

        num3=0;

}
 
 
public void add(){
num3=num1+num2;
//EmailConformation1 E=new EmailConformation1();
//E.sendMail();
}
}

Thanks,

Soni