-1

したがって、Winformがあり、データを含むdgvがあり、適切なセルがクリックされたときに、そのデータを2つの別々の配列リストに保存できる必要があります。それは実際には問題ありませんが、これまでにプログラムされた中で最もばかげたエラーである可能性があります。配列リストが「clsAL」であるクラスをインスタンス化する必要がありますが、プログラムはそれでもエラーをスローし、「オブジェクト参照オブジェクトのインスタンスに設定されていません」。これがコードです(特にスペイン語の場合は、これとは関係ありません:D)

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Data.OleDb;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;


    namespace WindowsFormsApplication7
    {
        public partial class FormH : Form
        {
            clsAL cls;
        public FormH()
        {
            InitializeComponent();
        }

        private void FormH_Load(object sender, EventArgs e)
        {
            cls = new clsAL();
            OleDbCommand comm;
            OleDbConnection conn = new OleDbConnection();
            DataSet dset;
            OleDbDataAdapter adap = new OleDbDataAdapter();
            OleDbCommandBuilder cmb;
            conn.ConnectionString = "Provider = 'Microsoft.Jet.OLEDB.4.0'; Data Source = 'RoboTIC.mdb'";
            comm = new OleDbCommand("Select * From tblHistorial", conn);
            adap = new OleDbDataAdapter(comm);
            dset = new DataSet();
            adap.Fill(dset, "tblHistorial");
            dgvHistorial.DataSource = dset.Tables["tblHistorial"];
        }

        private void dgvHistorial_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            cls = new clsAL();
            string operacion = dgvHistorial.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
            cls.dir.Clear();
            cls.time.Clear();
            string aux = "";
            for (int a = 0; a > operacion.Length; a++)
            {
                aux = aux + operacion[a];
                if (aux == "a")
                {
                    cls.dir.Add("avanzar");
                }
                if (aux == "d")
                {
                    cls.dir.Add("derecha");
                }
                if (aux == "i")
                {
                    cls.dir.Add("izquierda");
                }
                if (aux == "r")
                {
                    cls.dir.Add("retroceder");
                }
                if (aux == "/")
                { 

                }
                if (aux != "a" && aux != "a" && aux != "a" && aux != "a" && aux != "/")
                {
                    cls.time.Add(Convert.ToInt32(operacion[a]+operacion[a+1]));
                }
            }
        }
    }
}
4

1 に答える 1

0

問題は、のdirarraylistにありclsAlます。これはNullReferenceExceptionです。dirこれは、呼び出しようとしているときに配列リストを割り当てていないためですClear()

clsAl追加のコンストラクターで

     clsAl()
     {
          dir = new ArrayList();
          time = new ArrayList();
     }

そして、これらの例外の取得を停止します。

于 2012-12-02T23:36:42.483 に答える