Problem starting Javafx application

Asked

Viewed 54 times

0

Hello,

I’m trying to create a simple application in Javafx, everything was working until suddenly started giving problem when trying to start the program.

You’re making the following exception: Exception in Application start method java.lang.reflect.Invocationtargetexception

Could you help me identify the problem? Thank you!

public class Principal extends Application {

  public static void main(String[] args) {
    launch(args);
  }

  @Override
  public void start(Stage primaryStage) throws Exception {
   primaryStage.setTitle("Trabalho PC");       

   Label lbDados = new Label("Dados:");
   lbDados.setTranslateX(25);
   lbDados.setTranslateY(65);

   Label lbCod = new Label("Codificação: ");
   lbCod.setTranslateX(25);
   lbCod.setTranslateY(90);

   ToggleGroup groupDados = new ToggleGroup();       
   RadioButton rbTexto = new RadioButton("Texto");
   rbTexto.setToggleGroup(groupDados);
   rbTexto.setSelected(true);
   rbTexto.setTranslateX(80);
   rbTexto.setTranslateY(65);

   RadioButton rbBinario = new RadioButton("Binário");
   rbBinario.setToggleGroup(groupDados);
   rbBinario.setTranslateX(155);
   rbBinario.setTranslateY(65);

   ToggleGroup groupCod = new ToggleGroup();
   RadioButton rbCBinaria = new RadioButton("Binária");
   rbCBinaria.setToggleGroup(groupCod);
   rbCBinaria.setSelected(true);
   rbCBinaria.setTranslateX(115);
   rbCBinaria.setTranslateY(90);

   RadioButton rbCManchester = new RadioButton("Manchester");
   rbCManchester.setToggleGroup(groupCod);
   rbCManchester.setTranslateX(200);
   rbCManchester.setTranslateY(90);

   RadioButton rbCManchesterDif = new RadioButton("Manchester Diferencial");
   rbCManchesterDif.setToggleGroup(groupCod);
   rbCManchesterDif.setTranslateX(315);
   rbCManchesterDif.setTranslateY(90);

   Button btnIniciar = new Button();
   btnIniciar.setText("Iniciar");
   btnIniciar.setPrefSize(75, 25);
   btnIniciar.setTranslateX(400);
   btnIniciar.setTranslateY(25);

   TextField tfTexto = new TextField();
   tfTexto.setPrefSize(350, 10);
   tfTexto.setTranslateX(25);
   tfTexto.setTranslateY(25);       
   tfTexto.setText("Digite o texto ou binário");

   Slider sldVelocidade = new Slider();
   sldVelocidade.setMin(0);
   sldVelocidade.setMax(20);
   sldVelocidade.setValue(10);
   sldVelocidade.setShowTickMarks(true);
   sldVelocidade.setMajorTickUnit(5);
   sldVelocidade.setMinorTickCount(1);
   sldVelocidade.setBlockIncrement(10);
   sldVelocidade.setTranslateX(190);
   sldVelocidade.setTranslateY(125);

   Label lbSlow = new Label("Slow");
   lbSlow.setTranslateX(195);
   lbSlow.setTranslateY(150);

   Label lbFast = new Label("Fast");
   lbFast.setTranslateX(298);
   lbFast.setTranslateY(150);

   Image image = new Image(getClass().getResourceAsStream("Fundo.png"));

   ImageView imvFundo = new ImageView();
   imvFundo.setFitHeight(315);
   imvFundo.setFitWidth(507);
   imvFundo.setX(15);
   imvFundo.setY(220);
   imvFundo.setImage(image);

   Image image2 = new Image(getClass().getResourceAsStream("Teste.png"));

   ImageView[] imvCod = new ImageView[9];
   for(int x = 0; x < imvCod.length; x++){
       imvCod[x] = new ImageView();
       imvCod[x].setFitHeight(53);
       imvCod[x].setFitWidth(52);
   }

   imvCod[0].setX(459);
   imvCod[0].setY(232);

   imvCod[1].setX(404);
   imvCod[1].setY(232);

   imvCod[2].setX(350);
   imvCod[2].setY(232);

   imvCod[3].setX(296);
   imvCod[3].setY(232);

   imvCod[4].setX(241);
   imvCod[4].setY(232);

   imvCod[5].setX(187);
   imvCod[5].setY(232);

   imvCod[6].setX(133);
   imvCod[6].setY(232);

   imvCod[7].setX(78);
   imvCod[7].setY(232);

   imvCod[8].setX(24);
   imvCod[8].setY(232);

   Pane pnpPrincipal = new Pane();
   pnpPrincipal.setPrefSize(550, 580);
   pnpPrincipal.getChildren().addAll(tfTexto, btnIniciar, rbTexto, rbBinario, lbDados, lbCod, rbCBinaria, rbCManchester, rbCManchesterDif, sldVelocidade, lbSlow, lbFast, imvFundo);
   for(int x = 0; x < imvCod.length; x++){
        pnpPrincipal.getChildren().add(imvCod[x]);
   }
   Scene scene = new Scene(pnpPrincipal, 550, 580);

   primaryStage.setScene(scene);
   primaryStage.show();
   btnIniciar.setOnAction(new EventHandler<ActionEvent>() {
        @Override 
        public void handle(ActionEvent e) {
            String dados = tfTexto.getText();
            tfTexto.setEditable(false);
            rbTexto.setDisable(true);
            rbBinario.setDisable(true);
            rbCBinaria.setDisable(true);
            rbCManchester.setDisable(true);
            rbCManchesterDif.setDisable(true);
            btnIniciar.setDisable(true);
            if(groupCod.getSelectedToggle() == rbCBinaria){
                if(groupDados.getSelectedToggle() == rbTexto){
                    System.out.println("Binária / Texto");
                    Binaria bin = new Binaria(tfTexto.getText(), sldVelocidade, imvCod, 't');
                    bin.start(); 
                } else {
                    System.out.println("Binária / Binário");
                    Binaria bin = new Binaria(tfTexto.getText(), sldVelocidade, imvCod, 'b');
                    bin.start(); 
                }
            } else if (groupCod.getSelectedToggle() == rbCManchester){
                if(groupDados.getSelectedToggle() == rbTexto){
                    System.out.println("Manchester / Texto");
                } else {
                    System.out.println("Manchester / Binário");
                }
            } else {
                if(groupDados.getSelectedToggle() == rbTexto){
                    System.out.println("Manchester Diferencial / Texto");
                } else {
                    System.out.println("Manchester Diferencial / Binário");
                }
            }
        }
    });
  }    
}

