0

Eclipse for Java has stopped working. All the projects I have do not running. I think I have a problem with the server. I'm asking for help.

public class Test 
{
    private InetAddress host;
    private static final int port = 2048;
    
    private void connectToServer() 
    {
        Socket link=null;
        Scanner input=null;
        Scanner userEntry=null; 
        try {
            link=new Socket(host , port);
            input = new Scanner(link.getInputStream());
            PrintWriter output = new PrintWriter(link.getOutputStream(),true);
            userEntry=new Scanner(System.in);
            String message,response;
            do {
                System.out.println("Enter message: ");
                message=userEntry.nextLine();
                output.println(message);
                response=input.nextLine();
                System.out.println("Server response: "+ response);
            }
            while (!message.equals("Close"));
        } 
        catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            input.close();
            userEntry.close();
        }   }
    public static void main(String[] args) throws IOException {
        Test client = new Test();
        client.host=InetAddress.getLocalHost();  
        client.connectToServer();
    }   }

The program shows this error:

java.net.ConnectException: Connection refused: connect
at java.base/sun.nio.ch.Net.connect0(Native Method)
at java.base/sun.nio.ch.Net.connect(Net.java:493)
at java.base/sun.nio.ch.Net.connect(Net.java:482)
at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:588)
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:339)
at java.base/java.net.Socket.connect(Socket.java:603)
at java.base/java.net.Socket.connect(Socket.java:552)
at java.base/java.net.Socket.<init>(Socket.java:475)
at java.base/java.net.Socket.<init>(Socket.java:283)
at SerializableDemo.Test.connectToServer(Test.java:20)
at SerializableDemo.Test.main(Test.java:49)
Exception in thread "main" java.lang.NullPointerException
at SerializableDemo.Test.connectToServer(Test.java:39)
at SerializableDemo.Test.main(Test.java:49)
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • In the error message it says two lines of your connectToServer method seem to be responsible for the issue : line 20 and line 39. Have you tried examining the code to find the cause of these errors ? – Angela Lopez Jan 16 '22 at 16:19
  • Тhe error occurs in all projects I run. He is not the only one with this code – Barış Seyitoğlu Jan 16 '22 at 16:32

0 Answers0