This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Beginning GUI programming exercise.  Please help to figure out why it does not compile. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Beginning GUI programming exercise.  Please help to figure out why it does not compile." Watch "Beginning GUI programming exercise.  Please help to figure out why it does not compile." New topic
Author

Beginning GUI programming exercise. Please help to figure out why it does not compile.

Igor Mechnikov
Ranch Hand

Joined: Feb 13, 2011
Posts: 100

I suspect the classes are incorrect.
Thank you in advance.






String knock = "\u042F \u0418\u0433\u043e\u0440\u044c";
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

What is the error message the compiler gives you? And please cut-and-paste it, don't just summarize it in a few words...


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Igor Mechnikov
Ranch Hand

Joined: Feb 13, 2011
Posts: 100

38 errors found:
File: /home/whoami/javaClass/Chatter.java [line: 22]
Error: /home/whoami/javaClass/Chatter.java:22: invalid method declaration; return type required
File: /home/whoami/javaClass/Chatter.java [line: 22]
Error: /home/whoami/javaClass/Chatter.java:22: illegal start of type
File: /home/whoami/javaClass/Chatter.java [line: 22]
Error: /home/whoami/javaClass/Chatter.java:22: ')' expected
File: /home/whoami/javaClass/Chatter.java [line: 22]
Error: /home/whoami/javaClass/Chatter.java:22: ';' expected
File: /home/whoami/javaClass/Chatter.java [line: 22]
Error: /home/whoami/javaClass/Chatter.java:22: illegal start of type
File: /home/whoami/javaClass/Chatter.java [line: 22]
Error: /home/whoami/javaClass/Chatter.java:22: <identifier> expected
File: /home/whoami/javaClass/Chatter.java [line: 22]
Error: /home/whoami/javaClass/Chatter.java:22: ';' expected
File: /home/whoami/javaClass/Chatter.java [line: 22]
Error: /home/whoami/javaClass/Chatter.java:22: illegal start of type
File: /home/whoami/javaClass/Chatter.java [line: 22]
Error: /home/whoami/javaClass/Chatter.java:22: <identifier> expected
File: /home/whoami/javaClass/Chatter.java [line: 23]
Error: /home/whoami/javaClass/Chatter.java:23: invalid method declaration; return type required
File: /home/whoami/javaClass/Chatter.java [line: 23]
Error: /home/whoami/javaClass/Chatter.java:23: <identifier> expected
File: /home/whoami/javaClass/Chatter.java [line: 25]
Error: /home/whoami/javaClass/Chatter.java:25: <identifier> expected
File: /home/whoami/javaClass/Chatter.java [line: 26]
Error: /home/whoami/javaClass/Chatter.java:26: <identifier> expected
File: /home/whoami/javaClass/Chatter.java [line: 26]
Error: /home/whoami/javaClass/Chatter.java:26: <identifier> expected
File: /home/whoami/javaClass/Chatter.java [line: 27]
Error: /home/whoami/javaClass/Chatter.java:27: <identifier> expected
File: /home/whoami/javaClass/Chatter.java [line: 28]
Error: /home/whoami/javaClass/Chatter.java:28: <identifier> expected
File: /home/whoami/javaClass/Chatter.java [line: 28]
Error: /home/whoami/javaClass/Chatter.java:28: <identifier> expected
File: /home/whoami/javaClass/Chatter.java [line: 29]
Error: /home/whoami/javaClass/Chatter.java:29: <identifier> expected
File: /home/whoami/javaClass/Chatter.java [line: 30]
Error: /home/whoami/javaClass/Chatter.java:30: <identifier> expected
File: /home/whoami/javaClass/Chatter.java [line: 30]
Error: /home/whoami/javaClass/Chatter.java:30: <identifier> expected
File: /home/whoami/javaClass/Chatter.java [line: 31]
Error: /home/whoami/javaClass/Chatter.java:31: invalid method declaration; return type required
File: /home/whoami/javaClass/Chatter.java [line: 31]
Error: /home/whoami/javaClass/Chatter.java:31: <identifier> expected
File: /home/whoami/javaClass/Chatter.java [line: 31]
Error: /home/whoami/javaClass/Chatter.java:31: <identifier> expected
File: /home/whoami/javaClass/Chatter.java [line: 32]
Error: /home/whoami/javaClass/Chatter.java:32: <identifier> expected
File: /home/whoami/javaClass/Chatter.java [line: 33]
Error: /home/whoami/javaClass/Chatter.java:33: <identifier> expected
File: /home/whoami/javaClass/Chatter.java [line: 33]
Error: /home/whoami/javaClass/Chatter.java:33: illegal start of type
File: /home/whoami/javaClass/Chatter.java [line: 34]
Error: /home/whoami/javaClass/Chatter.java:34: <identifier> expected
File: /home/whoami/javaClass/Chatter.java [line: 34]
Error: /home/whoami/javaClass/Chatter.java:34: illegal start of type
File: /home/whoami/javaClass/Chatter.java [line: 35]
Error: /home/whoami/javaClass/Chatter.java:35: <identifier> expected
File: /home/whoami/javaClass/Chatter.java [line: 35]
Error: /home/whoami/javaClass/Chatter.java:35: illegal start of type
File: /home/whoami/javaClass/Chatter.java [line: 36]
Error: /home/whoami/javaClass/Chatter.java:36: invalid method declaration; return type required
File: /home/whoami/javaClass/Chatter.java [line: 36]
Error: /home/whoami/javaClass/Chatter.java:36: illegal start of type
File: /home/whoami/javaClass/Chatter.java [line: 36]
Error: /home/whoami/javaClass/Chatter.java:36: ')' expected
File: /home/whoami/javaClass/Chatter.java [line: 36]
Error: /home/whoami/javaClass/Chatter.java:36: ';' expected
File: /home/whoami/javaClass/Chatter.java [line: 36]
Error: /home/whoami/javaClass/Chatter.java:36: <identifier> expected
File: /home/whoami/javaClass/Chatter.java [line: 36]
Error: /home/whoami/javaClass/Chatter.java:36: illegal start of type
File: /home/whoami/javaClass/Chatter.java [line: 36]
Error: /home/whoami/javaClass/Chatter.java:36: ';' expected
File: /home/whoami/javaClass/Chatter.java [line: 36]
Error: /home/whoami/javaClass/Chatter.java:36: <identifier> expected