public class Binaria extends Codificacao {
  Slider sldVelocidade;
  ImageView[] imvCod;
  char op;
  String texto;

  public Binaria(String texto, Slider sldVelocidade, ImageView[] imvCod, char op){
    this.sldVelocidade = sldVelocidade;
    this.imvCod = imvCod;     
    this.op = op;
    this.texto = texto;
  }

  public void run() {
    Image vazio = new Image(getClass().getResourceAsStream("B1.png"));
    if(this.op == 't'){
        this.trasnformaString(texto);
    } else if(this.op == 'b'){
        this.separaBinario(texto);
    }
    while(this.fila.vazia() == false){            
        imvCod[8].setImage(imvCod[7].getImage());
        imvCod[7].setImage(imvCod[6].getImage());
        imvCod[6].setImage(imvCod[5].getImage());
        imvCod[5].setImage(imvCod[4].getImage());
        imvCod[4].setImage(imvCod[3].getImage());
        imvCod[3].setImage(imvCod[2].getImage());
        imvCod[2].setImage(imvCod[1].getImage());
        imvCod[1].setImage(imvCod[0].getImage());
        imvCod[0].setImage(vazio);

        if(imvCod[0].getImage().equals(vazio)){
            String bin = this.fila.remove();
            System.out.println(bin);                
            Image image = new Image(getClass().getResourceAsStream("B"+bin+".png"));
            imvCod[0].setImage(image);
        }
        try {
            Thread.sleep(500);
        } catch (InterruptedException ex) {
            Logger.getLogger(Binaria.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
  }    
}

public abstract class Codificacao extends Thread {
  Fila fila = new Fila();
  public void trasnformaString(String texto){
    for (char c: texto.toCharArray()) {  
        String bin = Integer.toBinaryString(c);  
        int i=0;
        int qtd = bin.length();
        for(i=0;i<qtd;i++){
            this.fila.insere(""+bin.charAt(i));
        }
    }
  }    

  public void separaBinario(String binario){
    int i=0;
    int qtd = binario.length();
    for(i=0;i<qtd;i++){
       this.fila.insere(""+binario.charAt(i));
    }
  }
}

public class Fila {
  private List<String> alunos = new LinkedList<String>();

  public void insere(String bit) {
    this.alunos.add(bit);
  }

  public String remove() {
    return this.alunos.remove(0);
  }

  public boolean vazia() {
    return this.alunos.size() == 0;
  }
}

1 answer

0

In the tests I performed on my computer the problems happened in these two lines:

Image image = new Image(getClass().getResourceAsStream("Fundo.png"));
// ...
Image image2 = new Image(getClass().getResourceAsStream("Teste.png"));

Because the files "Fundo.png" and "Teste.png" were not being found.

I haven’t seen your full stacktrace but if it matches what happened in my test you would find the real problem below:

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:498) at com.sun.javafx.application.Launcherimpl.launchApplicationWithArgs(Launcherimpl.java:389) at com.sun.javafx.application.Launcherimpl.launchApplication(Launcherimpl.java:328) 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:498) at sun.launcher.Launcherhelper$Fxhelper.main(Launcherhelper.java:767) Caused by: java.lang.Runtimeexception: Exception in Application start method at com.sun.javafx.application.Launcherimpl.launchApplication1(Launcherimpl.java:917) at com.sun.javafx.application.Launcherimpl.lambda$launchApplication$154(Launcherimpl.java:182) at java.lang.Thread.run(Thread.java:748) Caused by: java.lang.Nullpointerexception: Input stream must not be null at javafx.scene.image.Image.validateInputStream(Image.java:1128) at javafx.scene.image.Image.(Image.java:706) at principal.Principal.start(Home.java:105)

So if you moved or deleted the files should update your path.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.