0

I am using Asp.net/C# ,, i have declared an integer array as follows public int[] recno; As i dont know the exact size of the array ,, however inside a function i get to know its size based on the number of customer_id's in the table.Here is the function

public void GetRecordNo()
        {
            recid = from id in dt.cust_masters
                    select id;
            recno = new int[recid.Count()];

            for (int i = 0; i < recid.Count(); i++)
            {
                recno[i] = Convert.ToInt32(recid.ElementAt(i).customer_id);
            }

        }

When i try to call a function ShowRecord(int index) which accepts the id of the customer in the following manner

ShowRecord(recno[0])

It gives me an error

Object reference not set to an instance of an object.

Can anybody point me where am i going wrong. Thanks

5
  • Are you sure there your query returns values? Commented Mar 14, 2012 at 9:01
  • try using "from cust in dt.cust_masters select cust.id;" Commented Mar 14, 2012 at 9:01
  • @Maheep it does return value because when i call the function ShowRecord() inside that function it works well .. Commented Mar 14, 2012 at 9:06
  • what happens if you call ShowRecord(recno[1]) Commented Mar 14, 2012 at 9:07
  • @KOL it gives me the same error , which i mentioned in the question Commented Mar 14, 2012 at 9:09

4 Answers 4

3

You can simplify your code:

recid = from id in dt.cust_masters
          select id.customer_id;

//recno = new int[recid.Count()];
recno = recid.ToArray();
// remove for-loop

And to find/prevent your null ref problem:

void  ShowRecord(int index)
{
    if (index < 0 || index >= recno.Length)
       throw new InvalidArgumentException("index");

    var id = recno[index];
    ...
}
Sign up to request clarification or add additional context in comments.

3 Comments

,,,, but will that allow me to access values of recno outside that function
That depends on how public / private you make recno[]. Not on this method. But what is the actual question ?
,, i have made it public , my question is that i need the values of the array to be passed as parameters to ShowRecord() function.
3

Why can you just use:

public void GetRecordNo()
{
    var recno=(
        from id in dt.cust_masters
        select id.customer_id
    ).ToArray();
}

1 Comment

then how do i pass id's to my function
1

First check which object is null by setting a breakpoint and hovering over the parameter recno of ShowRecord(recno[0]). Is it null? If yes, make sure your GetRecordNo() method is actually being called before your call to ShowRecord.

Or use this to access recno:

public int[] RecNo {
    get {
        if (recno == null) { GetRecNo(); }
        return recno;
    }
}

and then use it like

ShowRecord(RecNo[0])

7 Comments

,, i call GetRecordNo() on load and im calling ShowRecord on click of a button , do you think this might be issue.
the issue was exactly what you pointed out ,,, when i called GetRecordNo() and then ShowRecord() ,, it worked fine ,,, so will i have to call it again n again ...Thanks
@freebird Maybe you are accessing a local variable rather than your public field? I edited my post, maybe it helps.
thanks a lot it worked well for me ,,,, if you could do a favour , could you please explain me how it works.Thanks
It checks if recno is null, and if it is, it calls GetRecNo(). That way, if you access the property RecNo, you never get null. But i think that your load method (where you said you call GetRecNo()) might not get called if recno is still null at this point.
|
0

I think, ShowRecord(int index) contains a parameter which includes int index but when you are calling function,you are using ShowRecord(recno[0]) which means you are passing recno record at index 0.......

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.