locked
Object reference not set RRS feed

  • Question

  • I have a problem with this code, I want to make a reservation of the film and show it to the 
    client's profile.
    int mov = int.Parse(dgvMovies.Rows[e.RowIndex].Cells[1].Text);
               Movies m = MoviesService.getMoviesbyID(mov);
    Reservation res = new Reservation();
    res.Customers.customerID = logedCustomer.customerID;
    res.Movies.moviesID = m.moviesID;
    res.Movies.name = m.name;
    ReservationService.saveRezervation(res);
    class : 
    public static Movies getMoviesbyID(int mov)
            {
                var mo = (from a in repeat.Movies
                            where a.moviesID == mov
                            select a).FirstOrDefault();
                return mo;
            }
    I do not know what is wrong here ???
    • Edited by registerr Tuesday, August 2, 2011 10:10 AM
    Sunday, July 31, 2011 9:32 PM

All replies

  • I get this :
    Reservation res = new Reservation();
    res.Customers.customerID = logedCustomer.customerID;   -  Object reference not set to an instance of an object
    res.Movies.moviesID = m.moviesID;
    res.Movies.name = m.name;
    ReservationService.saveRezervation(res);
    What's problem exactly ?
    Monday, August 1, 2011 9:05 AM
  • I propose you'd choose a better Title for your question; "problem here" could apply to 99% of the threads on this forum.

    Furthermore, your explanation is not that usefull;
    The "logedCustomer" seems to be NULL, but based on your code, I have no idea where that "logedCustomer" comes from.


    Regards,
    Koen
    Monday, August 1, 2011 1:08 PM
  • Line 52: res.Customers.customerID = logedCustomer.customerID;

    Stack Trace:

    [NullReferenceException: Object reference not set to an instance of an object.]
    WebUI.Movies.dgvMovies_RowDeleting(Object sender, GridViewDeleteEventArgs e) in C:\Movies\\UI\WebUI\Movies.aspx.cs:52
    System.Web.UI.WebControls.GridView.OnRowDeleting(GridViewDeleteEventArgs e) +133
    System.Web.UI.WebControls.GridView.HandleDelete(GridViewRow row, Int32 rowIndex) +569
    System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +869
    System.Web.UI.WebControls.GridView.RaisePostBackEvent(String eventArgument) +207
    System.Web.UI.WebControls.GridView.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
    System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +175
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

    object is null   :  res.Customers | null  
    Monday, August 1, 2011 3:57 PM
  • Hello,

    Thank you for posting.

    From your description and code snippet, I suggest you first should check whether logedCustomer.customerID was assigned the correct value, but not a NULL value.

    Best Regards,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, August 3, 2011 7:08 AM
  • I tried to make some changes and doing something but then again I have some problems. When I do this all works and can make reservation, but I need that every customer who is logged on your profile see my book, this is not visible, but the book does not successfully complete the examination of their provisions, could only see in the database all the reservations, because we should review CustomersID but I do not know how to do it
    if (logedCustomer != null)
    {
    int mov = int.Parse(dgvMovies.Rows[e.RowIndex].Cells[1].Text);
               Movies m = MoviesService.getMoviesbyID(mov);
    Reservation res = new Reservation();
    res.Customers = logedCustomer;                
    res.Movies = m;               
    }
    ReservationS.save(res);
    When I do this way, I get this message 
    if (logedCustomer != null)
    {
    int mov = int.Parse(dgvMovies.Rows[e.RowIndex].Cells[1].Text);
               Movies m = MoviesService.getMoviesbyID(mov);
    Reservation res = new Reservation();
    res.CustomersID = logedCustomer.CustomersID;                
    res.Movies = m;               
    }
    ReservationS.save(res);
    public static void save(Reservation res)
    {
         repeat.AddToReservation(res);
         repeat.SaveChanges();               -   'FK_Reservation_Customers' relationship. 0 related 'Customers' were found. 1 'Customers' is expected.
    }
    Thursday, August 4, 2011 8:29 AM