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, "cannot set connection");
 JOptionPane.showMessageDialog(null, "Connection established");
 try { in =new Scanner(socket.getInputStream());
 w = new PrintWriter(socket.getOutputStream(), true);
 } catch(IOException e) {
 JOptionPane.showMessageDialog(null, in);

 public void sendClient(String command) {

 public void Server() {
 try {
 s = new ServerSocket(port);
 } catch(IOException e) {
 JOptionPane.showMessageDialog(null, "cannot create server");


 public void processing() {
 while (true) {

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)
March 19th 20 at 08:22
1 answer
March 19th 20 at 08:24
ServerSocket s you have not initialized, I think. socket=s.accept() is causing the error. public void Server() seems to never invoked
Yes, it seems it is, thanks, now I will try to understand. - Kayla.Gaylo commented on March 19th 20 at 08:27

Find more questions by tags Java