title: Reason for "java.lang.NullPointerException" in my case?
body: <p>I have a little problem. I'm trying to bulid really simple RSA chat in java. But I don't know what correct in my code to avoid problems. When I run chat_Server.java, then chat_Client.java and send text it freezes, doesn't responding. And when I run only chat_Server.java, and send sth, then :</p>
  
  <p>(chat_Serwer.java:92) <strong>ois = new ObjectInputStream(s.getInputStream());</strong></p>
  
  <p>(chat_Serwer.java:13) <strong>public class chat_Serwer extends javax.swing.JFrame {</strong></p>
  
  <p>(chat_Serwer.java:53) <strong>msg_sendActionPerformed(evt);</strong> in GUI initComponents() </p>
  
  <pre><code>Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
  at chat.chat_Serwer.msg_sendActionPerformed(chat_Serwer.java:92)
  at chat.chat_Serwer.access$100(chat_Serwer.java:13)
  at chat.chat_Serwer$2.actionPerformed(chat_Serwer.java:53)
  at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
  at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
  at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
  at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
  at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
  at java.awt.Component.processMouseEvent(Component.java:6535)
  at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
  at java.awt.Component.processEvent(Component.java:6300)
  at java.awt.Container.processEvent(Container.java:2236)
  at java.awt.Component.dispatchEventImpl(Component.java:4891)
  at java.awt.Container.dispatchEventImpl(Container.java:2294)
  at java.awt.Component.dispatchEvent(Component.java:4713)
  at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
  at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
  at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
  at java.awt.Container.dispatchEventImpl(Container.java:2280)
  at java.awt.Window.dispatchEventImpl(Window.java:2750)
  at java.awt.Component.dispatchEvent(Component.java:4713)
  at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
  at java.awt.EventQueue.access$500(EventQueue.java:97)
  at java.awt.EventQueue$3.run(EventQueue.java:709)
  at java.awt.EventQueue$3.run(EventQueue.java:703)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
  at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
  at java.awt.EventQueue$4.run(EventQueue.java:731)
  at java.awt.EventQueue$4.run(EventQueue.java:729)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
  at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
  at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
  at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
  at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
  at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
  at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
  at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
  </code></pre>
  
  <blockquote>
    <p><strong>I'm begging for help.</strong></p>
  </blockquote>
  
  <p>Declaration:</p>
  
  <pre><code>public static int BIT_LENGTH = 1024;
  
  static ServerSocket ss;
  static Socket s;
  
  static ObjectInputStream ois;
  static ObjectOutputStream oos;
  </code></pre>
  
  <p>Action on button in Server.java:</p>
  
  <pre><code>private void msg_sendActionPerformed(java.awt.event.ActionEvent evt) {                                         
      try{
          ois = new ObjectInputStream(s.getInputStream());
          oos = new ObjectOutputStream(s.getOutputStream());
          //reading
  
          Message mess = (Message) ois.readObject();
          BigInteger d = mess.d;
          BigInteger n = mess.n;
          BigInteger e = mess.e;
          System.out.println("d: "+d);
          System.out.println("n: "+n);
          System.out.println("e: "+e);
  
          String msgout = "";
          msgout = msg_text.getText().trim();
  
          // conversion String to BigInteger
          String txt = msgout;
          byte[] msBytes = txt.getBytes();
          BigInteger msg = new BigInteger(msBytes);
          System.out.println(msg);
  
          // Encryption
          BigInteger enc = msg.modPow(e, n);
  
          // sending
  
          Message mes = new Message();
          mes.enc = enc;
          oos.writeObject(mes);
          oos.flush();
  
          }catch(IOException ex){
                  System.out.println("w przycisku: "+ex.getMessage());
          } catch (ClassNotFoundException ex) {
          Logger.getLogger(chat_Serwer.class.getName()).log(Level.SEVERE, null, ex);
      }
  }   
  </code></pre>
  
  <p>Main in Server.java</p>
  
  <pre><code>  // Prime numbers
        Random rand = new SecureRandom();
        BigInteger p = new BigInteger(BIT_LENGTH / 2, 100, rand);
        BigInteger q = new BigInteger(BIT_LENGTH / 2, 100, rand);
  
  
        BigInteger n = p.multiply(q);
        BigInteger phi = p.subtract(BigInteger.ONE)
                .multiply(q.subtract(BigInteger.ONE));
  
  
        BigInteger e;
        do e = new BigInteger(phi.bitLength(), rand);
        while (e.compareTo(BigInteger.ONE) &lt;= 0
                || e.compareTo(phi) &gt;= 0
                || !e.gcd(phi).equals(BigInteger.ONE));
        BigInteger d = e.modInverse(phi);
  
  
  
      try{
          ss = new ServerSocket(8080); //choose port
          s = ss.accept(); 
  
         //sending
          Message mes = new Message();
          mes.d = d;
          mes.n = n;
          mes.e = e;
          oos.writeObject(mes);
          oos.flush();
  
          //reading
          Message mess = (Message) ois.readObject();
          BigInteger enc = mess.enc;
  
           //decryption
           BigInteger dec = enc.modPow(d, n);
           String wejscie = new String(dec.toByteArray());
  
           msg_area.setText(msg_area.getText().trim()+"\n Serwer: "+wejscie); //shows message
  
      }catch( IOException z){
          System.out.println("w main: "+z);
      } catch (ClassNotFoundException ex) {
          Logger.getLogger(chat_Serwer.class.getName()).log(Level.SEVERE, null, ex);
      }
  </code></pre>
  
  <p>Message.java</p>
  
  <pre><code>package chat;
  
  import java.io.Serializable;
  import java.math.BigInteger;
  
  public class Message implements Serializable {
  
     public BigInteger e;
     public BigInteger d;
     public BigInteger n;
     public BigInteger enc;
     public BigInteger dec;  
  }
  </code></pre>
  
  <p>Client.java is almost the same. I'm only not sending e, d, n to Message.java, but read them.</p>