即使值为null,如何使程序继续

时间:2015-09-19 14:08:09

标签: java mysql image null imageicon

我在java和MySQL中有一个程序用于数据库。但问题是:当SQL中的IMAGE数据为空时,程序停止加载图片。 它显示" java.lang.IllegalArgumentException:input == null!"。我的问题是如何将Icon图片设置为null并继续。

  try{
        PreparedStatement ps = conn.prepareStatement(President);
        ResultSet rs = ps.executeQuery();
        if(rs.next()){
        rs.absolute(1);
        FirstName1.setText(rs.getString("cFirstname"));
        LastName1.setText(rs.getString("CLastname"));
        MiddleName1.setText(rs.getString("cmiddlename"));
        BufferedImage im1 = ImageIO.read(rs.getBinaryStream("cimage"));
        ImageIcon image1 = new ImageIcon(im1);
        Image img1 = image1.getImage();
        Image newImage = img1.getScaledInstance(144, 144,     Image.SCALE_SMOOTH);
        ImageIcon after = new ImageIcon(newImage);
        btnPic1.setIcon(after);
        btnPic1.setText("");
        FirstName1.setVisible(true);
        LastName1.setVisible(true);
        btnPic1.setVisible(true);
        MiddleName1.setVisible(true);
        }

在我的数据中,此处存储的图像为空。

if(rs.next()){
        rs.absolute(2);
        FirstName2.setText(rs.getString("cFirstname"));
        LastName2.setText(rs.getString("CLastname"));
        MiddleName2.setText(rs.getString("cmiddlename"));
        BufferedImage im2 = ImageIO.read(rs.getBinaryStream("cimage"));
        ImageIcon image2 = new ImageIcon(im2);
        Image img2 = image2.getImage();
        Image newImage2 = img2.getScaledInstance(144, 144, Image.SCALE_SMOOTH);
        ImageIcon after2 = new ImageIcon(newImage2);
        btnPic2.setIcon(after2);
        btnPic2.setText("");
        FirstName2.setVisible(true);
        LastName2.setVisible(true);
        btnPic2.setVisible(true);
        MiddleName2.setVisible(true);
        }

然后我的程序就不会这样做了。

if(rs.next()){
        rs.absolute(3);
        FirstName3.setText(rs.getString("cFirstname"));
        LastName3.setText(rs.getString("CLastname"));
        MiddleName3.setText(rs.getString("cmiddlename"));
        BufferedImage im3 = ImageIO.read(rs.getBinaryStream("cimage"));
        ImageIcon image3 = new ImageIcon(im3);
        Image img3 = image3.getImage();
        Image newImage3 = img3.getScaledInstance(144, 144, Image.SCALE_SMOOTH);
        ImageIcon after3 = new ImageIcon(newImage3);
        btnPic3.setIcon(after3);
        btnPic3.setText("");
        FirstName3.setVisible(true);
        LastName3.setVisible(true);
        btnPic3.setVisible(true);
        MiddleName3.setVisible(true);
        }

2 个答案:

答案 0 :(得分:0)

哪一行抛出IllegalArgumentException?也许我过于简单化,但一个简单的解决方案可能是你在此行之前检查一个空图像变量,如果该变量为null,则不要使用它,并跳过该行。一个更好的解决方案是找出图像为空的原因,并首先防止这种情况发生。

答案 1 :(得分:0)

结果代码将是这样的

if(rs.next()){
        rs.absolute(2);
        FirstName2.setText(rs.getString("cFirstname"));
        LastName2.setText(rs.getString("CLastname"));
        MiddleName2.setText(rs.getString("cmiddlename"));
            if(rs.getBinaryStream("cimage")!=null){
            BufferedImage im2 = ImageIO.read(rs.getBinaryStream("cimage"));
            ImageIcon image2 = new ImageIcon(im2);
            Image img2 = image2.getImage();
            Image newImage2 = img2.getScaledInstance(144, 144, Image.SCALE_SMOOTH);
            ImageIcon after2 = new ImageIcon(newImage2);
        btnPic2.setIcon(after2);
        btnPic2.setText("");}
            else{
            btnPic2.setText("No Picture");     
            }
        FirstName2.setVisible(true);
        LastName2.setVisible(true);
        btnPic2.setVisible(true);
        MiddleName2.setVisible(true);
        }

添加“if null”语句。虽然在这个用途。我扭转了它。感谢您的评论和解答。我已经能够提出这个解决方案。