Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. ASP.NET C# strange string array problem!

ASP.NET C# strange string array problem!

Scheduled Pinned Locked Moved C#
helpcsharpasp-netdata-structuresquestion
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Michael101
    wrote on last edited by
    #1

    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

    C 1 Reply Last reply
    0
    • M Michael101

      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

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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 )

      M V 2 Replies Last reply
      0
      • C Christian Graus

        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 )

        M Offline
        M Offline
        Michael101
        wrote on last edited by
        #3

        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.

        C 1 Reply Last reply
        0
        • M Michael101

          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.

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          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 )

          M 1 Reply Last reply
          0
          • C Christian Graus

            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 )

            M Offline
            M Offline
            Michael101
            wrote on last edited by
            #5

            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

            M C 2 Replies Last reply
            0
            • M Michael101

              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

              M Offline
              M Offline
              Malcolm Smart
              wrote on last edited by
              #6

              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 *********************************************

              1 Reply Last reply
              0
              • M Michael101

                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

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #7

                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 )

                1 Reply Last reply
                0
                • C Christian Graus

                  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 )

                  V Offline
                  V Offline
                  Vikram A Punathambekar
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups