public class hiveBinaryTranslate extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) throws Exception { Task<BufferedImage> queryTask = new Task<BufferedImage>() { @Override protected BufferedImage call() throws Exception { String CONNECTION_URL = "jdbc:hive2://192.168.88.100:10000/default"; String USERNAME = "root"; String PASSWORD = "123456"; String JDBC_DRIVER = "org.apache.hive.jdbc.HiveDriver"; Class.forName(JDBC_DRIVER); Connection conn = DriverManager.getConnection(CONNECTION_URL, USERNAME, PASSWORD); Statement stmt = conn.createStatement(); String sql = "SELECT data FROM picture_01 WHERE dt = '2023-07-13'"; ResultSet rs = stmt.executeQuery(sql); rs.next(); InputStream inputStream = rs.getBinaryStream("data"); BufferedImage bufferedImage = ImageIO.read(inputStream); rs.close(); stmt.close(); conn.close(); inputStream.close(); return bufferedImage; } }; queryTask.setOnSucceeded(event -> { BufferedImage bufferedImage = queryTask.getValue(); Image image = SwingFXUtils.toFXImage(bufferedImage, null); ImageView imageView = new ImageView(image); Scene scene = new Scene(imageView.getParent()); primaryStage.setTitle("Image Viewer"); primaryStage.setScene(scene); primaryStage.show(); }); queryTask.setOnFailed(event -> { Throwable exception = queryTask.getException(); // 处理异常 exception.printStackTrace(); }); Thread queryThread = new Thread(queryTask); queryThread.start(); } }报错Exception in thread "JavaFX Application Thread" java.lang.NullPointerException at javafx.embed.swing.SwingFXUtils.toFXImage(SwingFXUtils.java:85) at hiveBinaryTranslate.lambda$start$0(hiveBinaryTranslate.java:49) 如何修改
时间: 2024-02-14 19:30:01 浏览: 117
根据报错信息,空指针异常发生在`SwingFXUtils.toFXImage`这一行,可能是因为`bufferedImage`为空。这可能是因为查询结果为空或者读取`inputStream`时发生了异常。
要解决这个问题,你可以在使用`rs.next()`之前先检查结果集中是否有数据,可以使用`rs.first()`方法来判断。同时,你也可以在`try-catch`块中处理可能的异常,以确保资源的正确关闭。
下面是修改后的代码示例:
```java
public class hiveBinaryTranslate extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Task<BufferedImage> queryTask = new Task<BufferedImage>() {
@Override
protected BufferedImage call() throws Exception {
String CONNECTION_URL = "jdbc:hive2://192.168.88.100:10000/default";
String USERNAME = "root";
String PASSWORD = "123456";
String JDBC_DRIVER = "org.apache.hive.jdbc.HiveDriver";
Class.forName(JDBC_DRIVER);
Connection conn = DriverManager.getConnection(CONNECTION_URL, USERNAME, PASSWORD);
Statement stmt = conn.createStatement();
String sql = "SELECT data FROM picture_01 WHERE dt = '2023-07-13'";
ResultSet rs = stmt.executeQuery(sql);
BufferedImage bufferedImage = null;
if (rs.first()) { // 检查结果集中是否有数据
InputStream inputStream = rs.getBinaryStream("data");
bufferedImage = ImageIO.read(inputStream);
inputStream.close();
}
rs.close();
stmt.close();
conn.close();
return bufferedImage;
}
};
queryTask.setOnSucceeded(event -> {
BufferedImage bufferedImage = queryTask.getValue();
if (bufferedImage != null) {
Image image = SwingFXUtils.toFXImage(bufferedImage, null);
ImageView imageView = new ImageView(image);
Scene scene = new Scene(imageView.getParent());
primaryStage.setTitle("Image Viewer");
primaryStage.setScene(scene);
primaryStage.show();
} else {
// 处理结果为空的情况
}
});
queryTask.setOnFailed(event -> {
Throwable exception = queryTask.getException();
// 处理异常
exception.printStackTrace();
});
Thread queryThread = new Thread(queryTask);
queryThread.start();
}
}
```
请注意,如果结果集为空,你需要根据业务需求来处理结果为空的情况。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)