Null Pointer Exception


#1

I am new to Katalon and was trying to figure out the API Testing. After reading some posts, I have attempted testing and verifying APIs. However, it looks like a stalemate for the verification. Can someone please suggest where am I going wrong ?

Here is my code -

import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint
import com.kms.katalon.core.checkpoint.CheckpointFactory as CheckpointFactory
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.testcase.TestCase as TestCase
import com.kms.katalon.core.testcase.TestCaseFactory as TestCaseFactory
import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.testdata.TestDataFactory as TestDataFactory
import com.kms.katalon.core.testdata.ObjectRepository as ObjectRepository
import com.kms.katalon.core.testobject.ObjectRepository
import com.kms.katalon.core.testobject.RequestObject
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WSBuiltInKeywords
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUIBuiltInKeywords
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import internal.GlobalVariable as GlobalVariable
import com.kms.katalon.core.testobject.ResponseObject
import com.kms.katalon.core.testobject.RequestObject
import com.kms.katalon.core.testobject.impl.HttpTextBodyContent
import com.kms.katalon.core.webservice.verification.WSResponseManager
import static org.assertj.core.api.Assertions.*
import groovy.json.JsonSlurper

//POST Object
def request = (RequestObject)findTestObject(‘POST’)
String body = ‘{“requestid”: “12345”,“omid”: “dks”,“deviceid”: “sis”,“uid”: “sis_4”,“urls”: [“love.iwon.com”],“queries”: [“coreinfo”],“aca”: 1,“core”: 1,“xml”: 0}’

request.setBodyContent(new HttpTextBodyContent(body,“UTF-8”,“application/json”))
WS.sendRequest(request)

ResponseObject response = WSResponseManager.getInstance().getCurrentResponse()

WS.verifyResponseStatusCode(response, 200)
assertThat(response.getStatusCode()).toString()

String jsonString = response.getResponseText()
JsonSlurper slurper = new JsonSlurper()
Map parsedJson = slurper.parseText(body)

Here is the error -

01-04-2019 01:02:59 PM Test Cases/CreateAndVerifyPOST

Elapsed time: 6.410s

Test Cases/CreateAndVerifyPOST FAILED.
Reason:
java.lang.NullPointerException
at com.kms.katalon.core.webservice.verification.WSResponseManager.getCurrentResponse(WSResponseManager.java:49)
at com.kms.katalon.core.webservice.verification.WSResponseManager$getCurrentResponse$0.call(Unknown Source)
at CreateAndVerifyPOST.run(CreateAndVerifyPOST:38)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:328)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:319)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:298)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:290)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:224)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:106)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:97)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1546632173690.run(TempTestCase1546632173690.groovy:22)


#2

I have never seen such code. Rather try the following:

ResponseObject response = WS.sendRequest(request)

#3

Thank you very much. It worked charmingly. I was able to verify the results.
Appreciate your help.


#4

Using WSResponseManager.getInstance().getCurrentResponse() in test case is not recommended. It is a utility method to get the response for verification, as described here https://docs.katalon.com/katalon-studio/docs/verification-snippets.html#using-verification-snippets


#5

Which means, ‘WSResponseManager.getInstance().getCurrentResponse()’ belongs to codeless automation verfication. Thank you for the suggestion.

Per @kazurayam , My test was working good and when I tried rerunning today, the script is throwing the following error. Any thoughts, please ?

Reason:
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object ‘true’ with class ‘java.lang.Boolean’ to class ‘com.kms.katalon.core.testobject.ResponseObject’
at CreateAndVerifyPOST.run(CreateAndVerifyPOST:46)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:328)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:319)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:298)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:290)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:224)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:106)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:97)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1546987955768.run(TempTestCase1546987955768.groovy:22)


#6

Could you share your test case code?


#7

What is “CreateAndVerifyPOST” ? Your custom keyword? Please share the source code as well.


#8

Sorry for the delay. Here is the code :- The ‘CreateAndVerifyPOST’ is my test case. I have created it for verifying the POST Call and in the test case, the Object of the POST call is called.

import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint
import com.kms.katalon.core.checkpoint.CheckpointFactory as CheckpointFactory
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.testcase.TestCase as TestCase
import com.kms.katalon.core.testcase.TestCaseFactory as TestCaseFactory
import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.testdata.TestDataFactory as TestDataFactory
import com.kms.katalon.core.testdata.ObjectRepository as ObjectRepository
import com.kms.katalon.core.testobject.ObjectRepository
import com.kms.katalon.core.testobject.RequestObject
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WSBuiltInKeywords
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUIBuiltInKeywords
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import internal.GlobalVariable as GlobalVariable
import com.kms.katalon.core.testobject.ResponseObject
import com.kms.katalon.core.testobject.RequestObject
import com.kms.katalon.core.testobject.impl.HttpTextBodyContent
import com.kms.katalon.core.webservice.verification.WSResponseManager
import static org.assertj.core.api.Assertions.*
import groovy.json.JsonSlurper
/import java.util.List
import java.util.concurrent.locks.Condition
import java.util.ArrayList
/

