Example :
Apex Class :
public class ArgumentNotNull {
public Integer intValue {get;set;}
public ArgumentNotNull () {
List <Account> accountList = [select id,name,AccountNumber from Account where id=:'00128000003yhTi'];
if(accountList != null) {
intValue = Integer.valueof(accountList[0].AccountNumber);
}
}
}
Visualforce Page :
<apex:page controller="ArgumentNotNull">
The integer value :{!intValue }
</apex:page>
Assume you didnot enter any value in accountnumber field of Account with record id "00128000003yhTi".
In the line "intValue = Integer.valueof(accountList[0].AccountNumber);" since accountList[0].Accountnumber value is null,We are getting the above error.
Solution: We need to add null check before going to the above line.
Modify the above apex class as:
public class ArgumentNotNull {
public Integer intValue {get;set;}
public ArgumentNotNull () {
List <Account> accountList = [select id,name,AccountNumber from Account where id=:'00128000003yhTi'];
if(accountList != null && accountList[0].AccountNumber != null) {
boolean isNonNumericValue = pattern.matches('[a-zA-Z]+',accountList[0].AccountNumber);
if(isNonNumericValue == false) {
intValue = Integer.valueof(accountList[0].AccountNumber);
}
}
}
}
Apex Class :
public class ArgumentNotNull {
public Integer intValue {get;set;}
public ArgumentNotNull () {
List <Account> accountList = [select id,name,AccountNumber from Account where id=:'00128000003yhTi'];
if(accountList != null) {
intValue = Integer.valueof(accountList[0].AccountNumber);
}
}
}
Visualforce Page :
<apex:page controller="ArgumentNotNull">
The integer value :{!intValue }
</apex:page>
Assume you didnot enter any value in accountnumber field of Account with record id "00128000003yhTi".
In the line "intValue = Integer.valueof(accountList[0].AccountNumber);" since accountList[0].Accountnumber value is null,We are getting the above error.
Solution: We need to add null check before going to the above line.
Modify the above apex class as:
public class ArgumentNotNull {
public Integer intValue {get;set;}
public ArgumentNotNull () {
List <Account> accountList = [select id,name,AccountNumber from Account where id=:'00128000003yhTi'];
if(accountList != null && accountList[0].AccountNumber != null) {
boolean isNonNumericValue = pattern.matches('[a-zA-Z]+',accountList[0].AccountNumber);
if(isNonNumericValue == false) {
intValue = Integer.valueof(accountList[0].AccountNumber);
}
}
}
}
No comments:
Post a Comment