You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by De...@richer.ca on 2004/06/16 19:11:39 UTC

Broker Helper -> representsNull

Hi all,

I've just upgraded to RC7, and I'm now getting the following error on a 
call to PersistenceBroker.retrieveAllReferences():
java.lang.NullPointerException
                 at 
org.apache.ojb.broker.util.BrokerHelper.representsNull(BrokerHelper.java:260)
                 at 
org.apache.ojb.broker.core.QueryReferenceBroker.hasNullifiedFK(QueryReferenceBroker.java:446)
                 at 
org.apache.ojb.broker.core.QueryReferenceBroker.getReferencedObjectIdentity(QueryReferenceBroker.java:410)
                 at 
org.apache.ojb.broker.core.QueryReferenceBroker.retrieveReference(QueryReferenceBroker.java:334)
                 at 
org.apache.ojb.broker.core.QueryReferenceBroker.retrieveReferences(QueryReferenceBroker.java:396)
                 at 
org.apache.ojb.broker.core.PersistenceBrokerImpl.retrieveAllReferences(PersistenceBrokerImpl.java:1145)
                 at ...

It seems that the problem is in the BrokerHelper.representsNull method. 
The field it is complaining about is "reportNumber", which is an anonymous 
field in the target class.  Here's my class descriptor:
<class-descriptor
    class="ca.richer.domain.OutOfService"
    table="FDJ8CPP"
>
    ... other fields omitted for brevity ...
    <field-descriptor
        name="reportNumber"
        column="J8RZNB"
        jdbc-type="DECIMAL"
        precision="20"
        scale="0"
        access="anonymous"
    >
    </field-descriptor>
    <field-descriptor
        name="lineSequence"
        column="J8R0NB"
        jdbc-type="DECIMAL"
        precision="20"
        scale="0"
        access="anonymous"
    >
    </field-descriptor>
    <reference-descriptor
        name="defectLine"
        class-ref="ca.richer.domain.DefectLine"
    >
        <foreignkey field-ref="reportNumber"/>
        <foreignkey field-ref="lineSequence"/>
    </reference-descriptor>
</class-descriptor>

The problem is that AnonymousPersistentField.getType() always returns 
null, so a NullPointerException is thrown when the BrokerHelper tries to 
determine if this field represents a primitive type.

I would suggest the following patch:
    public boolean representsNull(FieldDescriptor fld, Object aValue)
    {
        if(aValue == null) return true;

        boolean result = false;
        if(((aValue instanceof Number) && (((Number) aValue).longValue() 
== 0)))
        {
                // PATCH STARTS HERE
                PersistentField field = fld.getPersistentField();
                Class type = field.getType();

                // AnonymousPersistentFields will *always* have a null 
type according to the
                // javadoc comments in AnonymousPersistentField.getType()
                if (type == null) {
                        return true;
                }
            result = type.isPrimitive();
                // PATCH ENDS HERE
        }
        else if((aValue instanceof String) && (((String) aValue).length() 
== 0))
        {
            result = fld.isPrimaryKey();
        }
        return result;
    }

Maybe I've overlooked something, but if this method is ever called with an 
anonymous field with a value of 0, it will surely throw a NPE.  Thoughts?

Thanks,
Phil Denis

Re: Broker Helper -> representsNull

Posted by Armin Waibel <ar...@apache.org>.
Is fixed in latest CVS.
Thanks Phil, Brian and all the rest of them.

regards,
Armin

