Search code examples
ibm-midrangerpglerpg

Can anyone explain why decimal data error is happening with %char()?


To begin with, I am not looking for just a solution to fix this error message. I would be far more appreciative of being directed to documentation explaining this error than a solution to the code problem. I need to understand WHY this is happening because I cannot find any documentation which explains what is causing this error. I'm relatively new to RPG (less than 2 years) so I'm at a reasonable level of ignorance on this.

I am working on some legacy RPG code which was recompiled in October, 2017. This code has been in place since 2013.

My issue is that my users have recently (within the past 3-4 months) begun to get decimal data errors reported on this line of code:

C                   EvalR     NewValueA = %char(NewValue)

The dumps from the error message shows that NewValue is empty. Not 0, just empty. In my experience with other languages, that's a NULL. None of my resources (including Google) give any indication how NULL is handled by %char() in RPGLE. It would be easy to assume that %char() doesn't handle NULL gracefully and move on, but that's hard to justify in a code review.

NewValue is defined as 7 0 decimal. NewValueA is an 8 character char.

Thanks in advance to all who have productive comments to offer.


Solution

  • The current version of RPG doesn't fully support NULL.

    Specifically, a stand-a-lone variable can't be NULL. Only variables which originate directly or indirectly (via LIKE, LIKEDS, or LIKEREC) in an externally described file (table), and which are defined in that file to allow NULL, can be NULL in an RPG program; and even that requires that the program be compiled with the ALWNULL(*USRCTL) option. See Database Null Value Support

    While this support has been available for a long time (the beginning of ILE?), most legacy files and related RPG code don't use NULL.

    So NewValue isn't likely to be empty (NULL).
    What is the hex value you see in debug?

    As jtaylor's answer mentions, most decimal data errors come from bad data being written to legacy DDS defined files. However, in that case, the error would have been thrown when the file was read, not on the line you've shown. (Unless the file is being read directly into a data structure.)

    Where is NewValue defined? How does it get a value?

    For a program variable, the most likely reason for a decimal data error is that the numeric variable is in a data structure that doesn't specify the INZ keyword. In that case, the data structure gets initialized to all blanks.