2017-12-14 15 views
1

あなたが私を助けてくれることを願っています。私のデータベースから取り出されたイメージを丸めてみましょう。次のイメージではイメージがimageview.userに正しく表示されています。正しいイメージを表示するためにイメージが変更されました。これは問題なく動作しています。Java FXサークルイメージ

This is the program

私はgestionarEventosでこのコードを試してみてください。

imgfotovisi.imageProperty().bind(imageRetrievalService.valueProperty()); 
     Image im = imgfotovisi.getImage(); 
     circulo.setFill(new ImagePattern(im)); 

が、Javaは言う:

... 58 more 
Caused by: java.lang.NullPointerException: Image must be non-null. 
    at javafx.scene.paint.ImagePattern.<init>(ImagePattern.java:235) 

私は行を削除した場合、プログラムが実行されますが(imgfotovisi.imagePropertyを怒鳴ります).bind(imageRetrievalService.valueProperty());ライン。

実行時に、なぜ画像がヌルであるのかわかりません。そこにはっきりと見ることができます。

これはクラスver_visitantesです:

import java.net.URL; 
    import java.sql.Connection; 
    import java.sql.PreparedStatement; 
    import java.util.ResourceBundle; 
    import java.util.function.Predicate; 
    import javafx.beans.property.ReadOnlyStringWrapper; 
    import javafx.beans.value.ChangeListener; 
    import javafx.beans.value.ObservableValue; 
    import javafx.collections.FXCollections; 
    import javafx.collections.ObservableList; 
    import javafx.collections.transformation.FilteredList; 
    import javafx.collections.transformation.SortedList; 
    import javafx.concurrent.Service; 
    import javafx.concurrent.Task; 
    import javafx.event.ActionEvent; 
    import javafx.fxml.FXML; 
    import javafx.fxml.Initializable; 
    import javafx.scene.Node; 
    import javafx.scene.control.Button; 
    import javafx.scene.control.Label; 
    import javafx.scene.control.TableColumn; 
    import javafx.scene.control.TableView; 
    import javafx.scene.control.TextField; 
    import javafx.scene.image.Image; 
    import javafx.scene.image.ImageView; 
    import javafx.scene.input.InputEvent; 
    import javafx.scene.layout.StackPane; 
    import javafx.scene.paint.Color; 
    import javafx.scene.paint.ImagePattern; 
    import javafx.scene.shape.Circle; 

    import javafx.stage.Stage; 

    public class ver_visitantes implements Initializable { 

     @FXML private TableView<visitantes> tbvisitantes; 
     @FXML private TableColumn<visitantes, String> clcedula,clnombres,clapellidos,clapartamento,clcelular,clobservaciones; 
     @FXML private ImageView imgfotovisiact,imgfotoact,imgfotovisi,imgfoto; 
     @FXML private TextField txtcedula,txtnombres,txtapto,txtapellidos,txtapt,txtcelular,txtobservaciones; 
     @FXML private Label lblinfovisiact,lblusuario,lblstatusvisi; 
     @FXML private Circle circulo; 

     private ObservableList<visitantes> visitorlist; 

     @Override 
     public void initialize(URL arg0, ResourceBundle arg1) { 


      ConexionSQL cnt = new ConexionSQL(); 
      cnt.conexion(); 

      visitorlist = FXCollections.observableArrayList(); 
      visitantes.llenarlistavisitas(cnt.conexion(), visitorlist); 
      tbvisitantes.setItems(visitorlist);// llenar table view con la lista 

      clcedula.setCellValueFactory(cellData -> new ReadOnlyStringWrapper(cellData.getValue().getcedula())); 
      clnombres.setCellValueFactory(cellData -> new ReadOnlyStringWrapper(cellData.getValue().getnombres())); 
      clapellidos.setCellValueFactory(cellData -> new ReadOnlyStringWrapper(cellData.getValue().getapellidos())); 
      clapartamento.setCellValueFactory(cellData -> new ReadOnlyStringWrapper(cellData.getValue().getapartamento())); 
      clcelular.setCellValueFactory(cellData -> new ReadOnlyStringWrapper(cellData.getValue().getcelular())); 
      clobservaciones.setCellValueFactory(cellData -> new ReadOnlyStringWrapper(cellData.getValue().getobservaciones())); 

      gestionarEventos(); 

      tbvisitantes.getSelectionModel().selectFirst(); 


     } 

     public void gestionarEventos() { 
      tbvisitantes.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<visitantes>() { 
       @Override 
       public void changed(ObservableValue<? extends visitantes> arg0, visitantes valorAnterior, 
         visitantes valorSeleccionado) { 
        imgfoto.setVisible(false); 
        btnmodificar.setDisable(false); 
        btncancelar.setDisable(false); 
        btneliminar.setDisable(false); 
        imageRetrievalService.restart(); 

        if (valorSeleccionado != null) { 

         txtcedula.setText(String.valueOf(valorSeleccionado.getcedula())); 
         txtnombres.setText(valorSeleccionado.getnombres()); 
         txtapellidos.setText(valorSeleccionado.getapellidos()); 
         txtapto.setText(String.valueOf(valorSeleccionado.getapartamento())); 
         txtcelular.setText(String.valueOf(valorSeleccionado.getcelular())); 
         txtobservaciones.setText(String.valueOf(valorSeleccionado.getobservaciones())); 
        } 
       } 
      }); 

     imgfotovisi.imageProperty().bind(imageRetrievalService.valueProperty()); 

     } 


     private final Service<Image> imageRetrievalService = new Service<Image>() {// cargar imagen en visitantes 
      @Override 
      protected Task<Image> createTask() { 
       final String id; 

       final visitantes visitante = tbvisitantes.getSelectionModel().getSelectedItem(); 
       if (visitante == null) { 
        id = null; 
       } else { 
        id = visitante.getcedula(); 
       } 
       return new Task<Image>() { 
        @Override 
        protected Image call() throws Exception { 
         if (id == null) { 
          return null; 
         } 
         return visitante.getImageById(id); 
        } 
       }; 
      } 
     }; 


    } 

