• Post Reply Bookmark Topic Watch Topic
  • New Topic

issues in sample code for graphical elemets

 
Olga Matveeva
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I tried the sample code from a textbook dated 2005 year while learning Java, but it gives me errors which I do not understand how to solve. Would really appreciate your help.



Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Syntax error on token "JFrame", import expected
Syntax error on token "JButton", import expected
b cannot be resolved
b cannot be resolved
b cannot be resolved

at BListener.<init>(BListener.java:5)
at InnerButton.go(InnerButton.java:21)
at InnerButton.main(InnerButton.java:12)
 
Campbell Ritchie
Marshal
Posts: 52519
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't try running code with red marks on it. Go back to Eclipse or whichever IDE you have and look for the compiler errors, shown by red triangles on the left of the code. If you hover the mouse on the, you will get details of the error and even how to correct it.
Your code is incorrectly indented. Try ctrl‑A→ctrl‑I on Eclipse to get the indentation sorted out, or ctrl‑A→ctrl‑F. Is the listener class supposed to be a top‑level class or an inner class? I suspect it should be an inner class, but you have made it a top‑level class.

Which book are you using? Does it cover Java5?
 
ab singh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the main problem in your code was, declaration of
JFrame frame;
JButton b;
outside class BListener.
please use this code it worked for me





But there will be a problem with text changing in button because Button object have not been created in Blistener class
so if you click button it will give null pointer exception, because of no object relevant to the action.


Rather than this you can use


 
Campbell Ritchie
Marshal
Posts: 52519
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I added code tags to your post. Always use the tags: doesn't it look better L)
Unfortunately it highlights your incorrect indentation which makes the code difficult to read. The many empty lines you had also don't help either; I removed most of them.
I still think the real error is that the original poster has miscopied the code with too many } in the middle and too few } at the end, converting the inner class to a top‑level class.
Your first example with two classes will probably compile and run but you will suffer a null pointer exception whenever you click the button.
 
Olga Matveeva
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go back to Eclipse or whichever IDE you have and look for the compiler errors, shown by red triangles on the left of the code. If you hover the mouse on the, you will get details of the error and even how to correct it.


This was exactly the thing. Error was saying that b cannot be resolved and I couldn´t understand what did that mean.

Is the listener class supposed to be a top‑level class or an inner class? I suspect it should be an inner class, but you have made it a top‑level class.


Yes, you were right. I mistakenly created listener as a separate class instead of creating it inside the InnerButton class. I fixed that and it solved the problem. Thanks a lot

Campbell Ritchie wrote:Which book are you using? Does it cover Java5?


I am using Heads first Java, 2nd edition.
 
Campbell Ritchie
Marshal
Posts: 52519
119
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, HFJ does cover Java5. Which page?

If you have the listener as an inner class, it automatically has access to the fields of its surrounding class, including b. Things may be different if the keyword static is used anywhere.
If you put too many } in the middle, you convert the listener to a top‑level class, which does not contain the field b and has no access to it. There are ways to give it access, e.g. by passing b to the listener's constructor.
 
Olga Matveeva
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Yes, HFJ does cover Java5. Which page?


395, Be the compiler exercise.

How could I pass b without making listener an inner class?
 
Campbell Ritchie
Marshal
Posts: 52519
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make it a field of the listener class and pass a reference to b to its constructor.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!