Why gets NullPointerException? When I send data from server to client it all ok.
Error java.lang.NullPointerException
at NetworkClient.Klient.wyslijDane(Klient.java:35)
EDIT Code I Added complete server class and client class In other class i create a new Object Client and Server
klient=new Klient(); and then call method WyslijDane. Server it's ok, and client throw exception.
Client Class
public class Klient {
public static final int PORT=50007;
public static Socket sock;
public static int msg=10;
public Klient() throws UnknownHostException, IOException
{
Socket sock=new Socket("localhost", PORT);
}
public static void wyslijDane(int GraczK) throws IOException
{
DataInputStream in=new DataInputStream(sock.getInputStream());
DataOutputStream out=new DataOutputStream(sock.getOutputStream());
// BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
out.writeInt(GraczK);
//out.writeBoolean(flaga);
out.flush();
System.out.println("Wyslano dane "+ GraczK );
}
}
Server Class
public class Server
{
public static final int PORT=50007;
public static MainWarSever main=new MainWarSever();
static Socket sock;
public Server() throws IOException
{
//tworzenie gniazda serwerowego
ServerSocket serv = null;
if (serv==null)
serv=new ServerSocket(PORT);
else
{
System.out.println("SErver już zajety brrrr");
serv.close();
}
sock=serv.accept();
System.out.println("Jest polaczenie: "+sock);
}
public static void wyslijDane(int GraczK) throws IOException
{
DataInputStream in=new DataInputStream(sock.getInputStream());
DataOutputStream out=new DataOutputStream(sock.getOutputStream());
boolean flaga=true;
int msg;
int i=0;
int msgOut;
msgOut=GraczK;
out.writeInt(msgOut);
out.writeBoolean(flaga);
out.flush();
System.out.println("Wyslano dane "+ GraczK + "Flage" + flaga);
}