//POST Object
def request = (RequestObject)findTestObject(‘POST’)
String body = ‘{“requestid”: “12345”,“oemid”: “brightcloudsdk”,“deviceid”: “deviceid_isis”,“uid”: “isisiteration_4”,“urls”: [“love.iwon.com”],“queries”: [“getrepinfo”],“a1cat”: 1,“reputation”: 1,“xml”: 0}’

try {
request.setBodyContent(new HttpTextBodyContent(body,“UTF-8”,“application/json”))
} catch(Exception Ex) {
println (Ex.detailMessage)
}
WS.sendRequest(request)

ResponseObject response = WS.sendRequest(request)

ResponseObject respCode = WS.verifyResponseStatusCode(response, 200)
println respCode
assertThat(response.getStatusCode()).toString()

String jsonString = response.getResponseText()
JsonSlurper slurper = new JsonSlurper()
Map parsedJson = slurper.parseText(body)

/import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.testobject.ResponseObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
/

/ResponseObject response = WS.sendRequest(findTestObject(‘test_object_id’))
println response.getStatusCode()
println response.getElapsedTime()
/


#10

The second line above is wrong. It should be:

Boolean statusIs200 = WS.verifyResponseStatusCode(response, 200)

#11

Thank you very much - the problem is fixed. Just wondering, is there a documentation for dos and don’ts. Please help provide, it will be a great help. Appreciate your help.


#12

In this case, the above message tells you all you need to pay attention to.
Just get used to this unfriendly format.


#13

Thank you very much. I am also trying for API Data Driven Testing. For this, I am following Raghav Pal’s video. I think I followed it fairly well. The test by it self is passing, however when I added it to Test Suite and using the Excelsheet, for some reason the test is failing due to null pointer exception. Don’t know where am I going wrong. Any thoughts, please ?

verifyElementPropertyValue(response, “results[0].queries.getinfo.cats[0].catid”, category) FAILED.
Reason:
com.kms.katalon.core.exception.StepFailedException: Unable to verify element property value (Root cause: java.lang.NullPointerException: Cannot invoke method getAt() on null object)
at com.kms.katalon.core.keyword.internal.KeywordMain.stepFailed(KeywordMain.groovy:32)
at com.kms.katalon.core.keyword.internal.KeywordMain.stepFailed(KeywordMain.groovy)
at com.kms.katalon.core.keyword.internal.KeywordMain.runKeyword(KeywordMain.groovy:56)
at com.kms.katalon.core.webservice.keyword.builtin.VerifyElementPropertyValueKeyword.verifyElementPropertyValue(VerifyElementPropertyValueKeyword.groovy:48)
at com.kms.katalon.core.webservice.keyword.builtin.VerifyElementPropertyValueKeyword.execute(VerifyElementPropertyValueKeyword.groovy:43)
at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:53)
at com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords.verifyElementPropertyValue(WSBuiltInKeywords.groovy:123)
at com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords$verifyElementPropertyValue$0.call(Unknown Source)
at CCRefresh-UrlGetInfo.run(CCRefresh-UrlGetInfo:22)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:328)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:319)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:298)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:290)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:224)
at com.kms.katalon.core.main.TestSuiteExecutor.accessTestCaseMainPhase(TestSuiteExecutor.java:129)
at com.kms.katalon.core.main.TestSuiteExecutor.accessTestSuiteMainPhase(TestSuiteExecutor.java:112)
at com.kms.katalon.core.main.TestSuiteExecutor.execute(TestSuiteExecutor.java:81)
at com.kms.katalon.core.main.TestCaseMain.startTestSuite(TestCaseMain.java:149)
at com.kms.katalon.core.main.TestCaseMain$startTestSuite$0.call(Unknown Source)
at TempTestSuite1547192617142.run(TempTestSuite1547192617142.groovy:36)


#14

Your previous question has been solved. OK?

If you have new question, then you should create a new Forum discussion. You should not continue adding comments to the previous discussion. The title and contents should be in sync, as you know.

Please create new one.

Also, please share your test case code as well. Pasting log is important but not enough to find out why you are told (Root cause: java.lang.NullPointerException: Cannot invoke method getAt() on null object)

Please have a look at the guideline [TIP] My test is BROKEN! NOTHING WORKS! PLEASE HELP ME!

Which version of Katalon Studio do you use? the latest 5.10.1? If not, is it possible to upgrate it? because I suppose the 5.10.1 would give you more informative error message.


#15

Yes, the previous one was solved. I will create a new post with the test case as well. Sorry about that. Also, I will first upgrade Katalon to 5.10.1 and then recreate the test case and run it and see. If it still a problem, I will post it in this forum. Thank you very much for the support.