Variables with null value when calling a Thread?

0

I am trying to use threads in my application to not block the user's main thread, I implemented the Runnable interface in my JFrame, I created the run () method and at the moment of running the application it launches a NullPointerException because apparently the variables I try use are in null

A list of checkboxes is generated, and when a button is pressed, the checkboxes that are active are saved in the 'routes' array, which if filled (verified with the debugger)

private void jBtGuardarRutasActionPerformed(java.awt.event.ActionEvent evt) {                                                
    // TODO add your handling code here:
   for(int i=0;i<totalcheck;i++){           
           rutas[i]=null;           
   }  
   for(int i=0;i<totalcheck;i++){
       if(check[i].isSelected()==true){
           rutas[i]=check[i].getText();
       }
   }
   for(int a=0;a<rutas.length;a++){
       if(rutas[a]!=null){
           System.out.println(rutas[a]);
           tamnovacio++;
       }else{

       }

   }              
   System.out.println("##############################");

}    

However, at the moment of calling the thread, in the debugger the variables appear null

Thread carga;
private void jBtCargarActionPerformed(java.awt.event.ActionEvent evt) {                                          

    try {
        carga = new Thread(new Interfaz());
        carga.start();
    } catch (InterruptedException ex) {
        Logger.getLogger(Interfaz.class.getName()).log(Level.SEVERE, null, ex);
    }
}                                         

@Override
public void run() {
        for(int i=0; i< rutas.length ;i++){ //Aquí manda la exepción
            if(rutas[i]!=null){
                RutaOrigen=rutas[i];
                jLabel34.setText(RutaOrigen);
                try {                                          
                    vec = new Vector<String>();
                    String cadena="";
                    String salida="";
                    File file;
                    file = new File(RutaOrigen);
                    BufferedReader in=new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.ISO_8859_1));
                    try {
                        while((cadena = in.readLine())!=null) {
                            vec.add(cadena);
                            //salida+=cadena+"\n";
                        }
                        for(int x=0;x<vec.size();x++){
                            salida+=vec.elementAt(x) +"\n";
                        }
                        in.close();

                        jTaTrabajo.setText(salida);
                    } catch (FileNotFoundException ex) {
                        Logger.getLogger(Interfaz.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (IOException ex) {
                        Logger.getLogger(Interfaz.class.getName()).log(Level.SEVERE, null, ex);
                    }


                } catch (FileNotFoundException ex) {
                    Logger.getLogger(Interfaz.class.getName()).log(Level.SEVERE, null, ex);
                }
                Modificar();
                GeneraArchivo();
            }
        }
}

Just in the first line of the run gives me an exception because the previous filling of the 'routes' array detects it as empty. Does anyone know what is happening or any suggestions?

    
asked by Jorge Armando Ramírez Rocha 26.07.2018 в 18:56
source

0 answers