0

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()); ???

0

2 Answers 2

5
players[0] = new PongPlayerThread(serverSocket.accept(), 0, rtnInfo());
players[0].start();
1
  • 1
    ok, when i now run my code i get this 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) if you want the code for the server just say
    – Canvas
    Commented Feb 12, 2013 at 22:43
2

The problem is that Thread.start() returns void, and you are trying to assign the result of new PPT(...).start() to players[0].

new PPT(...).start() returns void, and so the compiler is complaining that it can't convert this void into an object of type PongPlayerThread.

Not the answer you're looking for? Browse other questions tagged or ask your own question.