Java中equals()方法判断字符串相等时报NullPointerException,如s.equals("String")
作者:互联网
1、题目
阅读getCondition方法,找出其中的错误点并修复
- 要求不能使用第三方包,只能用JDK中的方法
- 要求对修复之后的方法进行测试,并打印结果
2、代码片段
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
public class SQLConstructor {
public static void main(String[] args) {
System.out.println("阅读getCondition方法,找出其中的错误点并修复");
System.out.println("要求不能使用第三方包,只能用JDK中的方法");
System.out.println("要求对修复之后的方法进行测试,并打印结果");
}
/**
* 构造查询SQL语句
* @param searchParams
* @param values
* @return 返回SQL
*/
private static String getCondition(Map<String, Object> searchParams, List<Object> values) {
StringBuilder sb = new StringBuilder();
for (String s : searchParams.keySet()) {
if ("startTime".equals(s)) {
if (String.valueOf(searchParams.get("startTime")).contains(":")) {
throw new RuntimeException("请使用时间进行查询!");
} else {
sb.append("UPDATE_TIME >= ? AND ");
values.add(paseDateFromLongStr(searchParams.get(s).toString()));
}
} else if (s.equals("DIC_TYPE")) {
sb.append(" DIC_TYPE IN (?,?,?) AND ");
String[] split = String.valueOf(searchParams.get(s)).split(",");
if (split.length == 3) {
values.addAll(Arrays.asList(split));
} else if (split.length < 3) {
// TODO 处理非3的情况
}
} else if ("ITEM_ID".equals(s)) {
sb.append("ITEM_ID =? AND ");
} else if ("ORG_ID".equals(s) || "COMPANY_ID".equals(s)) {
sb.append(s + " =? AND ");
values.add(searchParams.get(s));
}
}
if (!searchParams.keySet().contains("DIC_CODE")) {
sb.append(" DIC_CODE IN ('JOB_DUTY','GROUP_JOB_DUTY','USER_POST') AND ");
}
return sb.toString();
}
public static Date paseDateFromLongStr(String dateStr) {
Date date = null;
try {
Long timestamp = Long.parseLong(dateStr);
date = new Timestamp(timestamp);
} catch (Exception e) {
throw new RuntimeException(
"请检查日期格式(需使用时间戳格式的时间)");
}
return date;
}
}
3、代码解答
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SQLConstructor {
public static void main(String[] args) {
System.out.println("阅读getCondition方法,找出其中的错误点并修复");
System.out.println("要求不能使用第三方包,只能用JDK中的方法");
System.out.println("要求对修复之后的方法进行测试,并打印结果");
Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("startTime", "1648349346");
List<Object> list1=new ArrayList<Object>();
String str1 =SQLConstructor.getCondition(map1, list1);
System.out.println(str1); //UPDATE_TIME >= ? AND DIC_CODE IN ('JOB_DUTY','GROUP_JOB_DUTY','USER_POST') AND
Map<String, Object> map2 = new HashMap<String, Object>();
map1.put("startTime", "2022-03-27 10:49:06");
List<Object> list2=new ArrayList<Object>();
String str2 =SQLConstructor.getCondition(map2, list2);
System.out.println(str2); // DIC_CODE IN ('JOB_DUTY','GROUP_JOB_DUTY','USER_POST') AND
System.out.println("========================================");
Map<String, Object> map3 = new HashMap<String, Object>();
//方便测试全放到map3中
map3.put("DIC_TYPE", "value2");
map3.put("ITEM_ID", "value3");
map3.put("ORG_ID", "value4");
map3.put("COMPANY_ID", "value5");
List<Object> list3=new ArrayList<Object>();
String str3 =SQLConstructor.getCondition(map3, list3);
System.out.println(str3); // ORG_ID =? AND DIC_TYPE IN (?,?,?) AND ITEM_ID =? AND COMPANY_ID =? AND DIC_CODE IN ('JOB_DUTY','GROUP_JOB_DUTY','USER_POST') AND
//map.put(null, "value"); //s.equals("DIC_TYPE" 报错 NullPointerException
}
/**
* 构造查询SQL语句
* @param searchParams
* @param values
* @return 返回SQL
*/
private static String getCondition(Map<String, Object> searchParams, List<Object> values) {
StringBuilder sb = new StringBuilder();
for (String s : searchParams.keySet()) {
if ("startTime".equals(s)) {
if (String.valueOf(searchParams.get("startTime")).contains(":")) {
throw new RuntimeException("请使用时间进行查询!");
} else {
sb.append("UPDATE_TIME >= ? AND ");
values.add(paseDateFromLongStr(searchParams.get(s).toString()));
}
// } else if (s.equals("DIC_TYPE")) { 修改前
} else if ("DIC_TYPE".equals(s)) {
sb.append(" DIC_TYPE IN (?,?,?) AND ");
String[] split = String.valueOf(searchParams.get(s)).split(",");
if (split.length == 3) {
values.addAll(Arrays.asList(split));
} else if (split.length < 3) {
// TODO 处理非3的情况
}
} else if ("ITEM_ID".equals(s)) {
sb.append("ITEM_ID =? AND ");
} else if ("ORG_ID".equals(s) || "COMPANY_ID".equals(s)) {
sb.append(s + " =? AND ");
values.add(searchParams.get(s));
}
}
if (!searchParams.keySet().contains("DIC_CODE")) {
sb.append(" DIC_CODE IN ('JOB_DUTY','GROUP_JOB_DUTY','USER_POST') AND ");
}
return sb.toString();
}
public static Date paseDateFromLongStr(String dateStr) {
//1648349346 2022-03-27 10:49:06
Date date = null;
try {
Long timestamp = Long.parseLong(dateStr);
date = new Timestamp(timestamp);
} catch (Exception e) {
throw new RuntimeException(
"请检查日期格式(需使用时间戳格式的时间)");
}
return date;
}
}
由于个人能力有限,如有不足之处,欢迎指正。
标签:searchParams,Java,String,equals,DIC,new,ID,NullPointerException 来源: https://www.cnblogs.com/CodedogWorld/p/16067105.html