【问题标题】:Skipping null values using a for loop使用 for 循环跳过空值
【发布时间】:2022-02-03 16:00:26
【问题描述】:

DB_Example 我正在探索数据库的自动化测试,并努力在我的表中命中空值。

我要测试:

  1. 如果 EID1 =

  2. 如果 EID2 =

有空值,我收到以下错误:无法调用“String.startsWith(String)”,因为 java.util.Map.get(object) 的返回值为空。

我的第一个值始终为空,因此我从计数器 1 开始循环,这解决了该问题,但我的表中的 EID 和 CID 中都有空值。如果不满足条件(即值为空)测试失败,这很好,但我的应用程序在达到任何空值时崩溃。我可以更改哪些内容以避免崩溃和过滤空值?我无法解决它。尝试添加 nullpointerexception 和一些摆弄循环但没有运气。我已经包含了表格的示例图像以供参考。

Map<String, String> result = new HashMap<>();
Map<String, String> result = new HashMap<String, String>();
query = "select * FROM DEA.TUSET where T_DATE >'2010-11-02'";
resultSet = statement.executeQuery(Query)
ResultSetMetaData rsmd = resultSet.getMetaData();
long pId = 1:
while (resultSet.next()) {
for int col = 1; col <= rsmd.getColumnCount(); col ++) {
result.put(rsmd.getColumnLabel(col)+pId, resultSet.getString(col)); }
pId ++;
}
//print all results in col
for (int i=0; i<pId;i++)
System.out.println("EID: "+result.get("EID"+i));

//problem
for (int i=1; i<pId;i++)
if (result.get("EID"+i).startsWith("8"){
if result.get("CID"+i).startsWith("M") {
System.out.println ("true");
}
}

【问题讨论】:

    标签: java jdbc


    【解决方案1】:

    在检查startsWith()之前,检查该值是否为空。

    if(result.get("EID"+i)!=null && result.get("CID"+i)!=null ){
        //your code to execute
    }
    else {
        // some code to execute if the value is null
    }
    

    【讨论】:

    • 谢谢你,这一切都混在一起了。欣赏!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多