CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Dec 2010
    Posts
    7

    Object reference not set to an instance of an object

    I am coding an application that can store information in a listbox and save it to text file.

    I am trying to code a right click context menu for the listbox. I have successfully implemented a messagebox that comes up when the user right clicks the listbox and it contains no data....but I noticed when testing...if you click OK on the messageboard and quickly right click the listbox again...the application crashes with an "Object reference not set to an instance of an object" message.

    If I could just code it to do nothing when right clicked and also have a working context menu that would be great....here is the code below:



    private ContextMenuStrip listboxContextMenu;


    private void Form1_Load(object sender, EventArgs e)
    {
    listboxContextMenu = new ContextMenuStrip();
    listboxContextMenu.Opening += new CancelEventHandler(listboxContextMenu_Opening);
    listBox1.ContextMenuStrip = listboxContextMenu;

    }



    private void listboxContextMenu_Opening(object sender, CancelEventArgs e)
    {
    //Clear the menu and add custom items
    listboxContextMenu.Items.Clear();
    listboxContextMenu.Items.Add(string.Format("Edit - {0}", listBox1.SelectedItem.ToString()));
    }




    private void listBox1_MouseDown(object sender, MouseEventArgs e)
    {
    if (e.Button == MouseButtons.Right)
    {
    //Select the item under the mouse pointer
    listBox1.SelectedIndex = listBox1.IndexFromPoint(e.Location);
    if (listBox1.SelectedIndex != -1)
    {
    listboxContextMenu.Show();
    }
    else
    {
    MessageBox.Show("Add an item to the list first.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }

    }

    }

  2. #2
    Join Date
    Dec 2010
    Posts
    7

    Re: Object reference not set to an instance of an object

    Quote Originally Posted by novamike View Post
    listboxContextMenu.Items.Add(string.Format("Edit - {0}",
    Also, it highlights this line upon the crash.

  3. #3
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Object reference not set to an instance of an object

    ever heard of CODE tags?

    here's the answer to you qiuz probably this is failing
    Code:
    listboxContextMenu.Items.Add(string.Format("Edit - {0}", listBox1.SelectedItem.ToString()));
    because the SelectedItem is null and what is null cannot be converted to string, nothing cannot be turned into something
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Object reference not set to an instance of an object

    Yeah, you need to check if an item is selected in the first place.

    Quote Originally Posted by memeloo View Post
    because the SelectedItem is null and what is null cannot be converted to string, nothing cannot be turned into something
    Well, that really just depends on the implementation. For example, the nil object in ruby does inherit from Object and this is perfectly valid code:

    Code:
    puts nil.to_s
    =>""

  5. #5
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Object reference not set to an instance of an object

    Quote Originally Posted by BigEd781 View Post
    Well, that really just depends on the implementation. For example, the nil object in ruby does inherit from Object and this is perfectly valid code:

    Code:
    puts nil.to_s
    =>""
    sure, but this is c# forum so I assume the OP is talking about c#

    ruby hehe, its syntax is so funny
    Last edited by memeloo; December 14th, 2010 at 03:31 PM.
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  6. #6
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Object reference not set to an instance of an object

    I know, I was commenting on the conceptual nature of your comment and just noting that really it is an implementation detail. What do you want? I'm a stickler for accuracy :P

    BTW, I love ruby, it's a really fun language.

  7. #7
    Join Date
    Dec 2010
    Posts
    7

    Re: Object reference not set to an instance of an object

    So..how do I code around it since it is null?

    Do I need an if statement?

  8. #8
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Object reference not set to an instance of an object

    Since the only thing the context menu does is allow you to edit the current selection, just don't show it at all if SelectedItem == null. That said... you should probably just select the item under the mouse on a right click. This is better overall, solved the null item problem (unless the list is empty/they did not click an item), and gives the user a better experience.

  9. #9
    Join Date
    Dec 2010
    Posts
    7

    Re: Object reference not set to an instance of an object

    Quote Originally Posted by BigEd781 View Post
    Since the only thing the context menu does is allow you to edit the current selection, just don't show it at all if SelectedItem == null. That said... you should probably just select the item under the mouse on a right click. This is better overall, solved the null item problem (unless the list is empty/they did not click an item), and gives the user a better experience.
    Yes but I am having trouble developing the statement to if and else for that...

  10. #10
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Object reference not set to an instance of an object

    Quote Originally Posted by novamike View Post
    Yes but I am having trouble developing the statement to if and else for that...
    ok, here is a book more helpful then us. you seem not to know the basics
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  11. #11
    Join Date
    Dec 2010
    Posts
    7

    Re: Object reference not set to an instance of an object

    Quote Originally Posted by memeloo View Post
    ok, here is a book more helpful then us. you seem not to know the basics
    wow, helpful response. why even type that if you aren't trying to help? maybe i study the syntax and learn off that? if you can't help, move on to the next **** topic

  12. #12
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Object reference not set to an instance of an object

    Quote Originally Posted by novamike View Post
    wow, helpful response. why even type that if you aren't trying to help?
    I already helped you enough but

    Quote Originally Posted by novamike View Post
    maybe i study the syntax and learn off that?
    you don't even know how to check if a variable is null and I'm not going to teach you that becasue it's in every book and you are just too lazy to read one

    Quote Originally Posted by novamike View Post
    if you can't help,
    the question is not if I can help but whether I do it
    Quote Originally Posted by novamike View Post
    move on to the next **** topic
    however after this words I certainly won't
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  13. #13
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Object reference not set to an instance of an object

    But memeloo, the OP obviously doesn't want to learn, just give him teh codez!

  14. #14
    Join Date
    Dec 2010
    Posts
    7

    Re: Object reference not set to an instance of an object

    thx douchebags

  15. #15
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Object reference not set to an instance of an object

    Right. All I can say is that I hope you never interview for a job where I work.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured