Weird reference issue

I am having a weird issue with an assignment. I instantiate an object inside a constructor and when I try to use the object outside the constructor, the object is null, even though it is not within the constructor. Nowhere do I set the object to null. The odd thing is if I pass it to another reference in the constructor, I can use the new reference.

Here is the offending code and some test lines:

in main:

yak = new Chat(sock, sendSock,listSock, connections, temp, pass, errorFile);

yak.connectClients();

in Chat:

public class Chat {

private ServerSocket sock;
private DatagramSocket sendSock;
private DatagramSocket listSock;
private LinkedList userList;
private ChatErrors errorLog;
private ChatErrors temp;


private final String USERLIST;
private final String REQUIREPASS;
private final int MAXCONNECTS;
private final String SENDNAME;
private final String SENDPASSWORD;
private final String READYCHAT;
private final String NAMETAKEN;
private final String ROOMFULL;
private final String IOERROR;

private int connects;

Chat(ServerSocket sock, DatagramSocket sendSock, DatagramSocket listSock, int connections, String fileName, String pass, String errorFile)
{

ChatErrors temp = new ChatErrors(errorFile);

userList = new LinkedList();
this.sock = sock;
this.sendSock = sendSock;
this.listSock = listSock;
MAXCONNECTS = connections;
REQUIREPASS = pass;
USERLIST = fileName;
SENDNAME = "10";
SENDPASSWORD = "11";
READYCHAT = "20";
NAMETAKEN = "2";
ROOMFULL = "5";
IOERROR = "1";

connects = 0;
errorLog = temp;
}



public void connectClients()
{
Socket recv=null, send, list;
boolean error = false, ready = false;

//testing statements
System.out.println(temp);
System.out.println(errorLog);
....
....
}

In ChatErrors:

public class ChatErrors
{
private final String errorFile;



ChatErrors(String errorFile)
{
this.errorFile = errorFile;

}

public void logError(String error)
{
String timeStamp;
System.out.println(error);
try
{
PrintWriter fout = new PrintWriter (new BufferedWriter (new FileWriter (("errors.cht"),true)));
timeStamp = DateFormat.getDateInstance().format(new Date());
//System.out.println(timeStamp);

error = timeStamp + " " + error;
//System.out.println(error);
fout.println(error);
fout.close();

}
catch(IOException e)
{
System.out.println("error.cht could not be opened.");
}


}


public String toString()
{
return errorFile;
}

It prints out:

null
errors.txt

It is acting like I dropped a reference, but I can't see it. Any help will be greatly appreciated.

Comments

  • [code]
    : Chat(ServerSocket sock, DatagramSocket sendSock, DatagramSocket listSock, int connections, String fileName, String pass, String errorFile)
    : {
    :
    : ChatErrors temp = new ChatErrors(errorFile);
    :
    [/code]

    You never assign a value to the member variable "temp"; in the constructor you declare a 2nd variable with the same name, but the scope of this variable is only for the method. Do this instead:
    [code]
    this.temp = new ChatErrors(errorFile);
    [/code]

    ---------------------------------
    [size=1]HOWTO ask questions: http://catb.org/~esr/faqs/smart-questions.html[/size]

  • : [code]
    : : Chat(ServerSocket sock, DatagramSocket sendSock, DatagramSocket listSock, int connections, String fileName, String pass, String errorFile)
    : : {
    : :
    : : ChatErrors temp = new ChatErrors(errorFile);
    : :
    : [/code]
    :
    : You never assign a value to the member variable "temp"; in the constructor you declare a 2nd variable with the same name, but the scope of this variable is only for the method. Do this instead:
    : [code]
    : this.temp = new ChatErrors(errorFile);
    : [/code]
    :
    : ---------------------------------
    : [size=1]HOWTO ask questions: http://catb.org/~esr/faqs/smart-questions.html[/size]
    :
    :
    ACK! duh!!!

    I knew it was something stupidly obvious. Thank you very much! :)
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories