Connecting Tech Pros Worldwide Forums | Help | Site Map

error on drawString

Newbie
 
Join Date: Jan 2008
Posts: 2
#1: Jan 24 '08
I'm just learning how to program in java. I wrote my first java program, but got an error. Here is my program.


import java.awt.*;
import java.applet.*;
public class me extends Applet
{
public void init ()
{
String AuthorName;
AuthorName = "Steven Spielberg";
}
public void paint (Graphics x)
{
x.drawString (AuthorName,50,50);
}
}


------------------- END OF PROGRAM ------------------------------

When I compiled it with command "javac <file_name.java>" (ie: javac me.java), it got an error. Error showed:

% javac me.java
me.java:12: cannot find symbol
symbol : variable AuthorName
location: class me
x.drawString (AuthorName,50,50);
^
1 error

----------------------------------------------------------------------------------
Any help is appreciated. Thank you in advance.

-simon

BigDaddyLH's Avatar
Moderator
 
Join Date: Dec 2007
Location: Kelowna, BC Canada
Posts: 1,212
#2: Jan 24 '08

re: error on drawString


If this is your first program, I would say, slow down! Learn the basics before trying to write GUI programs, and avoid applets as long as possible, perhaps forever. Start here: http://java.sun.com/docs/books/tutor...ted/index.html
Newbie
 
Join Date: Jan 2008
Posts: 2
#3: Jan 25 '08

re: error on drawString


Quote:

Originally Posted by BigDaddyLH

If this is your first program, I would say, slow down! Learn the basics before trying to write GUI programs, and avoid applets as long as possible, perhaps forever. Start here: http://java.sun.com/docs/books/tutor...ted/index.html


Thank you. It's good advice. I had checked out the link, and it a good link.

-simon
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#4: Jan 25 '08

re: error on drawString


Quote:

Originally Posted by simon28317

Thank you. It's good advice. I had checked out the link, and it a good link.

-simon

Basically the scoping did you in there. You create something in one method and try to access it from another method. Better declare that String outside that init method.
NB. SmallDaddy's advice still holds though.
Newbie
 
Join Date: Jan 2008
Posts: 1
#5: Jan 25 '08

re: error on drawString


Hey

Do not declare the variable locally in the function. It is not being recognised datswhy
Familiar Sight
 
Join Date: Mar 2008
Posts: 174
#6: Jun 19 '08

re: error on drawString


The problem is you are telling it to draw the String AuthorName but it doesn't know what AuthorName is. You need to declare the variable AuthorName of the string type so that it will know what to print out.
Reply