puoli
puoli

Reputation: 33

Java asks to create constructor that already exist

I'm trying to create a client and server application in java. But I receive a "The constructor ServerIO(Socket) is undefined" exception. Can someone tell me what I'm missing here?

My setup.server() function (simplified):

import java.net.Socket;
import java.net.ServerSocket;
import inputOutput.*;

public void server (int port) {
    try {
        ServerSocket sock = new ServerSocket(port);
        Socket client = sock.accept();
        ServerIO serverIO = new ServerIO(client);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

My input output handler constructor (simplified):

import java.net.Socket;

public ServerIO(Socket socket) {
    this.socket = socket;
    try {
        this.in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        this.out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
    } catch (IOException e){
        e.printStackTrace();
    }
}

Stack trace:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The constructor ServerIO(Socket) is undefined

    at Setup.server(Setup.java:29)
    at Setup.main(Setup.java:113)

Upvotes: 3

Views: 170

Answers (1)

Nexevis
Nexevis

Reputation: 4667

The point of the Project -> Clean option in Eclipse is to remove your already compiled files in order to build your project again from scratch.

It is likely you ran into a discrepancy between your code and the compiled file, with the compiled file not containing the constructor which was giving a confusing error.

I have found it useful to utilize this option when a confusing error happens, such as not finding something that clearly exists or an import seeming to not work. It is always a good idea to try to Clean to see if it fixes these issues, as it does not take very long to try it out either.

Upvotes: 1

Related Questions