1

I'm getting a problem with getBinaryStream method when the blob columns has null values on db, here is my code:

public void getAderent(int id ) throws IOException{
 String rq_getAdrent ="SELECT * FROM gss_aderent WHERE ad_id= '"+id+"' ";
 Image img=null; 

        try {
            dbConnection db = new dbConnection();
            Connection connect = db.connectiondb();
            connect.setAutoCommit(false);
            Statement stmt= connect.createStatement();    

            ResultSet rs = stmt.executeQuery(rq_getAdrent);  
          int i=0;
            while(rs.next())
            {
                      addadh_id.setText(rs.getInt("ad_id")+"");
                      addadh_prenom.setText( rs.getString("ad_fname"));
                      addadh_nom.setText(rs.getString("ad_lname"));
                      if(rs.getString("ad_sexe").equals("f")) addadh_sexe.setSelected(true); 
                      addadh_ddn.setText(rs.getString("ad_ddn"));
                      addadh_adresse.setText(rs.getString("ad_adresse"));
                      addadh_mobile.setText(rs.getString("ad_mobile"));
                      addadh_email.setText(rs.getString("ad_email"));
                      addadh_sante.setValue(rs.getString("ad_sante"));
                      addadh_taille.setText(rs.getDouble("ad_taille")+"");
                      addadh_poids.setText(rs.getDouble("ad_poids_initial")+"");
                      addadh_urgence_contact.setText(rs.getString("ad_urgence_contact"));                  


                      if(rs.getBinaryStream("ad_photo") != null)  
                          img = new Image(rs.getBinaryStream("ad_photo")); 
                      else 
                          img = new Image("proson.png" );                
                      addadh_adherent_photo.setImage(img);
                      addadh_adherent_photo_label.setText("photo définie");      
            }


            rs.close();
            stmt.close();
            connect.commit();
            connect.close();

            // msg.showMsg(stage, "information", "Information", "Vous avez selectionné l'adérent numéro : "+id);
        } catch (SQLException ex) {
            Logger.getLogger(Cons_aderentController.class.getName()).log(Level.SEVERE, null, ex);
        }


  }    

and I am getting this exception:

        Executing D:\backup\NetBeansProjects\JavaFx Projects\GssPro\dist\run1860540670\GssPro.jar using platform C:\Program Files\Java\jdk1.8.0_31\jre/bin/java
avr. 14, 2015 2:13:48 PM javafx.scene.control.Control loadSkinClass
SEVERE: No valid constructor defined in 'com.sun.javafx.scene.control.skin.ComboBoxBaseSkin' for control ComboBox[id=addadh_sante, styleClass=combo-box-base combo-box].
You must provide a constructor that accepts a single Skinnable (e.g. Control or PopupControl) parameter in com.sun.javafx.scene.control.skin.ComboBoxBaseSkin.
avr. 14, 2015 2:13:48 PM javafx.scene.control.Control loadSkinClass
SEVERE: No valid constructor defined in 'com.sun.javafx.scene.control.skin.ComboBoxBaseSkin' for control ComboBox[id=addadh_sante, styleClass=combo-box-base combo-box].
You must provide a constructor that accepts a single Skinnable (e.g. Control or PopupControl) parameter in com.sun.javafx.scene.control.skin.ComboBoxBaseSkin.
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.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$ClickGenerator.postProcess(Scene.java:3437)
    at javafx.scene.Scene$ClickGenerator.access$7900(Scene.java:3365)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3733)
    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:1759)
    ... 30 more
Caused by: java.lang.NullPointerException
    at java.io.ByteArrayInputStream.<init>(ByteArrayInputStream.java:106)
    at org.sqlite.jdbc3.JDBC3ResultSet.getBinaryStream(JDBC3ResultSet.java:235)
    at org.sqlite.jdbc3.JDBC3ResultSet.getBinaryStream(JDBC3ResultSet.java:242)
    at controllers.Cons_aderentController.getAderent(Cons_aderentController.java:134)
    at controllers.Cons_aderentController.initData(Cons_aderentController.java:92)
    at controllers.Maj_aderentController.ConsultAderent(Maj_aderentController.java:177)
    ... 40 more

when the row on db has not a null value of the blob columns all is fine but the problem is when the row has null value. How can I fix this problem?

PS: Cons_aderentController.java:134 corresponding to :if(rs.getBinaryStream("ad_photo") != null)

Cœur
  • 37,241
  • 25
  • 195
  • 267
KacemSys
  • 111
  • 3
  • 13
  • @James_D , i think it is not duplicated, i know what is NullPointerException but my problem is with the method getBinaryStream implemetend in SQLlite java driver, despite i have tested the value getted from the method i'm always getting problem when the value is null ? – KacemSys Apr 14 '15 at 06:36
  • Can you edit the question then and show which line is giving the exception? – James_D Apr 14 '15 at 09:30
  • The problem is with line : `if(rs.getBinaryStream("ad_photo") != null)` – KacemSys Apr 14 '15 at 11:10
  • Are you sure? The only way that line can throw a `NullPointerException` is if `rs` is `null`. Can you edit your question with the full stack trace? – James_D Apr 14 '15 at 12:39
  • @James_D , no i'm sure that the rs is not null , i'm getting this problem only when the blob value is null otherwise all is fine. i think that there is a bug on the method getInputStream method ? how can is fix it ? – KacemSys Apr 14 '15 at 13:23
  • Is the line with the `if(rs.getBinaryStream(...) != null)` line 133 or 134? You say "133" in your text but the stack trace identifies 134. – James_D Apr 14 '15 at 13:26
  • it was just mistake , my problem is with this line `if(rs.getBinaryStream("ad_photo") != null)` i tried even `if(rs.getBytes("ad_photo").length > 0) i had the same error ` – KacemSys Apr 14 '15 at 13:47

1 Answers1

0

Assuming the column name is correct and that the column genuinely contains a SQL NULL value (which I think is the only possibility given your description), then it looks like your JDBC Driver is not properly implementing ResultSet in this case. (I'm not a sqlite user, but it looks like this may be a known bug.)

As an (ugly) workaround, try

try {
    img = new Image(rs.getBinaryStream("ad_photo"));
} catch (Exception e) {
    img = new Image("proson.png" ); 
}
James_D
  • 201,275
  • 16
  • 291
  • 322
  • thanks @James_D , the ugly workaround has resolved the problem :p i just replaced : `img = new Image("proson.png" );` by `img = new Image(getClass().getResourceAsStream("proson.png"));`. – KacemSys Apr 14 '15 at 14:14
  • Looking at the link for the bug report, you may find that if you get the latest version of the driver, the issue is resolved. That would be a better option than using `try`-`catch` to implement the logic. – James_D Apr 14 '15 at 14:16