title: javafx retrieve image from sql server database
body: <p>I am working on an e-commerce app using JavaFX, I am using SQL server 2014 and I was unable to retrieve image from database. I am using a for loop to add all the values of the database on a grid pane. The image was supposed to be at the center of the grid pane with the product name and price at the bottom. I get a java.lang.NullPointerException every time I tried it. could any one please help me?</p>
  
  <pre><code>  BorderPane background[]= new BorderPane[13];
  
        Label productName[]= new Label[13];
        Label priceLable[]= new Label[13];
        Image image;
        ImageView imageView;
  
      for (int i = 0; i&lt;=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);
              }
  </code></pre>