2017-01-22 121 views
0

我正在使用JavaFX的电子商务应用程序,我正在使用SQL Server 2014,我无法从数据库检索图像。我正在使用for循环在网格窗格中添加数据库的所有值。图像应该位于网格窗格的中心,产品名称和价格位于底部。每次我尝试它时,都会得到java.lang.NullPointerException。任何人都可以帮我吗?javafx从sql server数据库检索图像

BorderPane background[]= new BorderPane[13]; 

     Label productName[]= new Label[13]; 
     Label priceLable[]= new Label[13]; 
     Image image; 
     ImageView imageView; 

    for (int i = 0; i<=12;i++){ 
     background[i]=new BorderPane(); 
     background[i].setStyle("-fx-background-color: rgb(216, 216, 216)"); 
     background[i].setPrefHeight(300); 
     background[i].setPrefWidth(250); 

     try { 

       String query = "select pname,price,manufacturer,pimg from Adulis_product where pid=?"; 
       pst = con.prepareStatement(query); 
       pst.setInt(1, adp.getProduct_id()); 

       rs = pst.executeQuery(); 

       while (rs.next()) { 

        productName[i]=new Label(rs.getString("pname")+" - "+rs.getString("manufacturer")); 
        productName[i].setStyle("-fx-text-fill: #282828"); 
        productName[i].setFont(Font.font(" sans-serif", FontWeight.EXTRA_BOLD,14)); 

        priceLable[i]= new Label(rs.getString("price")); 
        priceLable[i].setStyle("-fx-text-fill: #1da288"); 
        priceLable[i].setFont(Font.font(" sans-serif", FontWeight.EXTRA_BOLD,15)); 
        int finalI2 = i; 

        InputStream is = rs.getBinaryStream("pimg"); 
        OutputStream os= new FileOutputStream(new File("pic.jpg")); 
        byte[] content= new byte[1024]; 
        int size=0; 
        while((size = is.read(content))!=-1){ 

         os.write(content, 0,size); 
        } 
        os.close(); 
        is.close(); 
        image = new Image("file:pic"+i+".jpg", 250,300,false,true); 

        imageView = new ImageView(image); 
        imageView.setFitHeight(300); 
        imageView.setFitWidth(250); 


        VBox prceNmanufactue= new VBox(10); 
        prceNmanufactue.getChildren().addAll(productName[i],priceLable[i]); 
        prceNmanufactue.setPadding(new Insets(0,0,0,5)); 

        Image newp= new Image(getClass().getResourceAsStream("New_30px.png")); 
        ImageView newimv= new ImageView(newp); 

        background[i].setTop(newimv); 

        VBox borderElements= new VBox(5); 
        borderElements.getChildren().addAll(prceNmanufactue,addtocart[i]); 
        background[i].setBottom(borderElements); 

       } 
      } catch (Exception e1) { 
       System.out.println(e1); 
      } 
+0

请编辑问题以包含堆栈跟踪,并指出代码中的哪一行引发异常。 –

+0

我已经修复了Java异常。将图像插入数据库后,错误消息消失。但仍然无法从数据库中检索图像。 – ben

+0

下面是我用来检索图像的代码 – ben

回答

0

您正在使用的文件名pic.jpg图像数据写入文件:

InputStream is = rs.getBinaryStream("pimg"); 
OutputStream os= new FileOutputStream(new File("pic.jpg")); 
byte[] content= new byte[1024]; 
int size=0; 
while((size = is.read(content))!=-1){ 

    os.write(content, 0,size); 
} 

,然后试图从一个文件中使用不同的名称改为:

image = new Image("file:pic"+i+".jpg", 250,300,false,true); 

那么想必你打算做

OutputStream os= new FileOutputStream(new File("pic"+i+".jpg")); 

将所有数据复制到文件中以便读取它是非常低效的。你真的需要这个文件吗?为什么不只是做

InputStream is = rs.getBinaryStream("pimg"); 
image = new Image(is, 250,300,false,true); 

也似乎并没有与此图像视图做任何事情,所以你需要它的地方添加一些窗格。

相关问题