Java FX problem z wywołaniem funkcji

0

Witam,

Chciałem stworzyć metodę która będzie zmieniać tekst w TextArea. Jeżeli korzystam z metody (super.test()) w klasie w której ją stworzyłem to wszystko działa, lecz napotykam problem w momencie którym klasa dziedziczy tą metodę. Po wywołaniu tej funkcji zwracany jest NullPointerException.

Klasa Controler

public class Controller implements Initializable {


    @FXML
    TextField tf1;
    @FXML
    TextField tf2;
    @FXML private Button button;
    @FXML
    public TextArea logarea;
    public ArrayList<String> stringArrayList= new ArrayList<>();
    public ObservableList<String> observableArray = FXCollections.observableArrayList(stringArrayList);




    @Override
        public void initialize(URL location, ResourceBundle resources) {

        observableArray.addListener(new ListChangeListener<String>() {
            @Override
            public void onChanged(Change<? extends String> c) {
                System.out.println("Changed on " + c);
                if (c.next()) {
                    System.out.println(c.getFrom());
                }
            }
        });
        System.out.println("Initialize");


        }

    @FXML
    public void start(){

        
        System.out.println("Button");
        System.out.println(tf1.getText()+tf2.getText());
        long temp=Long.parseLong(tf2.getText());
        Runnable runnable = new CheckFiles(tf1.getText(), temp * 1000);
        Thread thread = new Thread (runnable);
        thread.start();
        //Runnable runnable1 = monitor();
        test();




    }

    @FXML
    public void end() {

        System.exit(1);

    }

   ** public void test()**{
        logarea.setText(stringArrayList.toString());
    }

Klasa która dziedziczy funcję

public class CheckFiles extends Controller implements Runnable  {

    public CheckFiles() {
        System.out.println("Utworzono wątek");
    }

    private Boolean isValid=true;
    private String Path=null;
    private long Interval=300000;


    public CheckFiles(String path,long interval) {
        System.out.println("Utworzono wątek");
        Interval=interval;
        File file = new File(path);
        if(file.isDirectory()){
            isValid=true;
            Path=path;
        }
        else{
            isValid=false;
        }
    }

    @Override
    public void run() {

        while(true) {
            System.out.println("Watek "+Thread.currentThread());
            try {
                if(isValid==true) {
                    String[] first=listfiles();
                    Thread.sleep(Interval);
                    System.out.println("Sleep minął");
                    String [] second=listfiles();
                    System.out.println(Arrays.equals(first,second));
                    if(!Arrays.equals(first,second)){
                        Platform.runLater(
                                () -> {
                                    try {
                                        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("notification.fxml"));
                                        Parent root1 = fxmlLoader.load();
                                        Stage stage = new Stage();
                                        stage.setScene(new Scene(root1));
                                        stage.setTitle("Błąd");
                                        stage.show();
                                    }catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                        );
                    }
                    else{
                        super.stringArrayList.add(Path);
                        **super.test();**
                    }
                    }
                }

             catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private String[] listfiles(){
       File file = new File(Path);
        ArrayList<String> arrayList= new ArrayList<>();
        File[] files= file.listFiles();
        //String[] paths= new String[files.length];
        for(int i=0;i<files.length;i++){
            //paths[i]=files[i].getPath();
            //System.out.println(paths[i]);
            arrayList.add(files[i].getPath());
            if(files[i].isDirectory()){
                File[] temp=files[i].listFiles();
                for(int x=0;x<temp.length;x++){
                    arrayList.add(temp[x].getPath());
                }
            }
        }
        String[] paths = new String[arrayList.size()];
        for(int y=0;y<arrayList.size();y++){
            paths[y]=arrayList.get(y);
            //System.out.println(paths[y]);
        }
        //System.out.println(files.toString());
        return paths;


    }
0

Wyeliminuj dziedziczenie, stwórz z tego serwis, przekaz ewentualnie referencje do kontrolera, i zamiast super, wywołaj test na tej referencji.
Nullpointer pewnie leci bo pewnie logarea jest nullem, ale to musisz sprawdzić w debugu.
Pamiętaj, debuger twoim najlepszym przyjacielem

0
  1. Wywal extends, to nie ma sensu, nic Ci nie daje
  2. Zmień tą linie:
 Runnable runnable = new CheckFiles(tf1.getText(), temp * 1000);

na coś takiego:

 Runnable runnable = new CheckFiles(tf1.getText(), temp * 1000, this);
  1. dalej, konstruktor:
public CheckFiles(String path,long interval) {
        System.out.println("Utworzono wątek");
      

na:

public CheckFiles(String path,long interval,, final Controller  controller ) {
        System.out.println("Utworzono wątek");
  this.controller = controller;
  1. i wtedy:
else{
                        super.stringArrayList.add(Path);
                        controller.test();
                   }
                    }
0

Dziś wprowadziłem powyższe zmiany i wszystko działa, Wielkie dzięki!
Czyli, o ile dobrze rozumiem w konstruktorze dodałeś nowy parametr Controler, a następnie przekazałeś przy inicjacji obiektu checkfiles, controler który już istnieje?

1 użytkowników online, w tym zalogowanych: 0, gości: 1