How to avoid NoSuchObjectException

7 posts, 0 answers
  1. Nobre
    Nobre avatar
    1 posts
    Member since:
    Jul 2009

    Posted 17 Jul 2009 Link to this post

    Hi all,

    I have two tables in my database that have an 1-1 weak relation: Customer and ContactPerson. This means ContactPerson table don't need to be filled.

    I have created a class Customer with a Property ContactPerson
    1 namespace Example  
    2 {  
    3     [Persistent]  
    4     public class ContactPerson  
    5     {  
    6         private String  _FirstName;  
    7         public String FirstName  
    8         {  
    9             get { return this._FirstName; }  
    10             set { this._FirstName = value; }  
    11         }  
    12     }  
    13  
    14     [Persistent]  
    15     public class Customer  
    16     {  
    17         private ContactPerson _contactPerson;  
    18         public ContactPerson ContactPerson  
    19         {  
    20             get { return this._contactPerson; }  
    21             set { this._contactPerson = value; }  
    22         }  
    23  
    24         private String _TaxNumber;  
    25         public String TaxNumber  
    26         {  
    27             get { return this._TaxNumber; }  
    28             set { this._TaxNumber = value; }  
    29         }  
    30     }  
    31 }
    32  

    The problem is when I have a Customer without ContactPerson and I try to check for it, with the code below, I get a "NoSuchObjectException" on line 3.
    1         public static Boolean HasContactPerson(Customer customer)  
    2         {  
    3             if (customer.ContactPerson == null)  
    4                 return false;  
    5             else 
    6                 return true;  
    7         }  
    8

    I know I can get the Exception with a try-catch block but, as they say, "Exception handling are for cases you don't expect". In my case I'm expecting the ContactPerson sometimes not exists.

    So, how can I avoid the "NoSuchObjectException" to be thrown?

    Thanks.
  2. PetarP
    Admin
    PetarP avatar
    754 posts

    Posted 22 Jul 2009 Link to this post

    Hello Nobre,
    we tried the code that you provided in order to reproduce the error but we failed. Everything worked as expected for us.
    What we did was:
    1. We created the same exact classes that you provided.
    2. We entered some information.
    3. We used Linq/Sql/Oql to retrieve a record and pass it to your method.
    Everything did worked for us.
    The exception itself usually indicates one of the following:
    1. The persistent object was removed from the database.
    2. The object ID used to retrieve the persistent object is not a referring to a persistent object.
    Most likely, you are attempting to access an object that has been deleted or has not been stored.
    If you are still unable to find a resolution for this problem please notify us, we will elevate the forum thread to a support thread, therefore you will be able to attach a small project that represents the problem and we will investigate it further.

    Kind regards,
    Petar
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. DevCraft banner
  4. John
    John avatar
    2 posts
    Member since:
    Jun 2009

    Posted 07 Aug 2009 Link to this post

    I'm experiencing the same situation. If you found a resolution, please post it!
  5. PetarP
    Admin
    PetarP avatar
    754 posts

    Posted 12 Aug 2009 Link to this post

    Hello John,

    Is it possible that you are trying to access an object by ID that has been added to the scope but not stored? Usually this exception is caused  by either trying to access deleted object or objects that are not yet stored into the database.

    Sincerely yours,
    Petar
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  6. Adrian
    Adrian  avatar
    15 posts
    Member since:
    Dec 2010

    Posted 13 Jun 2011 Link to this post

    have two databases, one called management and the other named departments.
    If a user creates a department, management products, and another user deletes management products. When the first user saves the department, is expelled from the program and receives an exception. How can block the second user can not delete management products, if involved in a transaction. (The second user can not delete management products, if the first user is involved in creating this department on the management but did not save)

    " Object Reference not intance of an object "
  7. Adrian
    Adrian  avatar
    15 posts
    Member since:
    Dec 2010

    Posted 13 Jun 2011 Link to this post

    I have a database that works two operators and one operator delete an object, the second running in parallel oerator uses that object in another database without refresh before and the program crashes. How do I handle this error.
  8. Alexander
    Admin
    Alexander avatar
    727 posts

    Posted 16 Jun 2011 Link to this post

    Hello Neagoe,

    If you do not want to allow two users to operate on the same object, you may want to use pessimistic concurrency control. More information about it can be found in this article. Please note the Lock() method, which you can use to explicitly lock an object, so that noone else can modify it. If someone tries to, an exception would be thrown but you can handle it and notify the user with an appropriate UI.

    Regards,
    Alexander
    the Telerik team
    Q1’11 SP1 of Telerik OpenAccess is available for download; also available is the Q2'11 Roadmap for Telerik OpenAccess ORM.
Back to Top
DevCraft banner