and the offending lines are:

setLayout( new BorderLayout(3,3) );
setBackground(Color.DARK_GRAY);
JPanel top = new JPanel(); // subpanel for top of main panel
privateMessageInput = new JTextField(35); // space for 35 chars
top.add(privateMessageInput);
sendPrivateButton = new JButton("Send To:");
top.add(sendPrivateButton);
recipientInput = new JTextField(8); // space for 8 characters
top.add(recipientInput);
add(top,BorderLayout.NORTH); // add subpanel to main panel
transcript = new JTextArea(30,60); // 30 lines, 60 columns
transcript.setWrapStyleWord(true);
transcript.setLineWrap(true);
transcript.setEditable(false); // user can't edit transcript
add(new JScrollPane(transcript), BorderLayout.CENTER);
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

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.

Igor Mechnikov
Ranch Hand

Joined: Feb 13, 2011
Posts: 100

many thanks.

regarding multiple public classes,
does




make more sense? I am including the main subroutine within ChatRoom for the sake of convenience.

Still a ton of errors, must have to do with the lack of methods mentioned:

38 errors found:
File: /home/whoami/javaClass/Chatter.java [line: 21]
Error: /home/whoami/javaClass/Chatter.java:21: <identifier> expected
File: /home/whoami/javaClass/Chatter.java [line: 21]
Error: /home/whoami/javaClass/Chatter.java:21: illegal start of type
File: /home/whoami/javaClass/Chatter.java [line: 21]
Error: /home/whoami/javaClass/Chatter.java:21: ')' expected
File: /home/whoami/javaClass/Chatter.java [line: 21]
Error: /home/whoami/javaClass/Chatter.java:21: ';' expected
File: /home/whoami/javaClass/Chatter.java [line: 21]
Error: /home/whoami/javaClass/Chatter.java:21: illegal start of type
File: /home/whoami/javaClass/Chatter.java [line: 21]
Error: /home/whoami/javaClass/Chatter.java:21: <identifier> expected
File: /home/whoami/javaClass/Chatter.java [line: 21]
Error: /home/whoami/javaClass/Chatter.java:21: ';' expected
File: /home/whoami/javaClass/Chatter.java [line: 21]
Error: /home/whoami/javaClass/Chatter.java:21: illegal start of type
File: /home/whoami/javaClass/Chatter.java [line: 21]
Error: /home/whoami/javaClass/Chatter.java:21: <identifier> expected
File: /home/whoami/javaClass/Chatter.java [line: 22]
Error: /home/whoami/javaClass/Chatter.java:22: <identifier> expected
File: /home/whoami/javaClass/Chatter.java [line: 22]
Error: /home/whoami/javaClass/Chatter.java:22: <identifier> expected
File: /home/whoami/javaClass/Chatter.java [line: 24]
Error: /home/whoami/javaClass/Chatter.java:24: <identifier> expected
File: /home/whoami/javaClass/Chatter.java [line: 25]
Error: /home/whoami/javaClass/Chatter.java:25: <identifier> expected
File: /home/whoami/javaClass/Chatter.java [line: 25]
Error: /home/whoami/javaClass/Chatter.java:25: <identifier> expected
File: /home/whoami/javaClass/Chatter.java [line: 26]
Error: /home/whoami/javaClass/Chatter.java:26: <identifier> expected
File: /home/whoami/javaClass/Chatter.java [line: 27]
Error: /home/whoami/javaClass/Chatter.java:27: <identifier> expected
File: /home/whoami/javaClass/Chatter.java [line: 27]
Error: /home/whoami/javaClass/Chatter.java:27: <identifier> expected
File: /home/whoami/javaClass/Chatter.java [line: 28]
Error: /home/whoami/javaClass/Chatter.java:28: <identifier> expected
File: /home/whoami/javaClass/Chatter.java [line: 29]
Error: /home/whoami/javaClass/Chatter.java:29: <identifier> expected
File: /home/whoami/javaClass/Chatter.java [line: 29]
Error: /home/whoami/javaClass/Chatter.java:29: <identifier> expected
File: /home/whoami/javaClass/Chatter.java [line: 30]
Error: /home/whoami/javaClass/Chatter.java:30: invalid method declaration; return type required
File: /home/whoami/javaClass/Chatter.java [line: 30]
Error: /home/whoami/javaClass/Chatter.java:30: <identifier> expected
File: /home/whoami/javaClass/Chatter.java [line: 30]
Error: /home/whoami/javaClass/Chatter.java:30: <identifier> expected
File: /home/whoami/javaClass/Chatter.java [line: 31]
Error: /home/whoami/javaClass/Chatter.java:31: <identifier> expected
File: /home/whoami/javaClass/Chatter.java [line: 32]
Error: /home/whoami/javaClass/Chatter.java:32: <identifier> expected
File: /home/whoami/javaClass/Chatter.java [line: 32]
Error: /home/whoami/javaClass/Chatter.java:32: illegal start of type
File: /home/whoami/javaClass/Chatter.java [line: 33]
Error: /home/whoami/javaClass/Chatter.java:33: <identifier> expected
File: /home/whoami/javaClass/Chatter.java [line: 33]
Error: /home/whoami/javaClass/Chatter.java:33: illegal start of type
File: /home/whoami/javaClass/Chatter.java [line: 34]
Error: /home/whoami/javaClass/Chatter.java:34: <identifier> expected
File: /home/whoami/javaClass/Chatter.java [line: 34]
Error: /home/whoami/javaClass/Chatter.java:34: illegal start of type
File: /home/whoami/javaClass/Chatter.java [line: 35]
Error: /home/whoami/javaClass/Chatter.java:35: invalid method declaration; return type required
File: /home/whoami/javaClass/Chatter.java [line: 35]
Error: /home/whoami/javaClass/Chatter.java:35: illegal start of type
File: /home/whoami/javaClass/Chatter.java [line: 35]
Error: /home/whoami/javaClass/Chatter.java:35: ')' expected
File: /home/whoami/javaClass/Chatter.java [line: 35]
Error: /home/whoami/javaClass/Chatter.java:35: ';' expected
File: /home/whoami/javaClass/Chatter.java [line: 35]
Error: /home/whoami/javaClass/Chatter.java:35: <identifier> expected
File: /home/whoami/javaClass/Chatter.java [line: 35]
Error: /home/whoami/javaClass/Chatter.java:35: illegal start of type
File: /home/whoami/javaClass/Chatter.java [line: 35]
Error: /home/whoami/javaClass/Chatter.java:35: ';' expected
File: /home/whoami/javaClass/Chatter.java [line: 35]
Error: /home/whoami/javaClass/Chatter.java:35: <identifier> expected

Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 557
Please, please, please post your code in code tags. If you don't know how, select the "Code" button from the items above the edit window and post your code between the resulting tags. Then, if not properly indented, fix it, or paste code that is.


Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37923
    
  22
