执行搜索以填充矢量时出错

时间:2016-04-29 06:59:36

标签: c#

我在搜索填充向量echo $item->getProduct()->custom_image();时出现返回错误,错误是:

  

" System.NullReferenceException" :对象引用未设置为   对象的实例"。

该类为PegaTipo[]返回向量:

Pegatipo[]

序列化:

    public PegaTipo[] buscaFunc(string func, int ID_empresa)
    {
        SqlDataReader objReader;
        SqlCommand objcmd = null;
        PegaTipo[] retornaInfo = null;
        ArrayList arr = new ArrayList();
        int t;

        vsql = "SELECT [NOME], [CPFCNPJ], [HIDE] FROM CLIENTE WHERE TIPO = @TIPO and ID_EMPRESA = @ID_EMPRESA";

        if (this.Conectar())
        {
            try
            {

                objcmd = new SqlCommand(vsql, objCon);

                objcmd.Parameters.Add(new SqlParameter("@TIPO", func));
                objcmd.Parameters.Add(new SqlParameter("@ID_EMPRESA", ID_empresa));

                objReader = objcmd.ExecuteReader();

                while (objReader.Read())
                {
                    arr.Add(objReader.GetString(0));
                    arr.Add(objReader.GetString(1));
                    arr.Add(objReader.GetBoolean(2));
                }

                t = arr.Count / 3;

                int j = 1;

                retornaInfo = new PegaTipo[t];


                for (int i = 0; i < t; i++, j++)
                {
                    retornaInfo[i].nome = Convert.ToString(arr[0 * j]);
                    retornaInfo[i].tipo = Convert.ToString(arr[1 * j]);
                    retornaInfo[i].hide = Convert.ToBoolean(arr[2 * j]);
                }

                return retornaInfo;
            }
            catch
            {
                throw;
            }
            finally
            {
                this.Desconectar();
            }
        }

        return retornaInfo;
    }

1 个答案:

答案 0 :(得分:1)

你正在初始化PegaTipo数组,但你没有在数组中创建PegaTipo的实例,所以句子:

  

retornaInfo [i] .nome = Convert.ToString(arr [0 * j]);

抛出异常因为retornaInfo [i]为null。你应该在你的循环中这样做:

for (int i = 0; i < t; i++, j++)
{
    retornaInfo[i] = new PegaTipo();
    retornaInfo[i].nome = Convert.ToString(arr[0 * j]);
    retornaInfo[i].tipo = Convert.ToString(arr[1 * j]);
    retornaInfo[i].hide = Convert.ToBoolean(arr[2 * j]);
}