Play Games

Search This Blog

Friday, December 11, 2015

Invalid integer:abc.An unexpected error has occured.Your development organization has been notified.

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 && accountList[0].AccountNumber != null) {
            intValue = Integer.valueof(accountList[0].AccountNumber);
        }
    }
}
Visualforce Page:
<apex:page controller="ArgumentNotNull">
The integer value :{!intValue }
</apex:page>
Problem: In the example above, the "accountList[0].AccountNumber" returns string value and we are trying to convert it into Integer.
Solution : First check whether the value is string or integer,then we need to typecast if necessary.
Modify the above class like this.
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