Hey guys,

Can anyone tell me what I missed in the following code? I've tried everything that came up to my mind, but I can't pass this error.

Let me give you some context. My friend and I are making this program that is supposed to send message through command line. It's a simple computer-to-computer connection through our IPs.

My friend sent me this source code that worked pretty well on his IDE. But I'm having some problem with it still. I'm using NetBeans on Windows Vista.

Here's the source code:

package messengerserver;

import java.util.*;
import java.io.*;
import java.net.*;

class Main
{


    public static void main (String[] args) throws java.lang.Exception
    {
        try
        {
         ServerSocket ss = new ServerSocket (1234);
         Socket s;
         PrintWriter p;
         Scanner sc;
         Thread t = new Thread();
         while(true)
         {
           s = ss.accept();
           p = new PrintWriter(s.getOutputStream(),true);
           printAll.addWriter(p);
           sc = new Scanner(s.getInputStream());
           t = new Listener(sc);
           t.start();
         }
         }
         catch(Exception e)
         {
           e.printStackTrace();
         }
    }

    public static class printAll
    {
      private printAll () {}
      private static ArrayList<PrintWriter> p = new ArrayList<PrintWriter>();
      //private static ArrayList<PrintWriter> p;
      //p = new ArrayList<PrintWriter>();
      public static void addWriter(PrintWriter pr)
      {
        p.add(pr);
      }
      public static synchronized void printToAll(String text)
      {
      for(PrintWriter out : p) out.println(text);
      }

    }

    /*public*/ class Listener //implements Runnable//extends Thread
    {
      private Scanner sc;
      private String text;
      public Listener(Scanner sc)
      {
        this.sc = sc;
      }

      public void run ()
      {
      while(true)
      {
      text = sc.nextLine();
      printAll.printToAll(text);
      }
      }
    }
}

And here's the line that's giving me some trouble: t = new Listener(sc); Thank you in advance, all help is welcome.

-CrazyPixel

@JamesCherrill
It says:

"Non-static variable this cannot be referenced from a static context"

Thank you for the attention,

-CrazyPixel

i think that you line 26 is wrong,if you make the listener class to be static(line 53) you will get an error message of incompatible type i.e Listener expected but found thread

You are running your code in the static main method, but parts of your code are not static - ie need an instance of the class. Your static code refers to an instance member when you haven't created an instance.
A better general pattern is to get rid of all your statics (except the main method itself), take all the code out of main method and put it into a default constructor for your class. Then all you put in the main method is
new Main();
That creates the new instance and all your code runs with that instance.

This article has been dead for over six months. Start a new discussion instead.