Hi Friends,
I am getting the following error with my code
"Object reference not set to instance of the object" .
My code goes as follows:

...
...
string[] isem= new string[125];
decimal[] perc = new decimal[125];
student s1=new student();
...
...
public void func()
{

isem = new string[125];
perc = new decimal[125];
string[] temproll=new string[125];
int j=0;
sem = s1.calcallperc1(..., ..., ....);//calcperc1 returns an array
j=-1;
foreach (string s in isem)
                    {
                                             
                        temproll[++j] = s.Split(' ')[0];
                        perc[j] = Convert.ToDecimal(s.Split(' ')[1]);

                    }


}
...

Please help!!

Recommended Answers

All 5 Replies

When I experience a NullReference exception(using VS 2008) The line that it occured in is pointed out to me, together with some tips.
Could you please give us the line where the NullRef occured?
Also the use of code tags is strongly reccomended.

null reference occured in the follwing line

temproll[++j] = s.Split(' ')[0];

None of your strings seem to be initialized. I tried this and it compiles, should give you some ideas.

string[] isem = new string[125];

            //isem = new string[125];

            string[] temproll = new string[125];
            for (int i = 0; i < 125; i++)
            {
                isem[i] = i.ToString()+ " " + "querty";
                temproll[i] = "";
            }

            int j = -1;
            foreach (string s in isem)
            {
                temproll[++j] = s.Split(new char[]{' '})[0];
            }

Because isem is empty and each string has null value when you split it it doesn't return an array, and you access its 0 index

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.