が、これは画像を取得するためにimageRetrievalServiceから呼び出され、visitantesクラスです:

package application; 

import java.io.IOException; 
import java.io.InputStream; 
import java.sql.Blob; 
import java.sql.Connection; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 



import javafx.beans.property.SimpleStringProperty; 
import javafx.beans.property.StringProperty; 
import javafx.collections.ObservableList; 
import javafx.scene.image.Image; 


public class visitantes { 
    private StringProperty cedula; 
    private StringProperty nombres; 
    private StringProperty apellidos; 
    private StringProperty apartamento; 
    private StringProperty celular; 
    private StringProperty observaciones; 



    public visitantes(String cedula,String nombres,String apellidos,String apartamento,String celular,String observaciones){ 

     this.cedula = new SimpleStringProperty(cedula); 
     this.nombres = new SimpleStringProperty(nombres); 
     this.apellidos = new SimpleStringProperty(apellidos); 
     this.apartamento = new SimpleStringProperty(apartamento); 
     this.celular = new SimpleStringProperty(celular); 
     this.observaciones = new SimpleStringProperty(observaciones); 



    } 




    public String getnombres(){ 
     return nombres.get(); 
    } 

    public void setnombres(String nombres){ 
     this.nombres = new SimpleStringProperty(nombres); 
    } 

    public String getcedula(){ 
     return cedula.get(); 
    } 

    public void setcedula(String cedula){ 
     this.cedula = new SimpleStringProperty(cedula); 
    } 
    public String getapellidos(){ 
     return apellidos.get(); 
    } 

    public void setapellidos(String apellidos){ 
     this.apellidos = new SimpleStringProperty(apellidos); 
    } 

    public String getapartamento(){ 
     return apartamento.get(); 
    } 

    public void setapartamento(String apartamento){ 
     this.apartamento = new SimpleStringProperty(apartamento); 
    } 

    public String getcelular(){ 
     return celular.get(); 
    } 

    public void setcelular(String celular){ 
     this.celular = new SimpleStringProperty(celular); 
    } 




      public Image getImageById(String id) throws SQLException, IOException { 
       try (
         ConexionSQL cn = new ConexionSQL(); 
         Connection con = cn.conexion(); 
        PreparedStatement ps = con.prepareStatement(
         "SELECT foto_visi FROM visitantes WHERE cedula_visi = ?"); 
       ) { 
        ps.setString(1, id); 
        ResultSet results = ps.executeQuery(); 
        Image img = null ; 
        if (results.next()) { 
         Blob foto = results.getBlob("foto_visi"); 
         InputStream is = foto.getBinaryStream(); 
         img = new Image(is) ; // false = no background loading 
         is.close(); 
        } 
        results.close(); 
        return img ; 
       } catch (Throwable e) { 
        String info = e.getMessage(); 
        System.out.println(info); 
       } 
       return null; 
      } 
    } 

私は、問題はここにあると思う:

imgfotovisi.imageProperty().bind(imageRetrievalService.valueProperty()); 

退職したイメージがこの行のimageivewにロードされているかどうかわかりません。はい、bu私がするならば、イメージim = imgfotovisi.getImage(); 、javaはnull.then私はサークルに画像を取得することはできませんと言う。

答えて

1

bindは、画像自体をロードするつもりはない事前に感謝:)、それだけで、時にソースの変更(サービスのこの場合はvalueプロパティ)一つの変数が変更されるように結合し、これは、サービスが非同期で実行されているため、すぐには実行されません。したがって、バインドステートメントを発行した直後に値を照会すると、ソースがまだ変更されていないため、期待している結果が得られません。

代わりに、画像が実際に利用可能になった後にのみ対処する必要があります。例えば

imageRetrievalService.valueProperty().addListener((obs, oldVal, newVal) -> 
    if (newVal != null) 
     circulo.setFill(new ImagePattern(newVal)) 
); 

それとも、あなたがサービスへの直接リンクをしたい、とimgfotovsi画像プロパティが既にサービス値にバインドされていることを与えられていない場合:

imgfotovisi.imageProperty().addListener((obs, oldVal, newVal) -> 
    if (newVal != null) 
     circulo.setFill(new ImagePattern(newVal)) 
); 
+0

"バインドは画像自体を読み込まない"はい私はそれを考えていたが、私のプログラミング能力はそれを解決するために低いです。 D –

+0

こんにちは私はコードが動作していますが、 "画像"を変更するとエラーが発生します。 "JavaFXアプリケーションスレッド"スレッドで例外が発生しました。java.lang.NullPointerException:画像がnullでない必要があります。プログラムがgood.iを実行するので、その重要なエラーを取り除くかどうかはわかりません。初期化でイメージを円で塗りつぶしますが、リスナーがchagesを検出したときにエラーが表示されます。 –

+0

ifステートメントをリスナーに置くことができます。イメージがnull値に変更された場合、新しいfillを設定しようとしません。私はこれを反映するために答えを更新しました。 – jewelsea

関連する問題