No enclosing instance of type Server is accessible

I'm encountering a weird issue in Java at the moment that I've never seen before.

The error is "No enclosing instance of type Server is accessible. Must qualify the allocation with an enclosing instance of type Server (e.g. x.new A() where x is an instance of Server)."

The line I've commented on is where the error occurs.

package game;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;

public class Server {

    private static List<ThreadModtagClient> clients;

    class ReceiveDataListener implements SocketListener {

        @Override
        public void dataReceived(ThreadModtagClient client, String data) {

        }   
    }

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub

        clients = new ArrayList<ThreadModtagClient>();

        ServerSocket welcomeSocket = new ServerSocket(16567);

        while (true) {
            Socket connectionSocket = welcomeSocket.accept();

            ThreadModtagClient client = new ThreadModtagClient(connectionSocket);
            ReceiveDataListener listener = new ReceiveDataListener(); // <--- this is where the error occurs
            client.addSocketListener(listener);
            clients.add(client);
        }

    }
}

class ThreadModtagClient extends Thread implements SocketThread {

    private BufferedReader inFromClient;
    private DataOutputStream outToClient;

    private Player player;

    private List<SocketListener> listeners;


    public ThreadModtagClient(Socket connection) throws IOException {
        inFromClient = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        outToClient = new DataOutputStream(connection.getOutputStream());
        listeners = new ArrayList<SocketListener>();

    }

    public void addSocketListener(SocketListener listener) {
        listeners.add(listener);
    }

    public void removeSocketListener(SocketListener listener) {
        listeners.remove(listener);
    }

    public Player getPlayer() {
        return player;
    }

    public void setPlayer(Player player) {
        this.player = player;
    }


    public void sendData(String data) throws IOException {
        outToClient.writeChars(data);
    }

    public void run() {

        while (true) {
            try {

                String data = inFromClient.readLine();

                for(SocketListener listener : listeners) {
                    listener.dataReceived(this, data);
                }

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch(NoSuchElementException e1) {
                e1.printStackTrace();
            }
        }
    }
}

Solution 1:

Server.ReceiveDataListener is a (non-static) inner class. You are creating it from a static context. You need to supply an instance of Server to be the outer instance. However, almost certainly you want ReceiveDataListener to be a static nested class, or probably an outer class.

Solution 2:

well the error tells you exactly what needs to be done. ReceiveDataListener is a non-static inner class and must be accessed via an object of the outer class (Server). You have three options: 1. take the compiler's advice (access via an object of Server) 2. make ReceiveDataListener static 3. pull ReceiveDataListener out to a separate .java and use it.

HTH

Solution 3:

You cannot instantiate a non-static inner class from a static context like main.

Solution 4:

This is because you're trying to create a ReceiveDataListener from a static method, and since ReceiveDataListener is not a static class, it needs to be attached to an instance of Server.