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

Asked

Viewed 288 times

-1

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

Of that mistake:

"Undefined object reference for an object instance."

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;
            }
        }
    }
}
  • error line??

  • If you had the line of error, it would be much easier to identify... But just out of curiosity, this "result" object is coming from where?

  • Hi friends thanks, for the return, come on, the staff was closing the company, I took the line because I had company reference, now I edited and put the full code and edited, now da para ter ideia, but the such return/ result is a json with the data, from the base, which fills the combobox. The error of when debugging it reaches the vector line

  • An Exception of type 'System.Nullreferenceexception' occurred in Xyz.UI.dll but was not handled in user code Additional information: Undefined object reference for an instance of an object. &#xA;&#xA; em xyz.UI.Processxyz.Venda.AjaxPrazoMedioClienteEditar.ProcessRequest(HttpContext context)&#xA; em System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()&#xA; em System.Web.HttpApplication.ExecuteStep(Iexecutionstep step, Boolean& completedSynchronously)

4 answers

0

Why are you using an array as return ??

Could use a string list and at the end the method ToArray package System.Linq

thus: lista.ToArray();

Apparently there are other errors in this code but you better make the full exception available.

0

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

Certainly this missing you declare the size of your vector, the error may not even be this, but it is most likely with the information you have.

  • but quantity is dynamic! can’t declare, have any idea I put static, but gave error guess by the difference of the total I declared with the values placed, private string[] v = new string[100];

  • If you want something dynamic, use a list instead of an array. It would look like this: List<string> v = new List<string>();

0

The problem is in this combo take 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

0

placing 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 matrix boundaries.

{"The index was outside the matrix boundaries."}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.