It's not a subroutine, it's a method.

Fred has already told you that all that executable code must be inside a method. Or a constructor. That code can easily be put into the constructor for that class.
Igor Mechnikov
Ranch Hand

Joined: Feb 13, 2011
Posts: 100

Campbell Ritchie wrote:That code can easily be put into the constructor for that class.


Maybe not at first. This works. Thank you much.

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

Here is your code, with some annotations:

Igor Mechnikov
Ranch Hand

Joined: Feb 13, 2011
Posts: 100

fred rosenberger wrote:Here is your code, with some annotations:



Thank you very much!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

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.
Igor Mechnikov
Ranch Hand

Joined: Feb 13, 2011
Posts: 100

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: 37923
    
  22
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!
Igor Mechnikov
Ranch Hand

Joined: Feb 13, 2011
Posts: 100

Campbell Ritchie wrote: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!


Thank you.

I did not even realize some of these errors referred to the code itself.
The numberings are off by -1. The line 124 error refers to line 123 as presented.

And, why is "addActionListener(this)" deprecated?
Igor Mechnikov
Ranch Hand

Joined: Feb 13, 2011
Posts: 100

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
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

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!!!).
Igor Mechnikov
Ranch Hand

Joined: Feb 13, 2011
Posts: 100

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: 37923
    
  22
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.
Igor Mechnikov
Ranch Hand

Joined: Feb 13, 2011
Posts: 100

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...
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

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.
Igor Mechnikov
Ranch Hand

Joined: Feb 13, 2011
Posts: 100

fred rosenberger wrote:
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?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

The line

sendButton.addActionListener(this);

is throwing a NPE because your sendButton variable has never been set to anything. At no point in your code do you have a

sendButton = <something>

Variable in java are not the actual object, but a reference to them. They are like address cards. when you say


you are creating an address card with the name 'sendButton', but not writing anybody's address on it. you then in this line:

say "Go to the house at this address and ring the doorbell" - however, since there is no address on the card, the JVM doesn't know where to go, so it can't ring the doorbell.

You'll notice how for most other member variables, you set it to something:



what's missing here? I would guess you could also get a NPE if the conditions for this were true:

Igor Mechnikov
Ranch Hand

Joined: Feb 13, 2011
Posts: 100

Fantastic.

I inserted

sendButton = new JButton();

before

sendButton.addActionListener(this);

and it ran Ok.

Thank you so much!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Beginning GUI programming exercise. Please help to figure out why it does not compile.
 
Similar Threads
JList
Pass data back to Window 1 from Window 2
Swing GUI working not properly
Need Help resizing my JScrollPane
Can a container have multiple layout managers?