简体   繁体   中英

Getting NPE while setting headers in post request

I'm trying to draft a post request and while doing so it is giving NPE. I'm using rest assured and cucumber.

PFB code from SalesCRM.java:

HashMap <String, String> headers;

JSONObject payload = new JSONObject();
BigInteger endPlacementTimestamp = new BigInteger("1593611640000");
BigInteger startPlacementTimestamp = new BigInteger("1594216440000");
payload.put("endPlacementTimestamp",endPlacementTimestamp );
payload.put("startPlacementTimestamp",startPlacementTimestamp);
headers.put("authorization","Bearer cd70f469-b963-48f1-9486-fe5d5e3c443b");//line76
response = (Response) AuxiliarMethods.postRequest(url,values,headers,"application/json",payload.toJSONString());

Stack trace:

java.lang.NullPointerException

at locustTask.SalesCRM.execute(SalesCRM.java:76)

at com.github.myzhan.locust4j.AbstractTask.run(AbstractTask.java:63)

at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)

at java.util.concurrent.FutureTask.run(FutureTask.java:266)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)

at java.lang.Thread.run(Thread.java:748)

The issue is not in that code.

As you can see here: at locustTask.SalesCRM.execute(SalesCRM.java:76)

It means that the NPE is happening in class SalesCRM line 76 .

UPDATE

Now it is more clear, thank you.

Your problem is because your are trying to call the method '.put' for a object that was not initialized.

Your object “headers” is null.

To fix it just initialize the object.

HashMap <String, String> headers = new HashMap<>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM