#vulnerable code 
                                public DesiredCapabilities buildDesiredCapability(String platform,
                                                      String jsonPath) throws Exception {
        final boolean[] flag = {false};
        DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
        JSONArray jsonParsedObject = new JsonParser(jsonPath).getJsonParsedObject();
        Object getPlatformObject = jsonParsedObject.stream().filter(o -> ((JSONObject) o)
                .get(platform) != null)
                .findFirst();
        Object platFormCapabilities = ((JSONObject) ((Optional) getPlatformObject)
                .get()).get(platform);
        ((JSONObject) platFormCapabilities)
                .forEach((caps, values) -> {
                    if ("browserName".equals(caps) && "chrome".equals(values.toString())) {
                        flag[0] = true;
                        try {
                            desiredCapabilities.setCapability("chromeDriverPort",
                                    availablePorts.getPort());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    if ("app".equals(caps)) {
                        if (values instanceof JSONObject) {
                            if (DeviceManager.getDeviceUDID().length()
                                    == IOSDeviceConfiguration.SIM_UDID_LENGTH) {
                                values = ((JSONObject) values).get("simulator");
                            } else if (DeviceManager.getDeviceUDID().length()
                                    == IOSDeviceConfiguration.IOS_UDID_LENGTH) {
                                values = ((JSONObject) values).get("device");
                            }

                        }
                        Path path = FileSystems.getDefault().getPath(values.toString());
                        if (!path.getParent().isAbsolute()) {
                            desiredCapabilities.setCapability(caps.toString(), path.normalize()
                                    .toAbsolutePath().toString());
                        } else {
                            desiredCapabilities.setCapability(caps.toString(), path
                                    .toString());
                        }
                    } else {
                        desiredCapabilities.setCapability(caps.toString(), values.toString());
                    }
                });
        if (DeviceManager.getMobilePlatform().equals(MobilePlatform.ANDROID) && !flag[0]) {
            if (desiredCapabilities.getCapability("automationName") == null
                    || desiredCapabilities.getCapability("automationName")
                    .toString() != "UIAutomator2") {
                desiredCapabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME,
                        AutomationName.ANDROID_UIAUTOMATOR2);
                desiredCapabilities.setCapability(AndroidMobileCapabilityType.SYSTEM_PORT,
                        availablePorts.getPort());
            }
            appPackage(desiredCapabilities);
        } else if (DeviceManager.getMobilePlatform().equals(MobilePlatform.IOS)) {
            String version = iosDevice.getIOSDeviceProductVersion();
            appPackageBundle(desiredCapabilities);
            //Check if simulator.json exists and add the deviceName and OS
            if (DeviceManager.getDeviceUDID().length() == IOSDeviceConfiguration.SIM_UDID_LENGTH) {
                desiredCapabilities.setCapability(MobileCapabilityType.DEVICE_NAME,
                        simulatorManager.getSimulatorDetailsFromUDID(
                                DeviceManager.getDeviceUDID()).getName());
                desiredCapabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION,
                        simulatorManager.getSimulatorDetailsFromUDID(DeviceManager.getDeviceUDID())
                                .getOsVersion());
            } else {
                desiredCapabilities.setCapability("webkitDebugProxyPort",
                        new IOSDeviceConfiguration().startIOSWebKit());
            }

            if (Float.valueOf(version.substring(0, version.length() - 2)) >= 10.0) {
                desiredCapabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME,
                        AutomationName.IOS_XCUI_TEST);
                desiredCapabilities.setCapability(IOSMobileCapabilityType
                        .WDA_LOCAL_PORT, availablePorts.getPort());
            }
            desiredCapabilities.setCapability(MobileCapabilityType.UDID,
                    DeviceManager.getDeviceUDID());
        }
        desiredCapabilities.setCapability(MobileCapabilityType.UDID,
                DeviceManager.getDeviceUDID());
        desiredCapabilitiesThreadLocal.set(desiredCapabilities);
        return desiredCapabilities;
    } 
                            #location 12 
                            #vulnerability type NULL_DEREFERENCE
