This is a migrated thread and some comments may be shown as answers.

How to avoid NoSuchObjectException

6 Answers 204 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Nobre
Top achievements
Rank 1
Nobre asked on 17 Jul 2009, 11:08 AM
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
namespace Example  
{  
    [Persistent]  
    public class ContactPerson  
    {  
        private String  _FirstName;  
        public String FirstName  
        {  
            get { return this._FirstName; }  
            set { this._FirstName = value; }  
        }  
    }  
 
    [Persistent]  
    public class Customer  
    {  
        private ContactPerson _contactPerson;  
        public ContactPerson ContactPerson  
        {  
            get { return this._contactPerson; }  
            set { this._contactPerson = value; }  
        }  
 
        private String _TaxNumber;  
        public String TaxNumber  
        {  
            get { return this._TaxNumber; }  
            set { this._TaxNumber = value; }  
        }  
    }  
}
 

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.
        public static Boolean HasContactPerson(Customer customer)  
        {  
            if (customer.ContactPerson == null)  
                return false;  
            else 
                return true;  
        }  

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.

6 Answers, 1 is accepted

Sort by
0
PetarP
Telerik team
answered on 22 Jul 2009, 03:34 PM
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.
0
John
Top achievements
Rank 1
answered on 08 Aug 2009, 12:07 AM
I'm experiencing the same situation. If you found a resolution, please post it!
0
PetarP
Telerik team
answered on 12 Aug 2009, 09:22 AM
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.
0
Adrian
Top achievements
Rank 2
answered on 13 Jun 2011, 09:05 PM
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 "
0
Adrian
Top achievements
Rank 2
answered on 13 Jun 2011, 09:07 PM
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.
0
Alexander
Telerik team
answered on 16 Jun 2011, 03:33 PM
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.
Tags
General Discussions
Asked by
Nobre
Top achievements
Rank 1
Answers by
PetarP
Telerik team
John
Top achievements
Rank 1
Adrian
Top achievements
Rank 2
Alexander
Telerik team
Share this question
or