Forum Discussion

bhupen's avatar
bhupen
Advisor
11 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) />

6 Replies

  • OlivierS's avatar
    OlivierS
    Lithium Alumni (Retired)
    11 years ago

    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
    11 years ago
    @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)
    11 years ago

    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 ...

  • bhupen's avatar
    bhupen
    Advisor
    11 years ago
    @olivier yes. I did try that but its not working to me
  • OlivierS's avatar
    OlivierS
    Lithium Alumni (Retired)
    11 years ago

    bhupen 

     

    It won't explain why it's working on stage and not on prod, but have you tried to not use 'string' for your sting name?

    I don't know how freemarker works, but it might be a reserved name ... 

  • bhupen's avatar
    bhupen
    Advisor
    11 years ago

    Some how I found that issue but now other issue comes up. following is the code I verified, one entity is missing in that code thats why its showing exception, just because two rest api is used.

    Also I got one solution but not sure how can I embed that solution in it.

    <#case 'mentions'>                        
                                <#assign date    = relativeOrAbsoluteDate( notification.target.time_stamp ) />
                              <#if notification.target.entity.subject??>                           
                               <#assign subject = truncateMaybe(notification.target.entity.subject) />
                                   <#assign link    = notification.target.entity.@view_href />
                                <a href="${link}">
                                    <h4>${notification.target.actor.login},
                                    ${text.format('pdc6.notifications.mentioned_string')},                                
                                    ${subject?string}</h4>
                                      <div class="details">
                                        <b class="notification-icon mono-icons-bg notification-mention"></b> ${date}
                                    </div>
                                    <#else>
                                <#return 1 />
                                    </#if>
                                </a>
                            <#break>

     

    Following is the error:

    evaluated to null or missing: ==> truncateMaybe(notification.target.entity.subject)

    Solution for this:

    Tip: If the failing expression is known to be legally null/missing, either specify a default value with myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing</#if>

    Complication: Iam not able to set condition for this. As per tip.