Hashtable problem when working with Threads and Sockets

I'm a newbit at Java, i'm having a problem with creating an employee instance. When the instance is created a hashtable is set up with with some staff information. Problem is that this information seems to be lost somewhere?!

in Client the following line getNoEmployees should write out 2, but instead i get an exception null error. Its like the employeeTable varible has been lost?! Why is this - does it have something to do with threads?

        employeeTable = new Employee();
        System.out.println(employeeTable);
        employeeTable.getNoEmployees();


I've included all my code bellow. It simply sets up a socket to wait for input from a client, that will return employee information.

Thank you for all you help,
Jason

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


/**
 * Contains employee information
 */
class Employee
{
  public Hashtable employeeTable;

  /**
   * Construct employee hashtable
   */
  public Employee()
  {
    Hashtable employeeTable = new Hashtable();
    employeeTable.put("a", "10000");
    employeeTable.put("b", "20000");
    employeeTable.put("c", "30000");
    System.out.println("Employee Table set up");
    System.out.println(employeeTable.get("a"));
  }


  public int getNoEmployees()
  {
    return employeeTable.size();
  }

  public String getSalaryOf(String i_employeeName)
  {
    return (String) employeeTable.get(i_employeeName);
  }

  public int getNoEmployeesWithSalaryGreaterThan(int i_salary)
  {
    return 1;
  }
}

public class Server extends Thread {
    private ServerSocket serverSocket;
    public  Server serverInstance;

    /**
     * Constructor to create Server
     */
    public Server() {
        try { serverSocket = new ServerSocket(2048); }
        catch (IOException e) { writeCatch(e, "Error creating server socket"); }
        System.out.println("TMA01Q2 server on port 2048");
        this.start(); // run
    }

    /**
     * Listen and respond to messages
     */
    public void run()
    {
      System.out.println("Server running");
      try
      {
        while(true)
        {
          System.out.println("Connections being accepted");
          Socket cSocket = serverSocket.accept (); // accept connection
          CSpawn c = new CSpawn(cSocket); // when connection recieved spawn new connection thread
        }
      }
      catch (IOException exp)
      {
        writeCatch(exp, "Error waiting for connection");
      }
    }

    public void isRunning()
    {
      System.out.println("is running");
    }

    /**
     * Start server
     */
    public static void main(String[] args)
    {
        new Server();
    }

    /**
     * Write out exception along with message for debugging
     */
    public static void writeCatch(Exception iExp, String iMsg)
    {
      System.out.println(iMsg + " => " +  iExp);
      Runtime.getRuntime().exit(666);
    }
}

/**
 * class handles socket threads made by client
 */
class CSpawn extends Thread
{
    private DataInputStream in;
    private PrintStream out;
    private Socket client;
    private Employee employeeTable;

    /**
     * Constructor to start connection
     */
    public CSpawn(Socket cSocket)
    {
        client = cSocket;
        try
        {
            in = new DataInputStream(client.getInputStream());
            out = new PrintStream(client.getOutputStream ());
        }
        catch (IOException exp)
        {
            try
            {
              client.close();
            }
            catch (IOException exp2)
            {
              // do nothing
            }
            return;
        }
        this.start(); // run
    }

    /**
     * Read and Analyse Input
     */
    public void run()
    {
        employeeTable = new Employee();
        System.out.println(employeeTable);
        employeeTable.getNoEmployees();

        String inputLine;
        try
        {
            while(true)
            {
                // read line from steam
                inputLine = in.readLine();
                out.println("10000");
            }
        }
        catch (IOException exp)
        {
          // ignore
        }
        finally
        {
          try
            {
              client.close();
            }
            catch (IOException exp)
            {
              //ignore
            }
        }
        // exceptions ignored because they are most likely caused by client closing
        // important exceptions are in server class
    }
}
LVL 1
jaycangelAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
>    Hashtable employeeTable = new Hashtable();

should be:

   employeeTable = new Hashtable();
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
objectsCommented:
The local var shadowes the member var
0
avinthmCommented:
Hi ,
>   public Employee()
>   {
>     Hashtable employeeTable = new Hashtable();
>     employeeTable.put("a", "10000");
>     employeeTable.put("b", "20000");
>     employeeTable.put("c", "30000");
>     System.out.println("Employee Table set up");
>     System.out.println(employeeTable.get("a"<wbr/>));
>   }


this creates a new Hashtable

change the first line to
employeeTable = new Hashtable();

cheers
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

avinthmCommented:
i m late :(
0
jaycangelAuthor Commented:
Damn you are good! I've been struggling for ages!!!! And it takes you like 15 second!

Maybe you could help me with this too.

The CSpawn class contains the instance of employee. Which means that everytime someone connects to the server an new employee instance is created along with the CSpawn (connection thread). How would I change the code so that there was only one employee instance located in the server class. I just don't know how the CSpawn class would refer back to the Server class that created it. Is there something like parent.employee ?!
0
objectsCommented:
create one in your Server class and pass  it as a parameter to the Cspawn constructor
0
jaycangelAuthor Commented:
Thanks, that works perfectly
0
objectsCommented:
no worries :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.