0

I'm encountering this error whenever I try to execute the code below. As I have checked, the parameters I tried to pass to the controller were null. I really don't know how to fix this. Please help.

Error: System.NullReferenceException was unhandled by user code

Code:

Controller:

public class VisitorController : ApiController
{
    static readonly IVisitorRepository repository = new VisitorRepository();

    [HttpGet]
    public HttpResponseMessage PostVisitor(Visitor guest)
    {
        guest = repository.AddGuest(guest);
        var response = Request.CreateResponse<Visitor(HttpStatusCode.Created, guest);

        string uri = Url.Link("DefaultApi", new { id = guest.ID });
        response.Headers.Location = new Uri(uri);
        return response;

    }
}

Model:

public class Visitor
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Company { get; set; }
    public string Contact { get; set; }
    public string Email { get; set; }
    public string Purpose { get; set; }
    public string Location { get; set; }
    public DateTime LoggedIn { get; set; }
    public DateTime LoggedOut { get; set; }
}

Class:

public Visitor AddGuest(Visitor details)
{
    string sp = "AddGuestDetails";

    try
    {
        SqlConnection conn = new SqlConnection(constring);
        SqlCommand cmd = new SqlCommand(sp, conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@ID", SqlDbType.Int).Value = details.ID;
        cmd.Parameters.Add("@VisitorName", SqlDbType.NVarChar).Value = details.Name;
        cmd.Parameters.Add("@Company", SqlDbType.NVarChar).Value = details.Company;
        cmd.Parameters.Add("@Contact", SqlDbType.NVarChar).Value = details.Contact;
        cmd.Parameters.Add("@Email", SqlDbType.NVarChar).Value = details.Email;
        cmd.Parameters.Add("@Purpose", SqlDbType.NVarChar).Value = details.Purpose;
        cmd.Parameters.Add("@Location", SqlDbType.NVarChar).Value = details.Location;
        cmd.Parameters.Add("@LogIn", SqlDbType.NVarChar).Value = details.LoggedIn;
        cmd.Parameters.Add("@LogOut", SqlDbType.NVarChar).Value = details.LoggedOut;

        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
    }
    catch (Exception ex)
    {
        Console.WriteLine("An error occurred: '{0}'", ex);
    }

    return details;
}

execution:

localhost/api/visitor/postvisitor/?>Name=Anna&Company=ABCD&Contact=5555&[email protected]&Purpose=Meeting&Location=Roces&Log>gedIn=07/18/2014%2010:00:00&LoggedOut=07/18/2014%2010:00:00}"

7
  • Guest is null? You don't have a line number associated with this null reference? You should always check input parameters and respond gracefully to invalid requests. Also you're going to need something other than the console to log errors... like log4net Commented Jul 21, 2014 at 5:10
  • Sorry. Yes GUEST is NULL. I don't know exactly how to test this. Commented Jul 21, 2014 at 5:15
  • This is where the error shows: string uri = Url.Link("DefaultApi", new { id = guest.ID }); Commented Jul 21, 2014 at 5:21
  • I think you should reword your question to specifically ask how to call your Web API function. Other than not checking the guest parameter is not null and responding with something better than a null reference exception I don't think there's anything wrong with your code. I don't think the URL you're using correct. This might help... weblog.west-wind.com/posts/2012/May/08/… Commented Jul 21, 2014 at 5:28
  • No. The way I call my url works as I've checked with POSTMAN. But I really don't get why the parameters I passed are null. Commented Jul 21, 2014 at 5:31

1 Answer 1

1

You need to pass guest as JSON in the body. Either write it in Javascript like this...

http://weblog.west-wind.com/posts/2012/May/08/Passing-multiple-POST-parameters-to-Web-API-Controller-Methods

Or use your POSTMAN tool to post the JSON with the url, the following doesn't use POSTMAN but it's pretty similiar...

http://www.agile-code.com/blog/building-an-asp-net-web-api-restful-service/

Sign up to request clarification or add additional context in comments.

1 Comment

You could click the up arrow and give me credit for the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.