cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

CPI: Groovy - java.io.Reader error: Cannot invoke method eachLine() on null object

suwandi_cahyadi
Contributor
5,563

Hi,

I have seen some blog post where people are using java.io.Reader in Groovy to read the body content. For example:

def body = message.getBody(java.io.Reader)<br>body.eachLine { line -><br>	.... Some code here     <br>}

But then if I try the same code, I get the following error message:

javax.script.ScriptException: java.lang.Exception: java.lang.NullPointerException: Cannot invoke method eachLine() on null object@ line 26 in script1.groovy, cause: java.lang.NullPointerException: Cannot invoke method eachLine() on null object

Is there something that I missed?

EDIT: I realized it is error if I used conversion XML to CSV before the script step. If I remove the conversion step this error does not occur.

Thank you.

Accepted Solutions (0)

Answers (4)

Answers (4)

shivaprasad1
Participant
0 Likes

Hi Suwandi,

Is there any way that you have figured out the issue?

I am also facing the same issue

Please let me know if you find any solution

Thanks

Shiva

Muniyappan
Active Contributor
0 Likes

Can you please give us the groovy code you used and input payload you sent?

suwandi_cahyadi
Contributor
0 Likes

Hi,

This is the groovy script:

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
def Message processData(Message message) {   
    //Body 
    def objReader = message.getBody(java.io.Reader);
    def array = []
    
    objReader.eachLine { String line ->
        array.add(line)
    }
   
    return message;    
}<br>

And this is the input payload:

"0047224526,449 ,0006429 ,LIV ,20210429092751"
"0047241670,449 ,0006429 ,LIV ,20210420092958"
"0047242774,437 ,0006429 ,LIV ,20210429101150"
"0047244164,321 ,0006429 ,LIV ,20210420093320" <br>

Error is at line 26: objReader.eachLine { String line ->

I realized it is error if I used conversion XML to CSV before the script step. If I remove the conversion step this error does not occur.

Thank you.

Muniyappan
Active Contributor
0 Likes

Well. I have tried to run the code with given input. I dont see issue with this.

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
def Message processData(Message message) {
//Body
def objReader = message.getBody(java.io.Reader);
def array = []

objReader.eachLine { String line ->
array.add(line)
}

return message;
}

input

"0047224526,449 ,0006429 ,LIV ,20210429092751"
"0047241670,449 ,0006429 ,LIV ,20210420092958"
"0047242774,437 ,0006429 ,LIV ,20210429101150"
"0047244164,321 ,0006429 ,LIV ,20210420093320" <br>
0 Likes

Hi Suwandi,

let us know your problem solved or new problem.

Warm regards, Avinash

0 Likes

Hi Suwandi,

could you read the message body as string like below.

def body = message.getBody(java.lang.String)as String;

reg, avinash

suwandi_cahyadi
Contributor
0 Likes

Hi Avinash,

Yes, it is possible. But in this case I want to read line by line, which will make the transform operation easier.

Thank you.