Добрый день , пытаюсь реализовать вывод данных из сокета клиента в textarea javafx , но все время получаю ошибку :
Exception in thread "Thread-3" java.lang.NullPointerException: Cannot invoke "javafx.scene.control.TextArea.appendText(String)" because "this.text1" is null
at CCONTROL.send_message(CCONTROL.java:53)
at client_connect.printMessage(client_connect.java:48)
at client_connect.onDisconnect(client_connect.java:88)
at TCPConnection$1.run(TCPConnection.java:49)
at java.base/java.lang.Thread.run(Thread.java:832)
код контроллера :
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import java.io.IOException;
public class CCONTROL {
public client_connect con;
@FXML
private TextField nick;
@FXML
private TextArea text1;
@FXML
private TextField mess;
@FXML
private Button But2;
@FXML
private Button But1;
public String str=new String();
@FXML //but2
void onClickMethod2(ActionEvent event) {
try {
con=new client_connect();
}
catch(IOException e) {
e.printStackTrace();
}
}
@FXML //but1
void onClickMethod1(ActionEvent event) {
con.tcsend( str );
}
public void send_message(String s){
//text1.setText( s+"\r\n" );
}
}
код сокета:
import com.sun.glass.ui.Application;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.*;
import java.lang.Thread;
import java.util.ArrayList;
public class client_connect implements ActionListener,TCPConnectionListener {
private static final String IP_ADDRES = "127.0.0.1";// для ip
private static final int PORT = 5000;// для номера порта
private final TCPConnection tcpConnection;
public CCONTROL form = new CCONTROL();
public client_connect() throws IOException{
tcpConnection = new TCPConnection( this, IP_ADDRES, PORT );
}
@Override
public void run() {
try {
new client_connect();
}catch (IOException e){
e.printStackTrace();
}
}
private synchronized void printMessage(String s){
form.send_message( s );
}
public void tcsend(String str){
tcpConnection.sendMessage(str);
}
@Override
public void onConnectionReady(TCPConnection tcpConnection){
printMessage( "Соеденение готово... " );
}
@Override
public void onString(TCPConnection tcpConnection, String s){
printMessage( s );
}
@Override
public void onException(TCPConnection tcpConnection, Exception e){
printMessage( "ошибка соеденения "+e );
}
@Override
public void onDisconnect(TCPConnection tcpConnection){
printMessage( "соеденение закрыто" );
}
@Override
public void actionPerformed(ActionEvent e) {
}
}
код формы:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="474.0" prefWidth="408.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="CCONTROL">
<children>
<Button fx:id="But1" layoutX="320.0" layoutY="421.0" mnemonicParsing="false" onAction="#onClickMethod1" text="Button" />
<TextField fx:id="mess" layoutX="88.0" layoutY="421.0" prefHeight="25.0" prefWidth="232.0" />
<TextArea fx:id="text1" layoutX="31.0" layoutY="89.0" prefHeight="325.0" prefWidth="347.0" text="Отсутствует подключение к серверу" />
<TextField fx:id="nick" layoutX="21.0" layoutY="421.0" prefHeight="25.0" prefWidth="66.0" />
<Button fx:id="But2" layoutX="38.0" layoutY="34.0" mnemonicParsing="false" onAction="#onClickMethod2" prefHeight="25.0" prefWidth="333.0" text="Подключится к серверу" />
</children>
</AnchorPane>