- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
虽然我不得不说,我对 Java 还是个新手,但我已经开始掌握它了,而且它非常酷。无论如何,在我的 Netbeans IDE 中,我收到警告 Dereferencing possible null pointer
。请在此处查看代码快照。您可以看到强调空指针警告的黄线。
这里是纯文本
/*
* Establish Connection to MySQL
*/
Connection conn;
conn = MySQLConnectionClass.getCurrentConnection();
if (conn == null){
System.out.println("MySQL not connected!");
}
/*
* get the change in price-nav/price
*/
double nav_change;
float first_close = 0;
float first_nav_close = 0;
float last_nav_close = 0;
String sym = "";
try{
try (PreparedStatement get_p = conn.prepareStatement("SELECT close, symbol FROM test.cef_daily_data where symbol = (select symbol from cef_nav_real_time_trading_watch_list where id = ?) order by date desc limit 6,1;")) {
get_p.setInt(1,id);
ResultSet price = get_p.executeQuery();
try{
price.next();
first_close = price.getFloat(1);
sym = price.getString(2);
price.close();
}catch (SQLException e){
}
}
这是显示警告的图像。
我找到了一个关于取消引用空指针的很好的初学者解释 here .
我想也许 PreparedStatement
变量:get_p
有可能是 null
,所以我试着在后面加一行来检查确定 get_p
不是 null
,但这并没有使警告消失。
所以我不只是试图让这个警告消失,我还试图了解问题所在以提高我的 Java 技能。感谢您的帮助!
更新
问题是 conn
变量有可能为 null
。我像这样添加了一个 return
行来解决问题:
/*
* Establish Connection to MySQL
*/
Connection conn;
conn = MySQLConnectionClass.getCurrentConnection();
if (conn == null){
System.out.println("MySQL not connected!");
return;
}
最佳答案
if (conn == null){
System.out.println("MySQL not connected!");
}
...将只打印一条消息并继续引用后续行中的空指针(prepareStatement 调用是它的以下用法)。如果没有连接,您可能想要添加一个 return
或绕过实际查询的某种错误处理。
关于java - 使用 mysql PrepareStatement 取消引用可能的空指针警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16913917/
我正在尝试使用以下命令执行准备好的语句: dbaBean.setPrepStmt(dbaBean.getConn().prepareStatement( "SELECT id, author,
当 id 是我的 SQL 表的键时,我使用 prepareStatement() 它将由 SQL 创建,我想使用这个语句 :(我应该写什么而不是 SQL 表第一列中的 X(提醒:SQL 自动创建) F
我在这个函数中遇到错误,但我不知道为什么。你能帮帮我吗? 错误在我调用 executeQuery() 的行中 public static int numeroElementi(String table
我正在尝试使用 prepareStatement 函数。代码如下。执行后,它会返回一堆 vlicense 字符串而不是值。 当代码完成 statement.setString() 时,语句变为: se
我正在尝试使用 prepareStatement 将数据插入 mysql 数据库,但它显示错误,如 Type mismatch: cannot convert from java.sql.Prepar
整个数据操作如下。我想关闭每个资源而不干扰下一个连接。我应该将构造函数更改为connection()方法,然后使用disconnect()方法,但是这样做之后我应该在哪里 public class D
在像这样的 prepareStatement 中运行此查询时 SELECT SYSDATE + INTERVAL '7' DAY FROM DUAL; PreparedStatement ps
我尝试使用 prepareStatement 查询 sqlite,但遇到异常: java.sql.SQLException: not supported by PreparedStatment
我在某处看到了这个例子: rs = connection.prepareStatement("select * from table").executeQuery(); 如果我想执行这样的查询“ S
我注意到有很多关于CreateStatement 与PreparedStatement 的帖子,但是我的问题不同(只是为了通知这不是这些搜索结果之一的重复) 当我从 java 代码查询数据库时,我可以
我是 eclipse 的新手,我正在尝试创建一个注册界面。作为一个外壳,它工作得很好,即如果没有给出输入,它会接受输入并显示警告,但它没有连接到数据库。我现在尝试使用prepareStatement
我已阅读prepareStatement(String sql)的描述还有很多与PreparedStatement的性能和缓存相关的帖子,我很清楚数据库将解析和编译准备好的语句的查询,这样对于后续相同
当我尝试使用准备好的语句更新 SQL 数据库时,我不知道为什么我的代码在 executeUpdate(sql) 时出错。当我尝试在 Oracle SQL 终端中运行 SQL 查询时,它工作得很好。谁能
PrepareStatement 是否只能与值一起使用?示例:我可以使用类似 con.prepareStatement(SELECT ? FROM ? WHERE name =?)或者 ? 仅用于值,
我的数据库添加功能出现问题。下面的代码卡在 ExecuteUpdate 语句上,它没有抛出异常,所以我不知道哪里出了问题。 public static boolean addUser(User use
在运行时循环两次,在通过请求查询调用 PrepareStatement 时因 Bad_Access 而崩溃。 所以我检查了所有的 ResultSet 内存并释放了它但是没有想法有什么办法可以解决这个问
虽然我不得不说,我对 Java 还是个新手,但我已经开始掌握它了,而且它非常酷。无论如何,在我的 Netbeans IDE 中,我收到警告 Dereferencing possible null po
我有以下代码在 MySQL 表中插入行: String sqlInsert = "INSERT INTO test_perf_table ( id, name) VALUES ( ?, ?);"; P
我正在编写一个需要与 HSQLDB 连接的 Jbatch 服务,但在某个时候它会抛出 NullPointerException (and some SQLExceptions/PersistenceE
这个问题在这里已经有了答案: JDBC prepareStatement doesn't work (4 个答案) 关闭 7 年前。 所以我试图创建一个未指定的表名和未指定的列,但是,我是 prep
我是一名优秀的程序员,十分优秀!