TypeConverter null reference exception

Posts   
 
    
jovball
User
Posts: 443
Joined: 23-Jan-2005
# Posted on: 03-Oct-2011 15:02:41   

I created a CharTrimTypeConverter to auto-trim CHAR fields. It is working correctly except when the field value is null. At that point I get a null reference exception in SD.LLBLGen.Pro.ORMSupportClasses.FieldUtilities on this line

value = persistenceInfo.TypeConverterToUse.ConvertFrom(null, null, value);

The exception detail is below and I have attached my typeconverter class. It looks to me as if the FieldUtilities DetermineValueToSet method should handle the null values (value == DBNull.Value on line 109).

What am I missing?


System.NullReferenceException was unhandled by user code
  Message=Object reference not set to an instance of an object.
  Source=SD.LLBLGen.Pro.TypeConverters
  StackTrace:
       at SD.LLBLGen.Pro.TypeConverters.CharTrimConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
       at SD.LLBLGen.Pro.ORMSupportClasses.FieldUtilities.DetermineValueToSet(IEntityFieldCore fieldToSet, IFieldPersistenceInfo persistenceInfo, Object currentValue, Boolean& isConverted) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.1\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\Miscellaneous\FieldUtilities.cs:line 124
       at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.ReadRowIntoFields(Object[] values, IEntityFields2 rowDestination, List`1 fieldIndexToOrdinal, IFieldPersistenceInfo[] fieldsPersistenceInfo) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.1\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\AdapterSpecific\DataAccessAdapterBase.cs:line 5054
       at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.FetchOneRow(IDataReader dataSource, IEntityFields2 rowDestination, IFieldPersistenceInfo[] fieldsPersistenceInfo) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.1\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\AdapterSpecific\DataAccessAdapterBase.cs:line 5022
       at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.ExecuteSingleRowRetrievalQuery(IRetrievalQuery queryToExecute, IEntityFields2 fieldsToFill, IFieldPersistenceInfo[] fieldsPersistenceInfo) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.1\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\AdapterSpecific\DataAccessAdapterBase.cs:line 567
       at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.FetchEntityUsingFilter(IEntityFields2 fieldsToFetch, IFieldPersistenceInfo[] persistenceInfos, IRelationPredicateBucket filter) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.1\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\AdapterSpecific\DataAccessAdapterBase.cs:line 4892
       at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.FetchEntityUsingFilter(IEntity2 entityToFetch, IPrefetchPath2 prefetchPath, Context contextToUse, IRelationPredicateBucket filter, ExcludeIncludeFieldsList excludedIncludedFields) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.1\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\AdapterSpecific\DataAccessAdapterBase.cs:line 4829
       at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.FetchEntityUsingUniqueConstraint(IEntity2 entityToFetch, IPredicateExpression uniqueConstraintFilter, IPrefetchPath2 prefetchPath, Context contextToUse, ExcludeIncludeFieldsList excludedIncludedFields) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.1\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\AdapterSpecific\DataAccessAdapterBase.cs:line 1921
       at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.FetchEntityUsingUniqueConstraint(IEntity2 entityToFetch, IPredicateExpression uniqueConstraintFilter) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.1\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\AdapterSpecific\DataAccessAdapterBase.cs:line 1850
       at DCSE.iAPECS.BusinessLogic.FcrEmployerEntityBL.FetchOneEntityWithFilter(IPredicateExpression filterExpression) in C:\VS-Projects\DCSE.iAPECS-StarterSolution\DCSE.iAPECS.BusinessLogic\EntityBL\Generated\FcrEmployerEntityBL.vb:line 174
       at DCSE.iAPECS.Web.UI.NDNHParticipantEmployerDetail.GetEmployerAddress(String fein) in C:\VS-Projects\DCSE.iAPECS-StarterSolution\DCSE.iAPECS.Web.UI\FCR\NDNHParticipantEmployerDetail.aspx.vb:line 110
       at DCSE.iAPECS.Web.UI.NDNHParticipantEmployerDetail.loadEmployerAddresses() in C:\VS-Projects\DCSE.iAPECS-StarterSolution\DCSE.iAPECS.Web.UI\FCR\NDNHParticipantEmployerDetail.aspx.vb:line 71
       at DCSE.iAPECS.Web.UI.NDNHParticipantEmployerDetail.Page_Load(Object sender, EventArgs e) in C:\VS-Projects\DCSE.iAPECS-StarterSolution\DCSE.iAPECS.Web.UI\FCR\NDNHParticipantEmployerDetail.aspx.vb:line 28
       at System.Web.UI.Control.OnLoad(EventArgs e)
       at DCSE.iAPECS.Web.UI.BasePage.OnLoad(EventArgs e) in C:\VS-Projects\DCSE.iAPECS-StarterSolution\DCSE.iAPECS.Web.UI\Templates\BasePage.vb:line 62
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException: 



Attachments
Filename File size Added on Approval
CharTrimConverter.cs 6,255 03-Oct-2011 15:02.58 Approved
Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 03-Oct-2011 16:11:49   

The problem isin your ConvertFrom method, as shown in the stack trace.

if (value.GetType().FullName == "System.String")
{
    if (value != null && value != DBNull.Value)
    {
        toReturn = Convert.ToString(value).Trim();
    }
}

The above if condition (the first one) will throw exception if value is null. You should switch these 2 if conditions. So as the one checking for null comes first then the one checking for the Type.

jovball
User
Posts: 443
Joined: 23-Jan-2005
# Posted on: 05-Oct-2011 21:03:21   

Walaa:

Thanks, that fixed the problem.