0

I try to update the image after reading it from the database and throws

java.lang.NullPointerException
SQL Error
    at java.io.FileInputStream.<init>(FileInputStream.java:130)
    at tprison.Tprison.lambda$startgu$21(Tprison.java:947)

It will work if i choose an image using file chooser but what is this can't I change a person name or age something like that without changing the person image.

i understand the file is null but how, while it is having an image.

here is the code that will hold the image in the layout

browse = new JFXButton("browse");

    browse.setOnAction(e -> {

    fileg = filechooser.showOpenDialog(stageg);

    if(fileg != null){
        imageg = new Image(fileg.toURI().toString(),130,140,true,true);

         rectangleg = new Rectangle(130,140);
         rectangleg.setFill(new ImagePattern(imageg));

         rectangleg.setArcWidth(10);
         rectangleg.setArcHeight(10);

         bpgu.setTop(rectangleg);
         BorderPane.setAlignment(rectangleg, Pos.TOP_LEFT);
         bpgu.setMaxSize(130, 140);
         bpgu.setPrefSize(130, 140);
         bpgu.setVisible(true);

here is the code that retrieve the image from the database and display.

InputStream is = rsgf.getBinaryStream("Pic");
         OutputStream os = new FileOutputStream( new File("photo.jpg"));



            byte[] content = new byte[1024];
            int size = 0;
            while((size = is.read(content)) != -1){
                os.write(content,0,size);
            }
            os.close();
            is.close();

            imageg = new Image("file:photo.jpg", 130, 140, true, true);
            rectangleg = new Rectangle(130,140);
            rectangleg.setFill(new ImagePattern(imageg));

            rectangleg.setArcWidth(10);
            rectangleg.setArcHeight(10);

            bpgu.setTop(rectangleg);
            BorderPane.setAlignment(rectangleg, Pos.TOP_LEFT);
            bpgu.setMaxSize(130, 140);
            bpgu.setPrefSize(130, 140);
            bpgu.setVisible(true);

and finally here is the the code that update the image to the database and where the error points(fileg is null).

 pica = new FileInputStream(fileg);
 pstgu.setBinaryStream(10,(InputStream)pica, (int)fileg.length());
tom
  • 11
  • 6

1 Answers1

0

change this

imageg = new Image("file:photo.jpg", 130, 140, true, true);

to this get the job done

fileg = new File("photo.jpg");
imageg = new Image(fileg.toURI().toString(), 130, 140, true, true);
tom
  • 11
  • 6