获取错误作为对象引用未单击保存按钮时设置为对象的实例

时间:2014-01-09 05:34:52

标签: c# winforms datagridview

private void btnSave_Click(object sender, EventArgs e)
    {
        int i = 0;

        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            if (!row.IsNewRow)

                {
                    string keyword = row.Cells[0].Value.ToString();
                    string fname = row.Cells[1].Value.ToString();
                    string comm = row.Cells[2].Value.ToString();
                    string retur = row.Cells[3].Value.ToString();
                    string message = row.Cells[4].Value.ToString();
                    string inm = this.cmbInNa.Text.ToString();
                    i = pbl.fsave(keyword, fname, comm, retur, message, inm);
                    if (i > 0)
                    {
                        MessageBox.Show("Success");
                    }
                    else
                    {
                        MessageBox.Show("Error");
                    }
                }                            
        }
    }

单击btnSave时,我得到“对象引用未设置为对象的实例”。我想在gridview中插入值。有什么问题?

2 个答案:

答案 0 :(得分:2)

任何这些网格单元格值都可能null对空值应用ToString()会导致异常。

string keyword = row.Cells[0].Value.ToString();

最好的方法是在执行ToString()之前检查null。您可以使用Turnary operator

string keyword = (null != row.Cells[0].Value) ? row.Cells[0].Value.ToString() : string.Empty;

答案 1 :(得分:0)

与null异常有同样的问题,遵循此处给出的提示,但表中没有空字段。

问题是,我通过删除以下行禁用了按钮列:

public class DownloadInfoArrayAdapter extends ArrayAdapter<DownloadInfo> {
    // Simple class to make it so that we don't have to call findViewById frequently

    public static Integer result;
    private static class ViewHolder {
        TextView textView;
        ProgressBar progressBar;
        Button button;
        DownloadInfo info;
        TextView size;
        TextView prog;
    }


    private static final String TAG = DownloadInfoArrayAdapter.class.getSimpleName();

    public DownloadInfoArrayAdapter(Context context, int textViewResourceId,
                                    List<DownloadInfo> objects) {
        super(context, textViewResourceId, objects);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        final DownloadInfo info = getItem(position);
       // SignInAsyntask task1 = new SignInAsyntask(info);
        //task1.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        //new SignInAsyntask().execute();
        // We need to set the convertView's progressBar to null.

        ViewHolder holder = null;

        if(null == row) {
            LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.file_download_row, parent, false);

            holder = new ViewHolder();
            holder.textView = (TextView) row.findViewById(R.id.downloadFileName);
            holder.progressBar = (ProgressBar) row.findViewById(R.id.downloadProgressBar);
            holder.size=(TextView) row.findViewById(R.id.downloadFileSize);
            holder.prog=(TextView) row.findViewById(R.id.downloadFileProgress);
            holder.button = (Button)row.findViewById(R.id.downloadButton);
            holder.info = info;

            row.setTag(holder);
        } else {
            holder = (ViewHolder) row.getTag();

            holder.info.setProgressBar(null);
            holder.info = info;
            holder.info.setProgressBar(holder.progressBar);
            holder.info.setProgress(null);
            holder.info = info;
           // holder.info.setProgress(holder.prog);
        }

        holder.textView.setText(info.getFilename());
        holder.progressBar.setProgress(info.getProgress());
        holder.progressBar.setMax(info.getFileSize());
        holder.prog.setText(info.getFilePercent() + "/100");
        holder.size.setText(info.getFileSize() + "MB");

        info.setProgressBar(holder.progressBar);
        //info.setProgress(holder.prog);

        holder.button.setEnabled(info.getDownloadState() == DownloadState.NOT_STARTED);
        final Button button = holder.button;
        holder.button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                info.setDownloadState(DownloadState.QUEUED);
                button.setEnabled(false);
                button.invalidate();
                FileDownloadTask task = new FileDownloadTask(info);
                task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
            }
        });


        //TODO: When reusing a view, invalidate the current progressBar.

        return row;
    }
}

因此,对于每个按钮列,您必须提供一个没有代码的事件处理程序。否则,您将从桌面上点击的任何内容中获得例外。