1 public class UserService { 2 private UserRepo userRepo; // 依赖注入 3 4 public User getUser(String telephone) { 5 // 如果用户不存在,则返回null 6 return null; 7 } 8 } 9 10 // 使用函数getUser() 11 User user = userService.getUser("18917718965"); 12 if (user != null) { // 做NULL值判断,否则有可能会报NPE 13 String email = user.getEmail(); 14 if (email != null) { // 做NULL值判断,否则有可能会报NPE 15 String escapedEmail = email.replaceAll("@", "#"); 16 } 17 }
标签:String,处理,会报,getUser,user,null,异常,email From: https://www.cnblogs.com/woaiheniunai/p/16842998.html