#fixed code
                                public DesiredCapabilities buildDesiredCapability(String platform,
                                                      String jsonPath) throws Exception {
        final boolean[] flag = {false};
        System.out.println("DeviceMappy-----" + DeviceAllocationManager.getInstance().deviceMapping);
        Object port = ((HashMap) DeviceAllocationManager.getInstance().deviceMapping.get(DeviceManager
                .getDeviceUDID())).get("port");
        DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
        JSONArray jsonParsedObject = new JsonParser(jsonPath).getJsonParsedObject();
        Object getPlatformObject = jsonParsedObject.stream().filter(o -> ((JSONObject) o)
                .get(platform) != null)
                .findFirst();
        Object platFormCapabilities = ((JSONObject) ((Optional) getPlatformObject)
                .get()).get(platform);
        ((JSONObject) platFormCapabilities)
                .forEach((caps, values) -> {
                    if ("browserName".equals(caps) && "chrome".equals(values.toString())) {
                        flag[0] = true;
                        try {
                            desiredCapabilities.setCapability("chromeDriverPort",
                                    availablePorts.getPort());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    if ("app".equals(caps)) {
                        if (values instanceof JSONObject) {
                            if (DeviceManager.getDeviceUDID().length()
                                    == IOSDeviceConfiguration.SIM_UDID_LENGTH) {
                                values = ((JSONObject) values).get("simulator");
                            } else if (DeviceManager.getDeviceUDID().length()
                                    == IOSDeviceConfiguration.IOS_UDID_LENGTH) {
                                values = ((JSONObject) values).get("device");
                            }

                        }
                        Path path = FileSystems.getDefault().getPath(values.toString());
                        if (!path.getParent().isAbsolute()) {
                            desiredCapabilities.setCapability(caps.toString(), path.normalize()
                                    .toAbsolutePath().toString());
                        } else {
                            desiredCapabilities.setCapability(caps.toString(), path
                                    .toString());
                        }
                    } else {
                        desiredCapabilities.setCapability(caps.toString(), values.toString());
                    }
                });
        if (DeviceManager.getMobilePlatform().equals(MobilePlatform.ANDROID) && !flag[0]) {
            if (desiredCapabilities.getCapability("automationName") == null
                    || desiredCapabilities.getCapability("automationName")
                    .toString() != "UIAutomator2") {
                desiredCapabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME,
                        AutomationName.ANDROID_UIAUTOMATOR2);
                desiredCapabilities.setCapability(AndroidMobileCapabilityType.SYSTEM_PORT,
                        Integer.parseInt(port.toString()));
            }
            appPackage(desiredCapabilities);
        } else if (DeviceManager.getMobilePlatform().equals(MobilePlatform.IOS)) {
            String version = iosDevice.getIOSDeviceProductVersion();
            appPackageBundle(desiredCapabilities);
            //Check if simulator.json exists and add the deviceName and OS
            if (DeviceManager.getDeviceUDID().length() == IOSDeviceConfiguration.SIM_UDID_LENGTH) {
                desiredCapabilities.setCapability(MobileCapabilityType.DEVICE_NAME,
                        simulatorManager.getSimulatorDetailsFromUDID(
                                DeviceManager.getDeviceUDID()).getName());
                desiredCapabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION,
                        simulatorManager.getSimulatorDetailsFromUDID(DeviceManager.getDeviceUDID())
                                .getOsVersion());
            } else {
                desiredCapabilities.setCapability("webkitDebugProxyPort",
                        new IOSDeviceConfiguration().startIOSWebKit());
            }

            if (Float.valueOf(version.substring(0, version.length() - 2)) >= 10.0) {
                desiredCapabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME,
                        AutomationName.IOS_XCUI_TEST);
                desiredCapabilities.setCapability(IOSMobileCapabilityType
                        .WDA_LOCAL_PORT, Integer.parseInt(port.toString()));
            }
            desiredCapabilities.setCapability(MobileCapabilityType.UDID,
                    DeviceManager.getDeviceUDID());
        }
        desiredCapabilities.setCapability(MobileCapabilityType.UDID,
                DeviceManager.getDeviceUDID());
        desiredCapabilitiesThreadLocal.set(desiredCapabilities);
        return desiredCapabilities;
    }