-
December 14th, 2010, 02:19 PM
#1
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);
}
}
}
-
December 14th, 2010, 02:21 PM
#2
Re: Object reference not set to an instance of an object
Originally Posted by novamike
listboxContextMenu.Items.Add(string.Format("Edit - {0}",
Also, it highlights this line upon the crash.
-
December 14th, 2010, 02:30 PM
#3
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
-
December 14th, 2010, 03:16 PM
#4
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.
Originally Posted by memeloo
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:
-
December 14th, 2010, 03:22 PM
#5
Re: Object reference not set to an instance of an object
Originally Posted by BigEd781
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:
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
-
December 14th, 2010, 03:34 PM
#6
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.
-
December 14th, 2010, 04:44 PM
#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?
-
December 14th, 2010, 04:46 PM
#8
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.
-
December 14th, 2010, 04:49 PM
#9
Re: Object reference not set to an instance of an object
Originally Posted by BigEd781
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...
-
December 15th, 2010, 12:10 AM
#10
Re: Object reference not set to an instance of an object
Originally Posted by novamike
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
-
December 15th, 2010, 08:58 AM
#11
Re: Object reference not set to an instance of an object
Originally Posted by memeloo
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
-
December 15th, 2010, 11:38 AM
#12
Re: Object reference not set to an instance of an object
Originally Posted by novamike
wow, helpful response. why even type that if you aren't trying to help?
I already helped you enough but
Originally Posted by novamike
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
Originally Posted by novamike
if you can't help,
the question is not if I can help but whether I do it
Originally Posted by novamike
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
-
December 15th, 2010, 03:55 PM
#13
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!
-
December 17th, 2010, 10:25 AM
#14
Re: Object reference not set to an instance of an object
-
December 17th, 2010, 12:47 PM
#15
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|