0

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);
}
4
  • 2
    Wich one is line 35? Commented Oct 25, 2015 at 18:14
  • its this line 35 DataOutputStream out=new DataOutputStream(sock.getOutputStream()); Set Sock Client 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); } Commented Oct 25, 2015 at 18:30
  • Can you edit your question and add the part of code where you create the socket (on the client side)? Commented Oct 25, 2015 at 18:36
  • OK I was edit my question Commented Oct 25, 2015 at 18:52

2 Answers 2

1

1) Your are getting the NullPointerException because when you say sock.getOutputStream() or sock.getInputStream() you're using the sock variable declared outside the constructor wich is not initialized.

So instead of creating a new sock variable in the constructor use the one you already have:

//Change this line
Socket sock=new Socket("localhost", PORT);

//To this
Socket sock=new Socket("localhost", PORT);

2) Your sock variable shouldn't be declared as static and the wyslijDane should be called after the constructor is executed (the socket created) so it shouldn't be static.

3) Make sure you call the wyslijDane method after you create the Socket.

4) First create and flush the DataOutputStream and then create the DataInputStream. They should be in the same order both in the client and in the server code:

DataOutputStream out=new DataOutputStream(sock.getOutputStream());
out.flush();
DataInputStream in=new DataInputStream(sock.getInputStream());

Since you said the exception is thrown when you create the DataOutputStream the problem should be solved.


Suggestion:

As a good practice, don't create the DataOutputStream and the DataInputStream everytime you call the wyslijDane method. Instead create the DataOutputStream and the DataInputStream just after you create the socket.

Sign up to request clarification or add additional context in comments.

Comments

1

What is sock set to? If sock is not the reason for the NullPointerException, code with line numbers would be helpful.

EDIT: After you've added your complete source code ... when calling static wyslijDane(int GraczK) your Klient() constructor setting sock must not have been called. Making your variables and methods not-static would clarify this.

EDIT: BTW in your client class you define two sock variables, one static and one in the constructor, the latter is destroyed after leaving the constructor.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.