1

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
    Can you provide exception stack trace? – Alexey R. Nov 28 '18 at 16:24
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, privacy policy and cookie policy

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