[x3d-public] X3DJSAIL. Setting field value to null. Ambiguous

Don Brutzman brutzman at nps.edu
Sat Feb 18 15:09:11 PST 2017


On 2/16/2017 2:44 PM, John Carlson wrote:
> Likely this will throw my code for a loop, since I am only specifying the parameter at this point in the code I believe.   I could be wrong.   Anyway, it's likely that there will be some rework.

In most (perhaps all?) cases, if initially defining an SFNode or MFNode field, the default value is empty.  So you might simply be able to skip that step.

>    I trust that clear() returns the object it was called on so the method can be chained?

Good question that led to a whole line of inquiry and some further improvements, as follows.

The shared method clear() for all the MF field objects (such as MFNode, MFVec3f etc.) is found in parent abstract interface MField, so it is has a common return value of void, and thus: no, that is not chainable.

=====================================
http://www.web3d.org/specifications/java/javadoc/org/web3d/x3d/sai/MField.html#clear--

void clear()

Removes all values in the field array, and changes the array size to zero.
====================================

Note that this is one of the interfaces explicitly defined by Java SAI and so must be honored.  No other clear*() methods found in Java SAI.

=====================================
B.4.6 MField
http://www.web3d.org/documents/specifications/19777-2/V3.0/Part2/abstracts.html#MField

public interface MField extends X3DField {

     public int size();

     public void clear();

     public void remove(int index);
}
=====================================

Current implementation for MFNodeObject:

=====================================
/**
* Removes all values in the field array, and changes the array size to zero.
*/
@Override
public void clear()
{
	MFNode = new X3DNode[0];
}
=====================================

Checking further for other utility-method additions:

- fieldObject and fieldValueObject each has a utility method clearChildren()
- Didn't find any kind of a clear() method for SFNodeObject
- actually all SFNode/MFNode fields in all top-level node types also have a utility method clearAttributeName(), for example
   TransformObject has clearChildren() and clearMetadata(), ShapeObject has clearAppearance()clearGeometry() andclearMetadata(), etc.

... and so am adding a consistent clearChildren() method to fieldObject, fieldValueObject, SFNodeObject and MFNodeObject.  Since it this a utility method and has no dependency on the MField interface, these can all be chainable.

Plural name "setChildren()" seems OK for SFNodeObject since a single node there might contain other nodes as well.  Consistency helps.

I also looked at many of the setValue methods and utility constructors, added some necessary handling of null values where missing.  Hopefully got everything.

So that is a better, broader and consistent solution.  Will appear in next build tonite.


> Thanks
>
> John

likewise, that tightened up a number of code blocks to protect against NPE.

> On Feb 16, 2017 2:33 PM, "Don Brutzman" <brutzman at nps.edu <mailto:brutzman at nps.edu>> wrote:
>
>     John, you will find that field-type objects have a clear() method.
>
>     The only place NULL is allowed in X3D spec is when an MFNode array or child SFNode is empty.
>
>     Usually there are traps that look for empty string or null, setting result to default or else throwing an exception as best seems appropriate.
>
>     In general, setting something to null is ambiguous and can easily lead to Null Pointer Exceptions (NPE) if unhandled.  So the library tries to trap all of these accordingly, making it hard to build a broken scene graph.
>
>     Hope this helps.
>
>
>     On 2/15/2017 6:11 PM, yottzumm at gmail.com <mailto:yottzumm at gmail.com> wrote:
>
>         There’s slight ambiguity in setting  field values to null.  We’ll have to cast or provide a
>
>         setValueNull() method.   Let me know if an additional method is provided. Attached example.
>
>
>
>         NancyPrototypes.java:263: error: reference to setValue is ambiguous
>
>                         field0_2_2_0_35.setValue(null);
>
>                                        ^
>
>           both method setValue(String) in fieldObject and method setValue(SFStringObject) in fieldObject match
>
>         NancyPrototypes.java:270: error: reference to setValue is ambiguous
>
>                         field0_2_2_0_37.setValue(null);
>
>                                        ^
>
>           both method setValue(String) in fieldObject and method setValue(SFStringObject) in fieldObject match
>
>         NancyPrototypes.java:544: error: reference to setValue is ambiguous
>
>                         field0_2_6_0_18.setValue(null);
>
>                                        ^
>
>           both method setValue(String) in fieldObject and method setValue(SFStringObject) in fieldObject match
>
>         3 errors
>
>
>
>
>
>     all the best, Don
>     --
>     Don Brutzman  Naval Postgraduate School, Code USW/Br       brutzman at nps.edu <mailto:brutzman at nps.edu>
>     Watkins 270,  MOVES Institute, Monterey CA 93943-5000 USA   +1.831.656.2149 <tel:%2B1.831.656.2149>
>     X3D graphics, virtual worlds, navy robotics http://faculty.nps.edu/brutzman <http://faculty.nps.edu/brutzman>
>


all the best, Don
-- 
Don Brutzman  Naval Postgraduate School, Code USW/Br       brutzman at nps.edu
Watkins 270,  MOVES Institute, Monterey CA 93943-5000 USA   +1.831.656.2149
X3D graphics, virtual worlds, navy robotics http://faculty.nps.edu/brutzman



More information about the x3d-public mailing list