ASP.NET C# strange string array problem!
-
Hello everyone, I have an ASP.NET C# website that loads data (modules). This is my code below: private void prevMod() { string[] stModArray = (string[])Session["modArray"]; for (int i=stModArray.Length-1; i>0; i--) { if (Session["selectedModule"].ToString() == stModArray[i].ToString()) { Session["selectedModule"] = stModArray[i-1].ToString(); break; } } BindData(); } Ultimately, this button loads all the data into a string array and when you press it it moves to the previous item in the array but before I'm able to do that I get this error: Object reference not set to an instance of an object. - for (int i=stModArray.Length-1; i>0; i--) Does anyone have any ideas? Thanks in advance I appreciate the help
-
Hello everyone, I have an ASP.NET C# website that loads data (modules). This is my code below: private void prevMod() { string[] stModArray = (string[])Session["modArray"]; for (int i=stModArray.Length-1; i>0; i--) { if (Session["selectedModule"].ToString() == stModArray[i].ToString()) { Session["selectedModule"] = stModArray[i-1].ToString(); break; } } BindData(); } Ultimately, this button loads all the data into a string array and when you press it it moves to the previous item in the array but before I'm able to do that I get this error: Object reference not set to an instance of an object. - for (int i=stModArray.Length-1; i>0; i--) Does anyone have any ideas? Thanks in advance I appreciate the help
Have you tried stepping through the code to see what causes the error ? Is modArray always a string array ? Are the strings ever null ?
Michael101 wrote:
stModArray[i].ToString(
This is kind of ridiculous, you call ToString on a string, and it could be null, which would cause your error.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
Have you tried stepping through the code to see what causes the error ? Is modArray always a string array ? Are the strings ever null ?
Michael101 wrote:
stModArray[i].ToString(
This is kind of ridiculous, you call ToString on a string, and it could be null, which would cause your error.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
What causes the error is when I try to return the length for the for loop. I know the ToString looks stupid I origionally didn't do that but I will check if a null value is getting passed in but I can't see why...... The problem is when it firstly initiates the for loop.
-
What causes the error is when I try to return the length for the for loop. I know the ToString looks stupid I origionally didn't do that but I will check if a null value is getting passed in but I can't see why...... The problem is when it firstly initiates the for loop.
Then stModArray must be null. Have you tried setting a break point to see why it fails ?
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
Then stModArray must be null. Have you tried setting a break point to see why it fails ?
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
Yep I've found the problem.... It is null after all..... I did a Response.Write command and it's empty. I heard there is a way of promoting people within the site, since you've helped me I'll be more than happy to do it for you. How do you do it though? Thanks for the help
-
Yep I've found the problem.... It is null after all..... I did a Response.Write command and it's empty. I heard there is a way of promoting people within the site, since you've helped me I'll be more than happy to do it for you. How do you do it though? Thanks for the help
CG doesn't need promoting - he is generally recognised as the definitive answer is this forum. Just rate his post using the 1 2 3 4 5 at the bottom.
Regards *********************************************
-
Yep I've found the problem.... It is null after all..... I did a Response.Write command and it's empty. I heard there is a way of promoting people within the site, since you've helped me I'll be more than happy to do it for you. How do you do it though? Thanks for the help
Thanks for the 5 :-) Glad to help.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
Have you tried stepping through the code to see what causes the error ? Is modArray always a string array ? Are the strings ever null ?
Michael101 wrote:
stModArray[i].ToString(
This is kind of ridiculous, you call ToString on a string, and it could be null, which would cause your error.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
Christian Graus wrote:
you call ToString on a string
That brings back some memories.... this dude would write code like someString.ToLower().ToString() and someString = someOtherString.ToString(). :wtf:
Cheers, Vıkram.
After all is said and done, much is said and little is done.