CF7 : Java methods returning 'null' -UndefinedVariableException on assignment - is it a bug?

Ask a Question related to Coldfusion - Advanced Techniques, Design and Development.

  1. #1

    Default Re: CF7 : Java methods returning 'null' -UndefinedVariableException on assignment - is it a bug?

    well don't have it return a NULL, return something else or simply check if that var isDefined(), setting a cf var to NULL effectively destroys it as far as cf is concerned.

    PaulH Guest

  2. Similar Questions and Discussions

    1. #25503 [Bgs]: SQLite_query() returning NULL, not FALSE
      ID: 25503 User updated by: o_gangrel at hotmail dot com Reported By: o_gangrel at hotmail dot com Status: Bogus Bug...
    2. #25503 [Opn->Bgs]: SQLite_query() returning NULL, not FALSE
      ID: 25503 Updated by: wez@php.net Reported By: o_gangrel at hotmail dot com -Status: Open +Status: ...
    3. #25503 [NEW]: SQLite_query() returning NULL, not FALSE
      From: o_gangrel at hotmail dot com Operating system: Windows 98 PHP version: 5.0.0b1 (beta1) PHP Bug Type: SQLite related...
    4. OnEditCommand - .FindControl Returning Null
      cross posted in datagrid group. Inside <columns/> in my datagrid, I have the following template column <asp:templatecolumn HeaderText="To Be...
    5. OnEditCommand - FindControl returning Null
      Inside <columns/> in my datagrid, I have the following template column <asp:templatecolumn HeaderText="To Be Completed By"> <itemtemplate>...
  3. #2

    Default Re: CF7 : Java methods returning 'null' -UndefinedVariableException on assignment - is it a bug?

    Thanks for the input, however - this API is used by Java "clients" other than
    CF, and in Java, returning a variable with a value of null is perfectly
    acceptable. I don't want to have to call the method twice - I guess what I'm
    asking is - is there a reason why a null value from a Java method can't simply
    set the object to undefined, instead of throwing this bad exception?

    pmularien Guest

  4. #3

    Default Re: CF7 : Java methods returning 'null' -UndefinedVariableException on assignment - is it a bug?

    not following. why would you have to call it twice? if that var isn't defined then the method returned a NULL. game over.
    PaulH Guest

  5. #4

    Default Re: CF7 : Java methods returning 'null' -UndefinedVariableException on assignment - is it a bug?

    Right. The problem is that the exception occurs on -assignment- of the value
    null to the variable. The exception is thrown by the <cfset ... = > tag, and
    thus I can never test whether e.g. IsDefined(obj). Make sense?

    pmularien Guest

  6. #5

    Default Re: CF7 : Java methods returning 'null' -UndefinedVariableException on assignment - is it a bug?

    i guess the var keyword is causing cf to evaluate that object
    prematurely--though i'm not seeing this (in the simple test below) on cf 7.
    what version of cf? i recall building a dumbed down java class to produce/test
    for nulls and it seemed to work as i expected. i'm at home now, i'll try to dig
    out that code and test again when i get into the office tomorrow.

    <cfscript>
    function z(){
    var x=javacast("null","");
    if (isDefined("x"))
    return true;
    else
    return false;
    }
    writeoutput("is x defined? #yesNoFormat(z())#");
    </cfscript>

    PaulH Guest

  7. #6

    Default Re: CF7 : Java methods returning 'null' -UndefinedVariableException on assignment - is it a bug?

    ah, you're right. when i cracked open that java code i saw i'd built a method
    to detect nulls. and i understand why you mean to call that method twice
    (sometimes i am a monkey's uncle). does the workaround below make any sense?


    import java.lang.Object;

    public class nulls {
    public final static Object returnNull(){
    return null;
    }

    public final boolean isNull(Object varToTest) {
    if (varToTest == null)
    return true;
    else
    return false;
    }

    }

    <!--- round-a-bout --->
    <cfscript>
    function killMeDead() {
    var nullUtil=createObject("java","nulls");
    var nullVar=false;
    var nulledvar=false;
    try {
    nullVar=nullUtil.returnNull();
    nulledVar=nullVar;
    }
    catch (Any e) {
    nulledVar=true;
    }
    return nulledVar;
    }
    writeoutput("did a null kill me? #killMeDead()#");
    </cfscript>

    PaulH Guest

  8. #7

    Default Re: CF7 : Java methods returning 'null' -UndefinedVariableException on assignment - is it a bug?

    What are the circumstances under which the Java method call returns a null, and is it a straight-up "return null;" result?
    cf.Objective Guest

  9. #8

    Default Re: CF7 : Java methods returning 'null' -UndefinedVariableException on assignment - is it a bug?

    I ended up taking a slightly different approach - basically I created a wrapper
    function in CF to check for nulls - very trivial:

    ...
    <cffunction name="checkJavaNull" access="public" returntype="any"
    output="false" hint="Checks whether a java object is null and returns
    appropriately">
    <cfargument name="obj" type="any" required="no" hint="The java object">

    <cfif IsDefined("arguments.obj")>
    <cfreturn arguments.obj/>
    <cfelse>
    <cfreturn ""/>
    </cfif>
    </cffunction>
    ...

    Now for any Java call that may return null as a matter of course, I just wrap
    it with this method (all the CFCs that are interacting with Java have a common
    superclass, so it looks just like a regular CF function call:

    ...
    <cfset var obj = checkJavaNull(javaObj.javaMethodWhichMayReturnNull (...)) />
    ...


    Apparently (as we all know) CF is OK with method arguments being undefined -
    so we simply check within the checkJavaNull method whether or not a valid
    argument was passed in - if not, it must have been Java null, so just return CF
    null, which == "".

    Thanks, Paul, for the suggestion of moving the null detection into Java - IMHO
    being able to do it in CF is more natural for our CF developers/users, so I
    chose to go with a CF solution. This happens on either CF 6.1 or 7 - and it
    doesn't seem to matter whether I have the var declaration there or not - that
    is - both of these will fail in the same way -

    <cfset var obj = willReturnNull(...)/>

    vs.

    <cfset var obj = ""/>
    ...
    <cfset obj = willReturnNull(...)/>


    Hope this solution helps someone else!

    pmularien Guest

  10. #9

    Default Re: CF7 : Java methods returning 'null' -UndefinedVariableException on assignment - is it a bug?

    BTW, IMHO this is still a CF bug - I think CF should perform this null check
    behind the scenes for us when processing results from any Java method. But
    until/if this is fixed within CF, the workaround is fine for us.

    pmularien Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139