将字符串值添加到列表框时,NullrefernceException [英] Nullrefernceexception while adding string value to listbox

查看:76
本文介绍了将字符串值添加到列表框时,NullrefernceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在列表框中添加项目时,我正在获取system.NullReferenceException
在客户端服务器上运行的exe中

我当时在列表框中像这样的listboxname.items.add(string message)添加消息,但出现错误

i am getting system.NullReferenceException when i am trying to add item in listbox
in a exe which runs on a client server

i add message in listbox like this listboxname.items.add(string message) at that point i am getting error

推荐答案

您调用item.ToString(),但是item null.首先检查!
You call item.ToString(), but item is null. Check that first!




听起来您正在尝试添加尚未分配的值.在循环运行时,请确保获得了您认为的价值.

如果使用for循环读取字符串,请确保要读取的实例值包含期望的值.示例:

Hi,

It sounds like you are trying to add a value that has not been assigned yet. As you are running in a loop, make sure you are getting the value you think you are.

If you are using a for loop to read the strings, make sure the instance value you are reading contains what you expect. Example:

for (int i = 0; i <= 5; i++)
{
listboxname.items.add(sourchelist[i]);
}



确保您的范围是正确的...换句话说,请确保如果要创建从0开始的索引列表,则从0开始读取,并在正确的位置停止读取.例如确保您的范围选择器(在上例中为<= 5)是正确的.否则,当源列表只有5个条目时,您可能会尝试从索引5读取(在这种情况下,您应该从4处停止读取).

确保分配要添加的字符串的初始值.即,在创建包含要添加到列表的文本的变量的地方,请使用:



Make sure that your range is correct... in other words, make sure that if you are creating a 0-base indexed list you start reading from 0, and that you stop reading at the correct point. e.g. make sure your range selector (<= 5 in the above example) is correct. Otherwise you may be trying to read from index 5 when the source list only has 5 entries (in which case you should stop reading at 4).

Make sure you assign the initial value of the string you are adding. i.e. Where you create the variable that contains the text you are adding to the list use:

string textToAdd = "";
// Rest of your code...



代替



in stead of

string textToAdd;
// Rest of your code...




这样可以确保设置了初始值.

您在评论中提到正在使用多线程.这增加了一些要检查的内容:

1.确保您没有遇到竞争状况(在设置之前先读取该值)
2.确保从与正在运行列表框的线程相同的线程更新列表框的值.我通常在该代码中创建一个实用程序方法,您可以从其他线程中调用该方法来更新列表框:




Which makes sure that the initial value is set.

You mentioned in your comment that you are using multi-threading. This adds a few more things to check:

1. Make sure you are not running into a race condition (Reading the value before it is set)
2. Make sure you are updating the listbox values from the same thread as the one on which the listbox is running. I usually create a utility method in that code that you can call from other threads to update the listbox:

private void AddToListBox(string str)
{
    if (InvokeRequired) BeginInvoke(new Action<string>(AddToListBox), str);
    else listboxname.Items.Add(str);
}



3.确保如果使用队列锁定来确保线程数据的完整性,则锁定不会超时并且不会丢弃更新.

希望有帮助!



3. Make sure that if you are using queued locking to ensure your threaded data integrity, that your locks are not timing out and discarding your updates.

Hope that helps!


这篇关于将字符串值添加到列表框时,NullrefernceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