error on drawString

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • simon28317
    New Member
    • Jan 2008
    • 2

    error on drawString

    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
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    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

    Comment

    • simon28317
      New Member
      • Jan 2008
      • 2

      #3
      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

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        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.

        Comment

        • tulika004
          New Member
          • Jan 2008
          • 1

          #5
          Hey

          Do not declare the variable locally in the function. It is not being recognised datswhy

          Comment

          • Kid Programmer
            New Member
            • Mar 2008
            • 176

            #6
            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.

            Comment

            Working...