检查代码中的错误(替换方法代码) [英] check an error in code (replace method code )

查看:88
本文介绍了检查代码中的错误(替换方法代码)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开发人员,
我是夏尔马.
我将一个csv文件加载到数据集中,在datagridview中查看它.我正在研究一种替换方法.

我的问题是我从datagridview替换通配符的代码运行正常,但是执行代码后,消息框在屏幕上显示错误消息

我认为我没有正确编写代码,因此请正确编写.
我的代码是:

Hi developers,
I am vishal sharma.
I load a csv file into a dataset, view it in datagridview. I am working on a replace method.

My problem is that my code for replaceing wildcards from datagridview is working properly but after executing the code an error message is show on screen by a message box

I think I''m not writing the code properly, so please write them properly.
My code is:

public void Button1_Click(object sender, EventArgs e)
{
  try
  {
    for(int i= 0;i<DGV.Rows.Count;i++)
    {
      foreach(DataGridViewCell cell in DGV.Rows[i].Cells)
      {
        cell.Value = cell.Value.ToString().Replace(comma.ToString(), slash.ToString());
        cell.Value = cell.Value.ToString().Replace(colon.ToString(), Blank.ToString());
      }
    }
  }
  catch(System.Exception ex)
  {
    MessageBox.Show(ex.Message);
  }
}



执行此代码后显示的错误:

对象引用未设置为对象的实例"

请开发人员,立即解决此问题
我将非常感谢您.



The error that shows up after executing this code:

" object refrence not set to an instance of an object "

Please developers, solve this problem Immediately
I will be thankfull to you.

推荐答案

这行吗?
也许cell.Value为null,这会引发您的异常.

Does this work?
maybe cell.Value is null and this throws your exception.

public void Button1_Click(object sender, EventArgs e)
{
  try
  {
    for(int i= 0;i<DGV.Rows.Count;i++)
    {
      foreach(DataGridViewCell cell in DGV.Rows[i].Cells)
      {
        if(cell.Value == null)
          continue;

        cell.Value = cell.Value.ToString().Replace(comma.ToString(), slash.ToString());
        cell.Value = cell.Value.ToString().Replace(colon.ToString(), Blank.ToString());
      }
    }
  }
  catch(System.Exception ex)
  {
    MessageBox.Show(ex.Message);
  }
}


最好放置一个断点并检查每一行是否为空.
better put a breakpoint and check each line for null.


这篇关于检查代码中的错误(替换方法代码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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