add code to bypass the null pointer exception due to null  key retrieval

int nposision = tempOrder.get(pid);

the above line will generate a NullPointerException when 'pid' is not contains in the 'tempOrder' ,i.e. there is no key by the value 'pid'.
Added a code part to check that and avoid throwing an exception.