Forum Discussion

bhupen's avatar
bhupen
Advisor
10 years ago

NonStringException coming.

Hi

 

Iam showing notifcation for user. On stage environment its working fine but when I went on production its showing following exception:

 

NonStringException:For "?length" left-hand operand: Expected a string or something automatically convertible to string (number, date or boolean), but this evaluated to a sequence+hash (wrapper: f.e.dom.NodeListModel):
==> string  [in template "pdc6_helpers.ftl" at line 77, column 12]

The failing instruction (print stack trace for 6 more):
==> #if (string?length < limit?number)  [in template "pdc6_helpers.ftl" in function "truncateMaybe" at line 77, column 5]

 

I saw there is error on "pdc6_helpers.ftl" in function "truncateMaybe" at line 77. I have written following code:

<#function truncateMaybe string, append = '...'>
    <#if ( string?length < 30 ) >
        <#return string>
    <#else>
        <#return string?substring( 0, 30 ) + append>
    </#if>
</#function>

 

As per my consideration i think some attribute is coming null thats why it is appearing.

 

I think "string?has_content" content can solve this but for my case its not working.

 

Following is the code where iam showing notification.

  <#assign subject = truncateMaybe(notification.target.entity.subject) />

  • OlivierS's avatar
    OlivierS
    Lithium Alumni (Retired)

    bhupen 

     

    Not sure if that helps or not.

     

    I think you're correct, the string is null, hence the error.

     

    Maybe rather than using ?has_content, try the below:

     

    Checks if the attribute of the object is null:

    <#if object.attribute??></#if>

     

    Checks if object or attribute is null:

    <#if (object.attribute)??></#if>

     

    Hope this helps !

    • bhupen's avatar
      bhupen
      Advisor
      @olivier thats the case iam also assuming. Some time subject comes null that's why exception is coming. But iam not sure how to stop that null value or make such condition that it shouldn't show exception.
      • OlivierS's avatar
        OlivierS
        Lithium Alumni (Retired)

        Have you tried


        <#function truncateMaybe string, append = '...'>
        <#if (string.attribute)??>
        <#if ( string?length < 30 ) >
        <#return string>
        <#else>
        <#return string?substring( 0, 30 ) + append>
        </#if>
        </#if>
        </#function>

         

        You can probably combine the two conditions in one if statement ...

        I'm not a freemarker expert, this is just a guess ...