简体   繁体   English

使用Fillo在Excel中更新单元格时,getiing错误为“ NullPointerException”

[英]When updating Cell in Excel using Fillo , getiing error as “NullPointerException”

I am using below code,i have checked pathname and sheet name is all fine. 我正在使用下面的代码,我已经检查了路径名和工作表名称都很好。 I am using fillo version fillo-1.15. 我正在使用fillo版本fillo-1.15。

public class T_Test {
    public static void main(String[] args) throws Exception{
        try {
            com.codoid.products.fillo.Fillo fil_res = new com.codoid.products.fillo.Fillo();
            com.codoid.products.fillo.Connection con_res = fil_res
                    .getConnection("C:\\Users\\admin\\workspace\\Good Connect\\Test_Data/Result\\LifePlanner_Result.xlsx");
            String sql_res = "update Result_Data SET Product_ID = 'Sampoorna Raksha' Where TestCase_ID = 'TC1_SIS' ";
            System.out.println(sql_res);
            com.codoid.products.fillo.Recordset recordset_rec = con_res.executeQuery(sql_res);
            con_res.close();
        } catch (Exception e) {
            System.out.println("Exception result set:" + e.toString());
        }
    }
}

Error is below: 错误如下:

java.lang.NullPointerException Exception result set:java.lang.NullPointerException at com.codoid.products.parser.SelectQueryParser.isWherePresent(SelectQueryParser.java:66) at com.codoid.products.fillo.Select.getRecordset(Select.java:47) at com.codoid.products.fillo.Connection.executeQuery(Connection.java:56) at GoodSolutionAgence.T_Test.main(T_Test.java:89) java.lang.NullPointerException异常结果集:com.codoid.products.parser.SelectQueryParser.isWherePresent(com.codoid.products.fills.Select.getRecordset(Select.java :)处的java.lang.NullPointerException 47)在com.codoid.products.fillo.Connection.executeQuery(Connection.java:56)在GoodSolutionAgence.T_Test.main(T_Test.java:89)

I believe the error is here: 我相信错误在这里:

       com.codoid.products.fillo.Recordset recordset_rec = con_res.executeQuery(sql_res);

You are executing an UPDATE statement , not a SELECT query . 您正在执行UPDATE 语句 ,而不是SELECT 查询 An UPDATE statement won't return a record set, just the number of rows it updated. UPDATE语句不会返回记录集,而只会返回其更新的行数。 You therefore want to use executeUpdate in place of executeQuery : 因此,您要使用executeUpdate代替executeQuery

       int numberOfRowsUpdated = con_res.executeUpdate(sql_res);

However, the Fillo library could at least have warned you that you were using the wrong method. 但是,Fillo库至少可以警告您使用了错误的方法。 The fact that it threw a NullPointerException is in my opinion a bug in this library: it should handle this error condition better. 在我看来,它引发了NullPointerException这个事实是该库中的一个错误:它应该更好地处理此错误情况。

Null Pointer Exception occurs when you try to reference a variable that has not been initialized 当您尝试引用尚未初始化的变量时,将发生空指针异常

 com.codoid.products.fillo.Connection con_res = fil_res
                    .getConnection("C:\\Users\\admin\\workspace\\Good Connect\\Test_Data/Result\\LifePlanner_Result.xlsx");

In this case the con_res might be null since getConnection method is not functioning properly. 在这种情况下,con_res可能为null,因为getConnection方法无法正常运行。

Now when you try to execute 现在,当您尝试执行

con_res.executeQuery(sql_res);

It results in Null Pointer Exception. 结果为空指针异常。

Check whether con_res gets initialized properly and check the path specified in getConnection method 检查con_res是否正确初始化,并检查getConnection方法中指定的路径

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM