V
V
voron_kor2021-01-17 12:06:49
Java
voron_kor, 2021-01-17 12:06:49

How to organize data output from client socket to javafx textarea?

Good afternoon, I'm trying to implement data output from a client socket in textarea javafx , but I keep getting an error:
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)

controller code :

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" );
    }
}


socket code:
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) {
    }
}


form code:

<?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>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ryabos, 2021-01-22
@Ryabos

Try to connect and execute other logic inside the Platform.runLater(() -> {...}) block
Perhaps the thing is that the TextArea is accessed even before it is drawn

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question