CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2010
    Posts
    1

    Problem with Read method in dll

    Hi, I have problem with the List in Read method, in this line: sum += intermediateResult[i]; I have error:
    Code:
    System.NullReferenceException: Object reference not set to an instance of an object.
    System.NullReferenceException: 
       at Concatenate.Terminate()
    My list is still empty, I do not know how to properly write read method for the list:
    Code:
    [Serializable]
    [SqlUserDefinedAggregate(
        Format.UserDefined, 
        MaxByteSize = 8000)
    ]
    public class Concatenate : IBinarySerialize
    {
        private List<double> intermediateResult;
        private int count;
    
        public void Init()
        {
            count = 0;
            this.intermediateResult = new List<double>();
        }
    
        public void Accumulate(double? value)
        {
            if (value != null)
            {
                this.intermediateResult.Add((double)value);
                count++;
            }
        }
    
        public void Merge(Concatenate other)
        {
        }
    
        public SqlDouble Terminate()
        {
            double sum = 0.0;
            for (int i = 0; i < this.count; i++)
                sum += intermediateResult[i];
            return new SqlDouble(suma);
        }
    
        public void Read(BinaryReader r)
        {
            count = r.ReadInt32();
           // for(int i=0; i<14; i++) 
           //     intermediateResult[i] = r.ReadDouble();
        }
    
        public void Write(BinaryWriter w)
        {
            w.Write(count);
            w.Write(intermediateResult.Count * sizeof(double));
        }
    }
    In sql server :
    Code:
    CREATE ASSEMBLY zzz AUTHORIZATION dbo
    FROM 'C:\library.dll'
    WITH PERMISSION_SET=SAFE
    GO
    CREATE AGGREGATE aaa(@value float) RETURNS float 
    EXTERNAL NAME zzz.Concatenate
    GO
    SELECT dbo.aaa(height) as height FROM People

  2. #2
    Join Date
    Jun 2010
    Posts
    85

    Re: Problem with Read method in dll

    Why dont you have your "Init" Method called in the constructor. From the code I see init will never be called, and therefore intermediateResult will always be null, which is exactly what the exception is telling you.

    Why dont you put a break point on that line then hover your mouse over that variable and see that it is indeed null, it must be since it is the only thing there that can possibly be null.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured