Thread: Nullreference exception

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    222

    Question Nullreference exception

    hi all

    i have errors while bulding the code in c#

    Code:
     private void subMenuModify_Click(object sender, System.EventArgs e)
            {
                int nStart = X;
                int spos = 0;
                int epos = this.lvFiles.Columns[0].Width;
                for (int i = 0; i < this.lvFiles.Columns.Count; i++)
                {
                    if (nStart > spos && nStart < epos)
                    {
                        subItemSelected = i;
                        break;
                    }
    
                    spos = epos;
                    epos += this.lvFiles.Columns[i].Width;
                }
                subItemText = this.lvItem.SubItems[2].Text;
    the line at which exception occurs is subItemText = this.lvItem.SubItems[2].Text;

    the exception is as follows:


    Nullreference exception was unhandled

    Object reference not set to an instance of an object.


    Pls anyone help me resolve this issue?

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Looks like that item doesn't exist in your menu. Make sure there are at least 3 items in your menu or that will not work.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    222

    Question

    which item do you think is missing here? u mean "modify item"?

  4. #4
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Code:
    subItemText = this.lvItem.SubItems[2].Text;
    This indexer tries to access something that doesn't exist probably. Make sure you have 3 items at least in this collection. It's usually not good to address menu's in this order unless you are guaranteed to have something there, otherwise you can get exceptions like this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exception handling in a large project
    By EVOEx in forum C++ Programming
    Replies: 7
    Last Post: 01-25-2009, 07:33 AM
  2. exception handling
    By coletek in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2009, 05:28 PM
  3. Handle C++ exception and structured exception together
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 01-24-2008, 09:21 PM
  4. Signal and exception handling
    By nts in forum C++ Programming
    Replies: 23
    Last Post: 11-15-2007, 02:36 PM
  5. Problem with the exception class in MINGW
    By indigo0086 in forum C++ Programming
    Replies: 6
    Last Post: 01-20-2007, 01:12 PM