i added code tags to your original post, which makes it much easier to read.

The first thing I see is that you have two public classes. Each public class much be saved in its own file, with the file name EXACTLY matching the class name, including case.

Once you split those apart, I think you'll have problems with your ChatRoom class. you declare a bunch of member variables, and then got straight to executable code. That is not allowed, as all code has to be in a method somewhere.

Do you know what we mean by a constructor? It is a special method that you have to define yourself. This line:

is CALLING a constructor, but it isn't one itself. If you don't write any constructors, java is kind enough to make a zero-argument one for you, which is what is being called here. However, if you just stuck this code in there somewhere, you still have code NOT IN A METHOD. Adding more code will still not not put your existing code in a method.

Here is a basic example:

(Note: I just whipped this up, it may not be 100% valid)

Here I have done several things. I have defined a member variable "intMember". Then, I have a main() method. I then have two constructors. The important thing about are:

1) They have the EXACT same name as the class itself
2) There is NO return type - not even "void".

One is a no-arg constructor. If you would call "new MyClass()", this is what would happen, and intMember would be set to 17. I ALSO created a second constructor that takes a single int parameter. If you call "new MyClass(12)" or "new MyClass(intVariable)", THAT code would run, setting intMember to the value passed in.

Another note: If I define any constructor at all, Java will not provide by default a no-arg constructor.

So, try writing your own constructor for your class, and put all that bad code inside it. Post it here when you're done (and any compilation errors), and we'll take a look.

Thank you. The code compiled and ran fine.
I tried to extend it further with actions.

It compiles but produces runtime errors:

> run ChatRoom
java.lang.NullPointerException at ChatRoom.<init>(ChatRoom.java:124)
at ChatRoom.main(ChatRoom.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:271)

Please tell me where I err.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 41593

31

posted Mar 18, 2011 09:38:30

0

I would disagree very severely with anybody who uses addActionListener(this) as a routine.

I can't find where your MullPointerException occurred, since line 124 is blank!

fred rosenberger wrote:Do you know what we mean by a constructor? It is a special method that you have to define yourself. This line:

is CALLING a constructor, but it isn't one itself. If you don't write any constructors, java is kind enough to make a zero-argument one for you, which is what is being called here. However, if you just stuck this code in there somewhere, you still have code NOT IN A METHOD. Adding more code will still not not put your existing code in a method.

Here is a basic example:

(Note: I just whipped this up, it may not be 100% valid)

Here I have done several things. I have defined a member variable "intMember". Then, I have a main() method. I then have two constructors. The important thing about are:

1) They have the EXACT same name as the class itself
2) There is NO return type - not even "void".

One is a no-arg constructor. If you would call "new MyClass()", this is what would happen, and intMember would be set to 17. I ALSO created a second constructor that takes a single int parameter. If you call "new MyClass(12)" or "new MyClass(intVariable)", THAT code would run, setting intMember to the value passed in.

Another note: If I define any constructor at all, Java will not provide by default a no-arg constructor.

So, try writing your own constructor for your class, and put all that bad code inside it. Post it here when you're done (and any compilation errors), and we'll take a look.

Much obliged. I wrapped my initial code below public private into "public ChatRoom() {}" and it worked as expected.

fred rosenberger wrote:you declare the variable sendButton, but you never create the object it refers to. Therefore, you cannot call a method on the object (because it doesn't exist!!!).

The code ran after I commented out line 123 with sendButton(this).

It appears from my lab instructions that constructor creates the object in question automatically:

"you can add the following line to the constructor (some time after you have created the button object):

sendButton.addActionListener(this);

In this command, "this" refers to the object that the constructor is creating, that is to the ChatRoom object itself.
You can add similar lines to set up event-handling for messageInput, sendPrivateButton, and privateMessageInput.
If you've done all this, you should be able to run the program, type something in the message input box,
click the "Send" button, and see the text that you typed in the transcript."

This this is not working.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 41593

31

posted Mar 18, 2011 10:45:29

0

I never said it was deprecated. I said I disagree with it. I think it isn't object-oriented. See this thread, and the older thread it refers to, and this thread. There are bound to be others if you search.

Campbell Ritchie wrote:I never said it was deprecated. I said I disagree with it. I think it isn't object-oriented. See this thread, and the older thread it refers to, and this thread. There are bound to be others if you search.

to express disapproval of; to recommend against use of; to pray against
en.wiktionary.org/wiki/deprecate

I meant you expressed disapproval.
Why does "sendButton.addActionListener(this); " prevent the code from running?
I understand there are better ways to make buttons work, but...

Why does "sendButton.addActionListener(this); " prevent the code from running?

Impossible to say without seeing your code in it's current state

If you post your code again, please take out those huge comment blocks. They just make it harder to read the important stuff.

Now...does it not RUN, or does it not COMPILE? There is a huge difference. If you are getting the nullpointerexception, see my post above. it appears you may be trying to call a method on an object you never created.

Why does "sendButton.addActionListener(this); " prevent the code from running?

Impossible to say without seeing your code in it's current state

If you post your code again, please take out those huge comment blocks. They just make it harder to read the important stuff.

Now...does it not RUN, or does it not COMPILE? There is a huge difference. If you are getting the nullpointerexception, see my post above. it appears you may be trying to call a method on an object you never created.

Thank you.
The code is the same as above.
Here it is with the comments removed

It compiles since I created a constructor as you suggested.
It does not run and produces runtime errors
at ChatRoom.<init>(ChatRoom.java:124)
at ChatRoom.main(ChatRoom.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:271)
when
sendButton.addActionListener(this); is in place.
That is the line #124 in the runtime errors,
line #48 is ChatRoom room = new ChatRoom();
When I comment out //sendButton.addActionListener(this);
the code compiles and runs.

Edit: My instructions were these:

"As a final step in setting up event-handling,
you have to "wire" the object that produces the event to the object that will handle the event.
This is part of setting up the GUI and should be done in the constructor.
To make sure that action events from the sendButton get to the ChatRoom,
you can add the following line to the constructor (some time after you have created the button object):

sendButton.addActionListener(this);

In this command, "this" refers to the object that the constructor is creating, that is to the ChatRoom object itself.
You can add similar lines to set up event-handling for messageInput, sendPrivateButton, and privateMessageInput.
If you've done all this, you should be able to run the program, type something in the message input box,
click the "Send" button, and see the text that you typed in the transcript. "

I thought I complied with the bolded instruction, perhaps I did not and that is the issue. Am I missing a button?