0

I am trying to get the results of a linq query int a file. I created and array of the same type as the FileHelpers class I created, then queried the data and assigned the values to the array I just created.

I get the following error:

Object reference not set to an instance of an object.

The strange thing is that the item giving out the error is the one that is getting a value assigned to it. Not sure why this is happening:

NorthwindEntities dbContext = new NorthwindEntities();
        var q = from d in dbContext.Products
                select d;
        producdt[] items = new producdt[q.Count()];

        for (int i = 0; i < q .Count(); i++)
        {
            items[i].Field1 = q.ToList()[i].ProductName;
        }
        FileHelperEngine<producdt> engine = new FileHelperEngine<producdt>();
            engine.WriteFile("test.text", items);

including product class:

[FixedLengthRecord(FixedMode.ExactLength)]
public sealed class producdt
{

[FieldFixedLength(10)]
public String Field1;

[FieldFixedLength(10)]
public String Field2;

[FieldFixedLength(10)]
public String Field3;

[FieldFixedLength(10)]
public String Field4;

[FieldFixedLength(10)]
public String Field5;


}  
tshepang
  • 12,111
  • 21
  • 91
  • 136
  • Is `producdt` an object or a record? Also, this could be *heavily* optimised, your making 3 trips to the database here when really it could be done in one. – James May 01 '13 at 17:13
  • the error is on the line items[i].field1 = q.ToList()[i].ProductName; – user2275571 May 02 '13 at 13:19
  • Let me re-phrase - is `producdt` a class or a record? – James May 02 '13 at 13:31
  • producdt is a class: [FixedLengthRecord(FixedMode.ExactLength)] public sealed class producdt { [FieldFixedLength(10)] public String Field1; [FieldFixedLength(10)] public String Field2; [FieldFixedLength(10)] public String Field3; [FieldFixedLength(10)] public String Field4; [FieldFixedLength(10)] public String Field5; } – user2275571 May 02 '13 at 14:31
  • The solution @dotNET has suggested will do the job for you. Not only that, it is more efficient as you will only be making 1 trip to the DB....at the minute you are making 3 at minimum. – James May 02 '13 at 15:20
  • when i use his suggestion i get the following error: Error 6 Cannot implicitly convert type 'AnonymousType#1[]' to 'producdt' – user2275571 May 02 '13 at 15:37
  • That sounds as though your trying to project an anonymous type to an array of `producdt`, are you using the code as written? Or are you writing `select new { Name = d.Name }`. – James May 02 '13 at 16:02

1 Answers1

4

You must initialize items[i] before using it in the for loop. The problem is that you have created the array, but its individual elements are null. I guess you'd want to assign the return of your linq query to this array, after doing some projection using Select().

Though I don't know the specifics of your class, I suppose you'd do it on the following lines:

producdt[] items = (from d in dbContext.Products
                   select new producdt(){ 
                                          Field1 = d.Field1, 
                                          Field2 = d.Field2, 
                                          Field3 = d.Field3, 
                                          Field4 = d.Field4
                                         }).ToArray();
dotNET
  • 33,414
  • 24
  • 162
  • 251
  • 1
    That is, assuming `productdt` is a reference type (which it most likely is), though it would have helped to know which line the error was occurring on exactly. – Jeff Mercado May 01 '13 at 16:30
  • The WriteFile method accepts an IEnumerable so you can avoid the ToArray() too. var items = (from d in dbContext.Products select new producdt(){ Name = d.Name }); – Marcos Meli May 02 '13 at 13:15
  • this is the error when i try thet assignment above: Error 6 Cannot implicitly convert type 'AnonymousType#1[]' to 'producdt' – user2275571 May 02 '13 at 13:18
  • @user2275571: Can you plz include the body of productdt class in your question? – dotNET May 02 '13 at 14:25
  • check above...i've added the class – user2275571 May 02 '13 at 14:46
  • Updated my code. Then again I don't know the structure of your entity (Products), but I guess you should be able to easily expand my code. – dotNET May 02 '13 at 17:00