title: javafx receive image from mysql set to Image view
body: <p>I have image in my mysql database and i want to receive image i can do this in java but can't in javafx .</p>
  
  <p>I am using this code to receive image from database. </p>
  
  <pre><code> byte[] f51 = rs.getBytes(11);
  </code></pre>
  
  <p>now i want to set it in <code>Image view</code> .</p>
  
  <p>I can do this in <strong>Java</strong> like <code>img.setIcon(new ImageIcon(f51));</code> but this not working in <strong>javafx</strong></p>
  
  <p>I used the code </p>
  
  <pre><code>private static Image convertToJavaFXImage(byte[] raw, final int width, final int height) {
      WritableImage image = new WritableImage(width, height);
      try {
          ByteArrayInputStream bis = new ByteArrayInputStream(raw);
          BufferedImage read = ImageIO.read(bis);
          SwingFXUtils.toFXImage(read, image);
      } catch (IOException ex) {
          Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
      }
      return image;
  }
  </code></pre>
  
  <p>Provided by @Patrick</p>
  
  <p>now it gives error:</p>
  
  <pre><code>Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
      at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1762)
      at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1645)
      at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
      at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
      at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
      at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
      at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
      at javafx.event.Event.fireEvent(Event.java:198)
      at javafx.scene.Scene$MouseHandler.process(Scene.java:3724)
      at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3452)
      at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1728)
      at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2461)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:348)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:273)
      at java.security.AccessController.doPrivileged(Native Method)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:382)
      at com.sun.glass.ui.View.handleMouseEvent(View.java:553)
      at com.sun.glass.ui.View.notifyMouse(View.java:925)
      at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
      at com.sun.glass.ui.win.WinApplication.lambda$null$141(WinApplication.java:102)
      at com.sun.glass.ui.win.WinApplication$$Lambda$37/584634336.run(Unknown Source)
      at java.lang.Thread.run(Thread.java:745)
  Caused by: java.lang.reflect.InvocationTargetException
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:483)
      at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
      at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:483)
      at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
      at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1757)
      ... 30 more
  Caused by: java.lang.NullPointerException
      at javafx.embed.swing.SwingFXUtils.toFXImage(SwingFXUtils.java:85)
      at stockmanagement1.RawmaterialAddController.convertToJavaFXImage(RawmaterialAddController.java:340)
      at stockmanagement1.RawmaterialAddController.tabmrel(RawmaterialAddController.java:318)
      ... 40 more
  </code></pre>
  
  <p>Help Me please.</p>
  
  <p><strong>Updated</strong></p>
  
  <p>wait i think there is a confusing i have posted all codes:</p>
  
  <p>adding :</p>
  
  <pre><code>     @FXML
          private void add(ActionEvent event) {
  
                        String mcode=txt1.getText();
                     String mname=txt2.getText();
                     String material=txt3.getText();
                     String quantity=txt4.getText();
                     String unit=(String) com1.getSelectionModel().getSelectedItem();
                     String priceper=txt5.getText();
                     String godown=(String) com2.getSelectionModel().getSelectedItem();
  
                     String desc=txtdexc.getText();
  
                  byte[] buf = null; byte[] person_image5=null;
             try{
          String df = img.getText();
      File image=new File (df);
      FileInputStream fis=new FileInputStream(image);
      ByteArrayOutputStream bos= new ByteArrayOutputStream();
       buf=new byte[1024];
      for(int readnum;(readnum=fis.read(buf))!=-1; ){
      bos.write(buf,0,readnum);
  }
   person_image5 = bos.toByteArray();
  }
     catch(Exception e){ }   
         try{
              Connection con1=(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/stockmanagement","root","root");
              String sql1="INSERT INTO `rawmaterial`(`Name`, `Code`, `DOC`, `Material`, `godown`, `quantity`, `unit`, `cost`, `detail`, `pic`, `user`, `lastupdated`)"
                      + "                 VALUES    ('"+mname+"','"+mcode+"','2012-12-12','"+material+"','"+godown+"','"+quantity+"','"+unit+"','"+priceper+"','"+desc+"','"+person_image5+"','','')";
              PreparedStatement stm1=(PreparedStatement) con1.prepareStatement(sql1);
              stm1.executeUpdate(sql1);   
              tabdata();
                   } 
  
                   catch(Exception e){
                       System.out.println(e.getMessage());
   }
  
      }
  </code></pre>
  
  <p>and now codes to show image.</p>
  
  <pre><code>        @FXML
      void tabmrel(MouseEvent event) throws IOException {
      int x0=table.getSelectionModel().getSelectedIndex();
        String id=table.getItems().get(x0).getFirstName5();
                try{
      Class.forName("com.mysql.jdbc.Driver");
          Connection con=(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/stockmanagement","root","root");
          String sql="Select * from rawmaterial where code='"+id+"'";
         Statement stm=(Statement) con.createStatement();
        ResultSet rs=stm.executeQuery(sql);
  
         if(rs.next()){
      txt1.setText(rs.getString(3));
       txt2.setText(rs.getString(2));
     txt3.setText(rs.getString(5));
      txt4.setText(rs.getString(7));
       byte[]  f51=rs.getBytes(11); 
            System.out.println(f51);
  
              ByteArrayInputStream bis = new ByteArrayInputStream(f51);
              System.out.println(bis);
              BufferedImage read = ImageIO.read(bis);
              System.out.println(read);
              Image image = SwingFXUtils.toFXImage(read, null);
              imagego.setImage(image);
  
        com1.getSelectionModel().select(rs.getString(8));
                txt5.setText(rs.getString(9));
             com1.getSelectionModel().select(rs.getString(6));
         txtdexc.setText(rs.getString(10));
  
       }
       } 
      catch (ClassNotFoundException | SQLException e) {
  
      }
    }
  </code></pre>
  
  <p>help me.</p>