Integer[] recruitId = contractListByBossInBO.getRecruitId();执行 recruitId.length != 0报错
时间: 2024-02-27 08:53:55 浏览: 12
如果 `recruitId` 是一个 `Integer` 类型的数组,那么判断其是否为空应该使用 `recruitId != null && recruitId.length != 0` 这个条件语句,这样可以避免 `NullPointerException` 异常。代码示例如下:
```
if (recruitId != null && recruitId.length != 0) {
// 数组不为空,执行相应的操作
} else {
// 数组为空,执行相应的操作
}
```
另外,如果你的代码中使用的是 `recruitId.length() != 0` 这个语句,也会报错,因为 `length()` 方法是用于字符串类型的,而不是用于数组类型的。应该使用 `length` 属性来获取数组的长度,即 `recruitId.length`。
相关问题
Integer int=Integer.parseInt(string)报错
您好!对于 Integer.parseInt(string) 报错的情况,可能有以下几个原因:
1. 字符串无法被解析为整数:如果字符串不是一个有效的整数表示,例如包含非数字字符或超出了整数的范围,会导致 parseInt 方法抛出 NumberFormatException 异常。您可以检查一下字符串的内容是否符合整数的格式,并确保在解析之前进行必要的验证。
2. 字符串为空或为 null:如果字符串为空或为 null,调用 parseInt 方法会导致 NullPointerException 异常。在使用 parseInt 方法之前,您需要确保字符串不为 null,并且包含有效的内容。
3. 其他异常情况:除了上述两种常见情况外,还可能存在其他导致 parseInt 报错的异常情况。如果您能提供具体的错误信息或相关代码,我将更详细地帮助您解决问题。
请您检查以上情况,并提供更多信息,我会尽力帮助您解决问题。
优化代码:Long userId = SecurityUtils.getUserId(); boolean flag = permissionService.hasRole("brand"); // admin if (SecurityUtils.isAdmin(userId)) { Integer i = gsApparatusExampleMapper.selectCapacityNum(); Integer i1 = gsApparatusExampleMapper.selectUseCapacity(); return i - i1; } if (flag) { // 品牌 Integer result = 0; Integer result1 = 0; Long brandId = UserUtils.getBrandId(userId); List<GsShop> gsShops = gsShopMapper.selectList(Wrappers.<GsShop>lambdaQuery() .eq(GsShop::getBrandId, brandId)); List<Long> shopIds = gsShops.stream().map(gsShop -> gsShop.getId()).collect(Collectors.toList()); for (Long shopId : shopIds) { Integer i = gsApparatusExampleMapper.selectCapacity(shopId); Integer i1 = gsApparatusExampleMapper.selectUserCapacity(shopId); if (i == null) { i = 0; } if (i1 == null) { i1 = 0; } result += i; result1 += i1; } return result - result1; } // 店铺 Long shopId = UserUtils.getShopId(userId); Integer i = gsApparatusExampleMapper.selectCapacity(shopId); Integer i1 = gsApparatusExampleMapper.selectUserCapacity(shopId); if (i == null) { i = 0; } if (i1 == null) { i1 = 0; } return i - i1;
可以对代码进行一些优化,主要是减少重复的数据库查询和代码块重复。
优化后的代码如下:
```java
Long userId = SecurityUtils.getUserId();
boolean flag = permissionService.hasRole("brand");
// admin
if (SecurityUtils.isAdmin(userId)) {
Integer capacity = gsApparatusExampleMapper.selectCapacityNum();
Integer userCapacity = gsApparatusExampleMapper.selectUserCapacityNum();
return capacity - userCapacity;
}
if (flag) {
// 品牌
Long brandId = UserUtils.getBrandId(userId);
List<GsShop> gsShops = gsShopMapper.selectList(Wrappers.<GsShop>lambdaQuery()
.eq(GsShop::getBrandId, brandId));
List<Long> shopIds = gsShops.stream().map(GsShop::getId).collect(Collectors.toList());
Integer result = 0;
Integer userResult = 0;
for (Long shopId : shopIds) {
Integer capacity = gsApparatusExampleMapper.selectCapacity(shopId);
Integer userCapacity = gsApparatusExampleMapper.selectUserCapacity(shopId);
result += (capacity != null ? capacity : 0);
userResult += (userCapacity != null ? userCapacity : 0);
}
return result - userResult;
}
// 店铺
Long shopId = UserUtils.getShopId(userId);
Integer capacity = gsApparatusExampleMapper.selectCapacity(shopId);
Integer userCapacity = gsApparatusExampleMapper.selectUserCapacity(shopId);
capacity = (capacity != null ? capacity : 0);
userCapacity = (userCapacity != null ? userCapacity : 0);
return capacity - userCapacity;
```
优化说明:
1. 将管理员权限判断提前,避免不必要的数据库查询。
2. 使用 `selectCapacityNum` 和 `selectUserCapacityNum` 方法一次性查询容量和使用容量的总和。
3. 统一处理容量和使用容量的 null 值情况,避免重复代码。
4. 将品牌用户和店铺用户的逻辑合并,避免代码块重复。
5. 使用两个变量 `result` 和 `userResult` 分别累加容量和使用容量,在最后返回它们的差值。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)