判断imageView的image是否为空

判断image是否为空

这里写代码
    UIImage *image = [UIImage imageNamed:@""];
    CGImageRef cgref = [image CGImage];
    CIImage *cim = [image CIImage];

    if (cim == nil && cgref == NULL)
    {
        NSLog(@"no image");
    } else {
        NSLog(@"imageView has a image");
    }
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) 如何修改
最新发布
07-17
根据报错信息,空指针异常发生在`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(); } } ``` 请注意,如果结果集为空,你需要根据业务需求来处理结果为空的情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值