0

Getting Null pointer exception whenever running, unable to resolve.

Below is my code

public static RequestSpecification setFormParam(String formParam, RequestSpecification reqSpec)
    {
        String[] splittedFormParam=formParam.split(",");
        System.out.println("NoOfFormParam:"+splittedFormParam.length);
        for(String s:splittedFormParam)
        {
            System.out.println("FormParam:-"+s);
            String[] keyValue=s.split(":");
            System.out.println("key-->"+keyValue[0]);
            System.out.println("Value-->"+keyValue[1]);
            System.out.println(reqSpec);// reqSpec Always getting null value
            reqSpec.formParam(keyValue[0], keyValue[1]);
            testCaseLogger.get().log(Status.INFO, "Added "+keyValue[0]+" as FormParam with value "+keyValue[1]);
        }


        return reqSpec;

    }

Uses of setFormParam method

public static Response postCandidateResponse(Hashtable<String, String> data) {

        requestSpec=    TestUtils.setFormParam(data.get("formParam"), requestSpec);
        extentLogger().log(Status.INFO, " Method type is: "+data.get("methodType"));
        extentLogger().info("EndPoint is: "+data.get("endPoint"));
        return requestSpec.request(data.get("methodType"),data.get("endPoint"))
                .then()
                .extract()
                .response();

    }
2
  • 2
    Can you provide exception stack trace?
    – Alexey R.
    Nov 28, 2018 at 16:24
  • Have you tried to see what NullPointerException stands for? Have you looked at your stacktrace? See this answer to see how to read stacktraces and understand exceptions: sqa.stackexchange.com/a/51649/1933
    – dzieciou
    May 7, 2023 at 18:11

1 Answer 1

2

Now take a look at setFormParam() method:

System.out.println(reqSpec);// reqSpec Always getting null value
reqSpec.formParam(keyValue[0], keyValue[1]);

and then at postCandidateResponse() that calls setFormParam():

public static Response postCandidateResponse(Hashtable<String, String> data) {

   requestSpec=    TestUtils.setFormParam(data.get("formParam"), requestSpec);

In the code you listed you never initialize original requestSpec.

Then how would you expect it to have non-null value?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.