引用类实例时的问题:' Broadcaster中的Broadcaster(Socket)无法应用于0

时间:2017-04-10 14:27:13

标签: java reference instance

我必须创建一个类的实例,而不是调用非静态方法。当我这样做时:

       @FXML
       public void initialize() {
       bConnect.setOnAction(d -> {
        try {
            new Broadcaster().serverConnection();
        } catch (IOException e) {
            e.printStackTrace();
        }
    });

我收到此警告:Broadcaster (Socket) in Broadcaster cannot be applied to 0。 我不知道要引用的变量。我该如何解决这个问题?

这是Broadcaster类中与套接字有关的部分:

public class Client {

private String serverAddress = "localhost";
private final int serverPort = 9001;

 @FXML
public void initialize() {
    bQuit.setOnAction(event -> Platform.exit());
    bConnect.setOnAction(d -> {
        try {
            new Broadcaster().serverConnection();
        } catch (IOException e) {
            e.printStackTrace();
        }
    });

class Broadcaster extends Thread {
    Socket clientSocket;
    private BufferedReader read;
    private PrintStream write;
    int counter = 0; //Countern finns för att testa funktionen.
    String questions;

    public Broadcaster(Socket clientSocket) throws IOException {
        this.clientSocket = clientSocket;
    }

    public void serverConnection() {

        ExecutorService executor = Executors.newFixedThreadPool(10000);

        Task<Void> connection = new Task<Void>() {

            @Override
            public Void call() {

                try {
                    clientSocket = new Socket(serverAddress, serverPort);

1 个答案:

答案 0 :(得分:0)

我通过删除Broadcaster类来解决这个问题,这是完全没必要的。现在方法调用按预期工作!

@FXML
public void initialize() {
    bQuit.setOnAction(event -> Platform.exit());
    bConnect.setOnAction(d -> serverConnection());
    aliasField.setOnAction(a -> {

        alias = aliasField.getText();

        Input.setOnAction(b -> {
            userAnswer = Input.getText();
        });
    });
    aliasField.setPromptText("Alias");
    Input.setPromptText("And your answer is:");
}

public void main(String[] args) {
}

public void serverConnection() {
    try {
        ServerSocket quizSocket = new ServerSocket(serverPort);
    } catch (IOException e) {
        e.printStackTrace();
    }
    ExecutorService executor = Executors.newFixedThreadPool(10000);

    Task<Void> connection = new Task<Void>() {