2014-12-09 4 views
0

FXMLDocumentController 클래스가 있습니다. 클래스 Thread (파일에서 데이터를 읽음)에서 상속 한 클래스 ThreadForFile이 있습니다. 버튼 (FXMLDocumentController 클래스의)을 누른 후 ThreadForFile 클래스의 흐름을 만듭니다. ThreadForFile 클래스에서, 읽을 때 TextArea에 표시된 문자열이 필요합니다. Exception in thread "JavaFX Application Thread" java.lang.NullPointerException 다른 클래스의 TextArea 변경

:이 텍스트 영역 클래스 필드를 할당하지만 내가 매개 변수 텍스트 영역을 통과하면 파악하고, 생성자에서 변경할 수 없습니다,이 component.But에 변화가, 그것은 오류를 표시합니다

ThreadForFile extends Thread

private String path; 
private long ms; 
private int id; 
private String name; 
private boolean stop = false; 
private HTMLEditor web; 
private DB db = new DB(); 
private Button doc; 
public void setMS(long ms){ 
    System.out.print(ms); 
    this.ms =ms; 
} 
public ThreadForFile(String name,String path,long ms,int id,Button doc) { 
    this.path = new String(); 
    this.path = path; 
    this.ms = ms; 
    this.id = id; 
    this.name = name; 

    this.doc = new Button(); 
    this.doc = doc; 

} 

public void Stop(boolean stop){ 
    this.stop = stop; 
} 

public void run() { 
    try { 
     doc.setText("Zsczsc"); 
     System.out.print("asdasd"); 
     File file = new File(path); 
     File file1 = new File("C:\\out.txt"); 
     BufferedReader br = new BufferedReader (new InputStreamReader(new FileInputStream(file), "UTF-8")); 
     String line = null; 
     while((line = br.readLine()) != null){ 

       if(!Thread.interrupted()) //Проверка прерывания 
       { 
        if(!stop){ 
        PrintWriter out = new PrintWriter(file1.getAbsoluteFile()); 
        try { 
         sytem.out.print(line+"\n"); 
        } finally { 
        out.close(); 
        } 
      Thread.sleep(db.getParam().getMS()*1000); 

      System.out.print("ms" + db.getParam().getMS()); 

     } 

      }else{ 
      return;  
      } 
     }  
    } catch (Exception ex) { 
      System.out.print(ex.toString()); 
      Logger.getLogger(ThreadForFile.class.getName()).log(Level.SEVERE, null, ex); 
     } 
} 

public void read()throws FileNotFoundException, IOException 
{ 


} 

FXMLDocumentController

<!-- language: JavaFX --> 

public class FXMLDocumentController implements Initializable { 
private long ms = 5; 
    private ObservableList<ThreadForFile> threadForFile = FXCollections.observableArrayList(); 
    private ObservableList<threadFile> threadFile = FXCollections.observableArrayList(); 
    private int index ; 
    private DB db = new DB(); 
    private Parametrs param = new Parametrs(); 
    @FXML 
    private Button btnAdd; 
    @FXML 
    private Button btnStop; 
    @FXML 
    public Button btnDel; 
    @FXML 
    private Button btnStart; 

    @FXML 
    private TextField textFValueText; 
    @FXML 
    private TextField textFMs; 
    @FXML 
    private Button btnUpdate; 

    @FXML 
    public static TextArea textArea; 

    @FXML 
    private Label nameLabel; 
    @FXML 
    private Label pathLabel; 

    @FXML 
    private TextField textFName; 
    @FXML 
    private TextField textFPath; 

    @FXML 
    private TableColumn nameCol; 
    @FXML 
    private TableColumn pathCol; 
    @FXML 
    private TableColumn statCol; 


    public static FXMLDocumentController doc; 
    private ResourceBundle bundle; 
    @FXML 
    private TableView table; 



    @FXML 
    private void handleButtonAction(ActionEvent event) { 
     System.out.println("You clicked me!"); 
    } 



    @Override 
    public void initialize(URL url, ResourceBundle rb) { 

     bundle = rb; 


     System.out.println("You clicked me!"); 
     FileRead file = new FileRead(FXMLDocumentController.this); 
     Tab1(); 
     Tab2(); 
     Tab3(); 


     param = db.getParam(); 
     System.out.print(param.getMS() + " " + param.getValue()); 


    } 

    public void Tab1() 
    { 



    } 
    public void Tab2() 
    { 










     //root.getChildren().addAll(btn,table,btnStop,btnStart,btnDel,nameField,pathField,name,path); 

     btnAdd.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() { 
      @Override 
      public void handle(MouseEvent mouseEvent) { 

       threadFile.add(new threadFile(textFName.getText(),textFPath.getText(),1)); 
       threadForFile.add(new ThreadForFile(threadFile.get(threadFile.size()-1).getName(),threadFile.get(threadFile.size()-1).getPath(),ms,threadFile.size(),btnAdd)); 
       threadForFile.get(threadForFile.size()-1).start(); 
      index = table.getSelectionModel().getSelectedIndex(); 
       System.out.print(index+ "\n"); 

      } 
     }); 
. 
. 
. 
. 
. 
. 
. 
. 
. 
. 
} 
+0

문제를 더 잘 이해하고 해결할 수 있도록 [SSCCE] (http://sscce.org/)를 제공하십시오. –

+0

'NullPointerException'을 던지는 행은 무엇입니까? –

+0

doc.setText ("Zsczsc"); – user3054314

답변

1

JavaFX Application thread 외부의 JavaFX Control에 액세스하려고합니다.

컨트롤러 밖으로 컨트롤러를 사용하거나 다른 방법으로 매개 변수로 전달하지 마십시오. 대신 컨트롤러에 데이터를 전달하면 컨트롤러 내부의 컨트롤로 설정됩니다.

몇 가지 유용한 링크가 컨트롤러에 데이터를 전달에 있습니다

Passing Parameters JavaFX FXML

How to have constructor with arguments for controller?

자바 FX 멀티 스레딩에 대한 몇 가지 유용한 링크 :

How do I safely modify JavaFX GUI nodes from my own Thread?

JavaFX working with threads and GUI

관련 문제