Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm writing a ASP.NET C# REST service which GET's and POST's data. The GET works fine, I've choosen the XML-format to retrieve the info. The POST gives me "System.NullReferenceException: Object reference not set to an instance of an object." What is the reason why the Class "Relatie" doesn't get filled?

XML
XML
    <ArrayOfRelatie xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Dekra.Synergy.WS.Models">
        <Relatie>
            <BedrijfAdres />
            <BedrijfEmailadres />
            <BedrijfNaam>Generali France</BedrijfNaam>
            <BedrijfPlaats />
            <BedrijfPostcode />
            <BedrijfTelefoon />
            <Classificatie>PRI</Classificatie>
            <ContactAchternaam>--</ContactAchternaam>
            <ContactEmailadres />
            <ContactInitialen />
            <ContactTelefoon />
            <ContactTussenvoegsel />
            <ContactVoornaam />
            <Id>400146</Id>
            <Referentie />
            <Type>C</Type>
            <VoorkeurCC />
        </Relatie>
    </ArrayOfRelatie>



CLICK EVENT
    WebClient _webClient = new WebClient();
            _webClient.Credentials = new NetworkCredential("....", "....");
            _webClient.Headers.Add("Content-type", "application/xml");
            _webClient.Headers.Add("Accept", "application/xml");

            Uri uriGet = new Uri("https://claimsotap.dekra.com/Bizzoscope WS/api/Relatie?code=400146");
        string URL = "https://claimsotap.dekra.com/Bizzoscope WS/api/Relatie";
        string Response = _webClient.DownloadString(uriGet);

        byte[] bytes = Encoding.UTF8.GetBytes(Response);

        HttpWebRequest request = CreateWebRequest(URL, bytes.Length);

        using (var requestStream = request.GetRequestStream())
        {
            requestStream.Write(bytes, 0, bytes.Length);
        }

        using (var response = (HttpWebResponse)request.GetResponse())
        {
            if (response.StatusCode != HttpStatusCode.Created && response.StatusCode != HttpStatusCode.OK)
            {
                string message = String.Format("POST failed. Received HTTP {0}", response.StatusCode);
                throw new ApplicationException(message);
            }
        }
public HttpResponseMessage Post(Relatie relatie)
        {
            logger.Logger.Info("SaveRelatie");
            this.relatieRepository.SaveRelatie(relatie);

            var response = Request.CreateResponse<Relatie>(System.Net.HttpStatusCode.Created, relatie);

            return response;
        }

    public bool SaveRelatie(Relatie relatie)
    {
        try
        {
            Functions func = new Functions();
            Guid generatedGuid = new Guid();
            string caseValidationTable = "_CONN_TB_Csnob1799traffic";
            logger.Logger.Info("RelatieRepository");
            func.CreateRelatie(relatie, generatedGuid, caseValidationTable);
            logger.Logger.Info("RELATIE " + relatie.Id + " AANGEMAAKT");
            return true;
        }
        catch (Exception ex)
        {
            logger.Logger.ErrorException("Fout tijdens het aanmaken van de relatie.", ex);
            return false;
        }
    }

    public Guid CreateRelatie(Relatie relationDetails, Guid generatedGuid, string projectNr)
    {
        logger.Logger.Info("Functions");
        try
        {
            if (relationDetails != null) // HERE IT GOES WRONG
            {
                logger.Logger.Info("relDet");
                 ......
Posted
Updated 10-Dec-14 21:29pm
v2
Comments
John C Rayan 19-Feb-15 11:05am    
Where do you call the Post method?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900