Brian Latimer wrote:
> Inserting the following patch into my BrokerHelper class fixed my problem.
> 
>     public boolean representsNull(FieldDescriptor fld, Object aValue)
>     {
>         if(aValue == null) return true;
> 
>         boolean result = false;
>         if(((aValue instanceof Number) && (((Number) aValue).longValue() 
> == 0)))
>         {
>             //result = fld.getPersistentField().getType().isPrimitive();
>             // PATCH STARTS HERE
>                 PersistentField field = fld.getPersistentField();
>                 Class type = field.getType();
> 
> 
>                 // AnonymousPersistentFields will *always* have a null 
> type according to the
>                 // javadoc comments in AnonymousPersistentField.getType()
>                 if (type == null) {
>                         //  anonymous field never represents a primitive 
> field with value 0, but will return a null type
>                         return false;
>                 }
>             result = type.isPrimitive();
>             // PATCH ENDS HERE
>         }
>         // TODO: Do we need this check?? String could be nullified, why 
> should we assume
>         // it's 'null' on empty string?
>         else if((aValue instanceof String) && (((String) 
> aValue).length() == 0))
>         {
>             result = fld.isPrimaryKey();
>         }
>         return result;
>     }
> 
> Is this a safe patch?
> Is there a case where aValue could pass the aValue instanceof Number) && 
> (((Number) aValue).longValue() == 0 checks and yet still produce a null 
> type other than when it is an anonymous key?
> 
> should we expect a patch like this to appear in the CVS or next release?
> 
> thanks everyone!
> 
> 
> 
>> should be ok - except one difference: He always return 'true' for 
>> anonymous fields, I would suggest always return 'false', because the 
>> anonymous field never represents a primitive field with value 0.
>>
>> regards,
>> Armin
>>
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
> 
> 
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: Broker Helper -> representsNull

Posted by Brian Latimer <bl...@apcc.aspca.org>.
Inserting the following patch into my BrokerHelper class fixed my problem.

     public boolean representsNull(FieldDescriptor fld, Object aValue)
     {
         if(aValue == null) return true;

         boolean result = false;
         if(((aValue instanceof Number) && (((Number) aValue).longValue() 
== 0)))
         {
             //result = fld.getPersistentField().getType().isPrimitive();
             // PATCH STARTS HERE
                 PersistentField field = fld.getPersistentField();
                 Class type = field.getType();


                 // AnonymousPersistentFields will *always* have a null 
type according to the
                 // javadoc comments in AnonymousPersistentField.getType()
                 if (type == null) {
                         //  anonymous field never represents a primitive 
field with value 0, but will return a null type
                         return false;
                 }
             result = type.isPrimitive();
             // PATCH ENDS HERE
         }
         // TODO: Do we need this check?? String could be nullified, why 
should we assume
         // it's 'null' on empty string?
         else if((aValue instanceof String) && (((String) aValue).length() 
== 0))
         {
             result = fld.isPrimaryKey();
         }
         return result;
     }

Is this a safe patch?
Is there a case where aValue could pass the aValue instanceof Number) && 
(((Number) aValue).longValue() == 0 checks and yet still produce a null 
type other than when it is an anonymous key?

should we expect a patch like this to appear in the CVS or next release?

thanks everyone!



>should be ok - except one difference: He always return 'true' for 
>anonymous fields, I would suggest always return 'false', because the 
>anonymous field never represents a primitive field with value 0.
>
>regards,
>Armin
>



---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: Broker Helper -> representsNull

Posted by Armin Waibel <ar...@apache.org>.
Hi,

Thomas Dudziak wrote:

> Brian Latimer wrote:
> 
>> I get a NPE when representsNull is called on an anonymous field with a 
>> value of 0.
> 
> 
> 
> I can tell at least why this is happening: anonymous fields have no 
> representation in the java classes, and thus no associated java field 
> type (AnonymousPersistentField.getType() always returns null). So if 
> you're using numeric values in the anonymous field, the representsNull 
> method tries to check whether the type of the anonymous field is 
> primitive which obviously fails.
> 
> I'm no expert on anonymous fields, but the solution could be to provide 
> a reverse lookup "jdbc type -> java type" for anonymous fields which 
> provides a sensible java type (in your case probably Double or BigDecimal).
> 

Anonymous fields doesn't have a representation in the persistent object, 
thus it will never be a primitive java field pendant. Thus I think the 
patch provided by Phil Denis

I would suggest the following patch:
     public boolean representsNull(FieldDescriptor fld, Object aValue)
     {
         if(aValue == null) return true;

         boolean result = false;
         if(((aValue instanceof Number) && (((Number) 
aValue).longValue() == 0)))
         {
                 // PATCH STARTS HERE
                 PersistentField field = fld.getPersistentField();
                 Class type = field.getType();

                 // AnonymousPersistentFields will *always* have a null 
type according to the
                 // javadoc comments in AnonymousPersistentField.getType()
                 if (type == null) {
                         return true;
                 }
             result = type.isPrimitive();
                 // PATCH ENDS HERE
         }
         else if((aValue instanceof String) && (((String) 
aValue).length() == 0))
         {
             result = fld.isPrimaryKey();
         }
         return result;
     }

should be ok - except one difference: He always return 'true' for 
anonymous fields, I would suggest always return 'false', because the 
anonymous field never represents a primitive field with value 0.

regards,
Armin


> Tom
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
> 
> 
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: Broker Helper -> representsNull

Posted by Thomas Dudziak <to...@first.fhg.de>.
Brian Latimer wrote:

> I get a NPE when representsNull is called on an anonymous field with a 
> value of 0.


I can tell at least why this is happening: anonymous fields have no 
representation in the java classes, and thus no associated java field 
type (AnonymousPersistentField.getType() always returns null). So if 
you're using numeric values in the anonymous field, the representsNull 
method tries to check whether the type of the anonymous field is 
primitive which obviously fails.

I'm no expert on anonymous fields, but the solution could be to provide 
a reverse lookup "jdbc type -> java type" for anonymous fields which 
provides a sensible java type (in your case probably Double or BigDecimal).

Tom


---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: Broker Helper -> representsNull

Posted by Brian Latimer <bl...@apcc.aspca.org>.
I get a NPE when representsNull is called on an anonymous field with a 
value of 0.

I look forward to a patch or work-around for this issue.





---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: Broker Helper -> representsNull

Posted by Brian Latimer <bl...@apcc.aspca.org>.
This issue looks like it could be the cause of my recent problems as well.



At 03:46 AM 06/17/2004, you wrote:
>Hi Denis,
>
>I will try to write a test case for this problem, could please exactly 
>describe what to do (pseudo code) to reproduce this bug.
>
>regards,
>Armin



---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: Broker Helper -> representsNull

Posted by Armin Waibel <ar...@apache.org>.
Hi Denis,

I will try to write a test case for this problem, could please exactly 
describe what to do (pseudo code) to reproduce this bug.

regards,
Armin

DenisP@richer.ca wrote:
> Hi all,
> 
> I've just upgraded to RC7, and I'm now getting the following error on a 
> call to PersistenceBroker.retrieveAllReferences():
> java.lang.NullPointerException
>                  at 
> org.apache.ojb.broker.util.BrokerHelper.representsNull(BrokerHelper.java:260)
>                  at 
> org.apache.ojb.broker.core.QueryReferenceBroker.hasNullifiedFK(QueryReferenceBroker.java:446)
>                  at 
> org.apache.ojb.broker.core.QueryReferenceBroker.getReferencedObjectIdentity(QueryReferenceBroker.java:410)
>                  at 
> org.apache.ojb.broker.core.QueryReferenceBroker.retrieveReference(QueryReferenceBroker.java:334)
>                  at 
> org.apache.ojb.broker.core.QueryReferenceBroker.retrieveReferences(QueryReferenceBroker.java:396)
>                  at 
> org.apache.ojb.broker.core.PersistenceBrokerImpl.retrieveAllReferences(PersistenceBrokerImpl.java:1145)
>                  at ...
> 
> It seems that the problem is in the BrokerHelper.representsNull method. 
> The field it is complaining about is "reportNumber", which is an anonymous 
> field in the target class.  Here's my class descriptor:
> <class-descriptor
>     class="ca.richer.domain.OutOfService"
>     table="FDJ8CPP"
> 
>     ... other fields omitted for brevity ...
>     <field-descriptor
>         name="reportNumber"
>         column="J8RZNB"
>         jdbc-type="DECIMAL"
>         precision="20"
>         scale="0"
>         access="anonymous"
>     >
>     </field-descriptor>
>     <field-descriptor
>         name="lineSequence"
>         column="J8R0NB"
>         jdbc-type="DECIMAL"
>         precision="20"
>         scale="0"
>         access="anonymous"
>     >
>     </field-descriptor>
>     <reference-descriptor
>         name="defectLine"
>         class-ref="ca.richer.domain.DefectLine"
>     >
>         <foreignkey field-ref="reportNumber"/>
>         <foreignkey field-ref="lineSequence"/>
>     </reference-descriptor>
> </class-descriptor>
> 
> The problem is that AnonymousPersistentField.getType() always returns 
> null, so a NullPointerException is thrown when the BrokerHelper tries to 
> determine if this field represents a primitive type.
> 
> I would suggest the following patch:
>     public boolean representsNull(FieldDescriptor fld, Object aValue)
>     {
>         if(aValue == null) return true;
> 
>         boolean result = false;
>         if(((aValue instanceof Number) && (((Number) aValue).longValue() 
> == 0)))
>         {
>                 // PATCH STARTS HERE
>                 PersistentField field = fld.getPersistentField();
>                 Class type = field.getType();
> 
>                 // AnonymousPersistentFields will *always* have a null 
> type according to the
>                 // javadoc comments in AnonymousPersistentField.getType()
>                 if (type == null) {
>                         return true;
>                 }
>             result = type.isPrimitive();
>                 // PATCH ENDS HERE
>         }
>         else if((aValue instanceof String) && (((String) aValue).length() 
> == 0))
>         {
>             result = fld.isPrimaryKey();
>         }
>         return result;
>     }
> 
> Maybe I've overlooked something, but if this method is ever called with an 
> anonymous field with a value of 0, it will surely throw a NPE.  Thoughts?
> 
> Thanks,
> Phil Denis

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org