Error with vectors c # / {"Object reference not set to an instance of an object."}

0

Friends I'm coming in php and starting to develop in C #. The differences are many ideas that I have when I will apply give errors such as the problem that I have a combobox html that dynamically filled with database data, I thought separates the code which would be the value of the combobox and put as a value in each position from an array up to that, but it does not work. The current version of the following error, does anyone have any idea how I solve?

Give this error:

  

"Object reference not set to an instance of an object."

Code;

using System;
using System.Configuration;
using System.Data;
using System.Net.Http;
using System.Text;
using System.Web;
using Newtonsoft.Json.Linq;

namespace empresa.UI.ProcessEmpresa
{
    /// <summary>
    /// Summary description for AjaxPrazoMedioCliente
    /// </summary>
    public class AjaxPrazoMedioCliente : IHttpHandler
    {
        string[] v;//CRIACAO DO VETOR 
        public int i, j = 0;

        public void ProcessRequest(HttpContext context)
        {
            Grupo = Convert.ToString(context.Request.QueryString["codgrupo"]);
            temmontante = Convert.ToString(context.Request.QueryString["temmontante"]);
            selecionado = Convert.ToString(context.Request.QueryString["selecionado"]);
            temmontante = temmontante.Equals("") ? "0" : temmontante;
            HttpClient client = null;
            string retorno = string.Empty;
            if (client == null)
            {
                client = new HttpClient();
                client.BaseAddress = new Uri(ConfigurationManager.AppSettings["url_servidor"]);
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                HttpResponseMessage resultado = client.GetAsync("xyz/Venda/PrazoMedio/" + Grupo + "/" + temmontante).Result;
                retorno = resultado.Content.ReadAsStringAsync().Result;

                if (resultado.StatusCode != System.Net.HttpStatusCode.NotFound)
                {
                    if (retorno != "[]")
                {
                    sb.Append("<select name=\"txtpagpedido\" id=\"txtpagpedido\" onchange=\"HabilitarDescontoPromocional()\" style=\"width: 200px;\" >");
                    sb.Append("<option value=\"0\">Selecione..</option>");
                    JArray usuarioarrray = JArray.Parse(retorno);

                    foreach (JObject obj in usuarioarrray.Children<JObject>())
                    {
                        foreach (JProperty prop in obj.Properties())
                        {
                            if (v[i] != null)//MEU TRECHO PESQUISEI QUE TALVEZ SOLUCIONARIA O ERRO
                                if (Convert.ToString(prop.Value) != v[i])
                                    v[i] = prop.Value.ToString();
                                else
                                    break;

                            //vetor[i] = Convert.ToString(prop.Value.ToString());

                            switch (prop.Name)
                            {
                                case "e4_CODIGO":
                                    if (prop.Value.ToString().Equals(selecionado))
                                        sb.Append("<option value=\"" + Convert.ToString(prop.Value.ToString()) + "\" selected=\"selected\" >");
                                    else
                                        sb.Append("<option value=\"" + Convert.ToString(prop.Value.ToString()) + "\"  >");
                                    break;
                                case "e4_DESCRI":
                                    sb.Append(Convert.ToString(prop.Value.ToString()) + "</option>");
                                    break;
                                default:
                                    break;
                            }
                            i++;
                        }
                    }

                    sb.Append("</select>");

                }

            }

            context.Response.Write(sb.ToString());
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
    
asked by anonymous 26.12.2017 / 21:17

3 answers

0
if (v[i] != null)//MEU TRECHO PESQUISEI QUE TALVEZ SOLUCIONARIA O ERRO

Surely this is missing you declare the size of your vector, the error may not be this one, but it is more likely to be with the information you have.

    
27.12.2017 / 12:32
0

The problem is in this combo draw the repeated values

  

  Select ..   28   21/28/35   21   49   42/56/70   28/42/56   28/42/56   28/42/56   28/56   28/56/84   28/56/84   28/56/84   28/56/84   28/56/84   28/56/84   28/56/84   28/56/84   28/56/84   28/56/84   28/56/84   28/56/84   28/56/84   ANTICIPATED   ANTICIPATED   ANTICIPATED   ANTICIPATED   ANTICIPATED   ANTICIPATED   42/56   28/42/56/70   28/42   14   14/28/42   14/28/42   28/42/56/70/84   28/42/56/70/84   28/42/56/70/84   35/42/49   10   28/35/42/49   56   56   56   56   35   28/35/42   28/35/42/49/56   28/35/42/49/56   28/35/42/49/56   28 + 7D 7X   42   35/42/49/56   35/42/49/56/63   42/49/56   42/49/56/63/70   42/70/90   14/28   25/56   28/42/56/70/84   

    
27.12.2017 / 13:23
0
  

putting a static / fixed value gave the following error

    private string[] v = new string[100];

An exception of type 'System.IndexOutOfRangeException' occurred in xyz.UI.dll but was not handled in user code

Additional information: The index was outside the bounds of the array.

{"The index was outside the bounds of the array."}

    
27.12.2017 / 13:47