When we declare a Record variable as nullable:
- The Boolean value of Record is TRUE, if it is initialized with a NULL Keyword.
- The Boolean value of the record is FALSE, if it is initialized with any value other than NULL.
To indicate a variable as nullable, append a question mark (?) to the type when you declare it. A NULL value in a table is a value in a field that appears to be blank. The NULL value can cause problems when selecting data. However, comparing an unknown value to any other value can lead to an unexpected result if not handled properly. So, handling a null flag in a nullable record is important. In RBD 9.6.1, the Null Flag in the Nullable record was handled. This blog shows how EGL handles NULL flags in Nullable Records.
Steps to Create General Java Project with Null Flag in Record
Step 1: Create a General Project with Java as runtime
Follow the below steps to create a general project with Java as runtime.
1. Navigate to File->New->EGL project.
2. EGL Project window will appear.
3. Enter the Project name.
4. Select the General Project option and click Next.
5. Select Java in the Target runtime Platform.
6. Click Finish.
The Java General Project has been created successfully as shown below.
Step 2: Create a Program
Follow the below steps to create a program with a Record definition and check the Boolean value returned by the record after assigning Record fields with different data types.
1. Go to Project Explorer.
2. Under the Project Explorer, navigate to NullRecordTests->EGLSource.
3. Right-click the EGLSource.
4. Go to New->Program.
5. A new EGL Program window will appear.
6. Enter the Package name and EGL source file name.
7. Click Finish.
test1.eglfile has been created successfully with a default code as shown below.
Step 3: Define the Record definition and a function to access Record data
Follow the below steps to define a record:
1. Open test1.egl.
2. Define a Record with two fields and declare the Record as nullable.
2. Assign different datatypes to the Record fields (ex: int, null, string).
3. Pass the Record variable and Boolean value as parameters to the function.
Here MyRecord is a Record, and “myrec” is a Record variable.
When assigning a null keyword to the Record variable, a True Boolean value should be recorded.
myrec = null // null keyword assigned to record
ActualNullFlag = if(myrec=null) =TRUE // True is set to the record
When we assign an Integer value to the Record field, the Boolean value False should be set to record.
myrec. hello = 4 // Record field values are set to 4.
ActualNullFlag = if(myrec=null) =FALSE // False is set to the record.
When we assign a String value to the Record field, the Boolean value False should be set to record.
myrec.hello2 =”Raj” // Record field values is set to 4.
ActualNullFlag = if(myrec=null) =FALSE // False is set to the record.
Source Code:
package pack1;
record MyRecord
hello int;
hello2 string;
end
program test1 type BasicProgram {}
function main()
myrec MyRecord?;
verifyNull(myrec,true);
myrec.hello = 4;
verifyNull(myrec, false);
myrec = null;
verifyNull(myrec, true);
set myrec.hello initial;
verifyNull(myrec, false);
myrec = null;
verifyNull(myrec, true);
move "junk" to myrec.hello2;
verifyNull(myrec, false);
myrec = null;
verifyNull(myrec,false);
end
function verifyNull(arec MyRecord? in, expectedNullFlag boolean)
actualNullFlag boolean = (arec == null);
result STRING;
result = strLib.booleanAsString(actualNullFlag);
syslib.writeStdout(result);
if(actualNullFlag == expectedNullFlag)
Syslib.writestdout("success");
else
Syslib.writestdout("failure");
end
end
end
program Screenshot:
Step 4: Save and Generate the Program
Follow the below steps to save and generate the program:
1. Go to Project Explorer.
2. Under the Project Explorer, right-click on NullRecordTests and select Generate.
Test Program has been generated without errors as shown below.
Step 5: Run the Project as a Java Application
Follow the below steps to run the project as a Java application:
1. Go to Project Explorer.
2. Under the Project Explorer, navigate to NullRecordTests ->EGLGen/JavaSource->test1.java.
3. Right-click test1.java.
4. Go to Run As->Java Application.
Output: In the verifyNull (myrec, Boolean value) function, we compare the Boolean value returned by the record variable and Boolean value parameter. If both values are matched, it returns ‘success’ otherwise, it returns a ‘failure’ message.
A null flag is useful when it is necessary to distinguish between a field with no value and a field with an empty string or a zero value. If you did not handle the null values properly, writing queries to get the desired results is difficult. In this blog, we have handled the null flag in a nullable record to easily write the queries to return the desired result.
Note: Null Flag in the record also applies to COBOL, iSeries, and Rich-UI Project.
Lenka Suresh
QA Engineer – RBD