Skip to content

Commit

Permalink
Fix NPE in handleQueryWithUnboundKey method
Browse files Browse the repository at this point in the history
  • Loading branch information
nullterminated committed Mar 30, 2013
1 parent c100c3b commit f0e2baa
Showing 1 changed file with 17 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ public class ERXGenericRecord extends EOGenericRecord implements ERXGuardedObjec
* <a href="http://java.sun.com/j2se/1.4/pdf/serial-spec.pdf">Java Object Serialization Spec</a>
*/
private static final long serialVersionUID = 1L;

private transient EOEntity _entity;

/** holds all subclass related Logger's */
private static final NSMutableDictionary<Class, Logger> classLogs = new NSMutableDictionary<Class, Logger>();
Expand Down Expand Up @@ -633,7 +635,10 @@ public NSArray<String> primaryKeyAttributeNames() {
* @return EOEntity for the current object
*/
public EOEntity entity() {
return ERXEOAccessUtilities.entityNamed(editingContext(), entityName());
if(_entity == null) {
_entity = ERXEOAccessUtilities.entityNamed(editingContext(), entityName());
}
return _entity;
}

/** caches the primary key dictionary for the given object */
Expand Down Expand Up @@ -1064,17 +1069,19 @@ public boolean isValidatedWhenNested() {
*/
@Override
public Object handleQueryWithUnboundKey(String key) {
NSDictionary pkDict = EOUtilities.primaryKeyForObject(editingContext(), this);
if (pkDict == null) {
// This will be the case for new unsaved objects, so just
// check if the user was using a key that is valid as a primary key attribute
if (entity().primaryKeyAttributeNames().contains(key)) {
// Valid hidden PK key, so return null since PK is still essentially null.
// Handles primary key attribute values
if(entity().primaryKeyAttributeNames().contains(key)) {
// Deleted object. Return null.
if(editingContext() == null) {
return null;
}
NSDictionary pkDict = EOUtilities.primaryKeyForObject(editingContext(), this);
// New object. Return null.
if(pkDict == null) {
return null;
}
} else if (pkDict.allKeys().contains(key)) {
// Valid PK key, so return the atribute value.
return pkDict.valueForKey(key);
// Return value for key
return pkDict.objectForKey(key);
}
return super.handleQueryWithUnboundKey(key);
}
Expand Down

0 comments on commit f0e2baa

Please sign in to comment.