im trying to create a class that is a thread that can be stored in an array, which will make it easier for me to handle user inputs from clients to the server.
here is my PongPlayerThread
package Pong;
import java.net.*;
import java.io.*;
public class PongPlayerThread extends Thread
{
private Socket socket = null;
private String pongData = "";
public PongPlayerThread()
{
//clear
}
public PongPlayerThread(Socket socket, int id, String data)
{
//super("PongPlayerThread");
pongData = data;
this.socket = socket;
}
public void passData(String data)
{
pongData = data;
}
public void run()
{
try
{
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
while(true)
{
out.println(pongData);
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
now in this line on my server where a client can connect
players[0] = new PongPlayerThread(serverSocket.accept(), 0, rtnInfo()).start();
I get this error "cannot convert from void to PongPlayerThread" why is that? I have searched but it is quite hard when you get the error with your classname.
I now have this code
players[idPlayer] = new PongPlayerThread(serverSocket.accept(), idPlayer, rtnInfo());
players[idPlayer].start();
but when a client connects i get this error Pong 400.0:301.0:60.0:300.0:740.0:300.0 Server started Server was setup and will try to create a socket Exception in thread "main" java.lang.NullPointerException at Pong.PongServer.main(PongServer.java:82)
the line it is pointing at is players[idPlayer] = new PongPlayerThread(serverSocket.accept(), idPlayer, rtnInfo()); ???