M
M
Mark Karas2019-03-24 22:09:23
Java
Mark Karas, 2019-03-24 22:09:23

Exception in thread "main" java.lang.NullPointerException how to fix?

package Default;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

import javax.swing.JOptionPane;

public class Connect {
  int port = 2020;
  ServerSocket s;
  Scanner in ;
  PrintWriter w;

  private void connect() {
    Socket socket;
    try {
      socket = s.accept();
    } catch(IOException e) {
      JOptionPane.showMessageDialog(null, "Не могу установить соединение");
      return;
    }
    JOptionPane.showMessageDialog(null, "Соединение установлено");
    try { in =new Scanner(socket.getInputStream());
      w = new PrintWriter(socket.getOutputStream(), true);
    } catch(IOException e) {
      return;
    }
    JOptionPane.showMessageDialog(null, in);
  }

  public void sendClient(String command) {
    }

  public void Server() {
    try {
      s = new ServerSocket(port);
    } catch(IOException e) {
      JOptionPane.showMessageDialog(null, "Не могу создать сервер");
      System.exit(0);
    }

  }

  public void processing() {
    while (true) {
      connect();
    }
  }
}

The error itself:
Exception in thread "main" java.lang.NullPointerException
  at Default.Connect.connect(Connect.java:21)
  at Default.Connect.processing(Connect.java:54)
  at Default.Server.main(Server.java:8)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
EVGENY T., 2019-03-24
@FreeTile

ServerSocket s you have not initialized, it seems to me. socket=s.accept() throws an error. public void Server() doesn't seem to be called anywhere

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question