ben
ben

Reputation: 480

javafx retrieve image from sql server database

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?

  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);
            }

Upvotes: 0

Views: 1543

Answers (1)

James_D
James_D

Reputation: 209603

You are writing the image data to a file with filename 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);
}

and then trying to read from a file with a different name:

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

So presumably you intended to do

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

Copying all the data to a file just to read it back in is incredibly inefficient. Do you really need the file? Why not just do

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

You also don't seem to do anything with this image view, so you need to add it to some pane somewhere.

Upvotes: 2

Related Questions