Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Default frame.add( hello );

    Hello I'm really new to Java I just downloaded a book by O'Reilly Media called Learning Java. I'm on the first chapter and I'm already stuck

    Here's the code so far
    Java Code:
    package hellojava;
    
    import java.awt.Graphics;
    import javax.swing.*;
    
    
    public class Hellojava {
    
        
    public static void main(String[] args) {
       
            JFrame frame = new JFrame( "Hello, Java!");
            frame.add( hello );
            frame.setSize (300,300);
            frame.setVisible(true);
            
        }   
        
    
    class HelloComponent extends JComponent {
        
        
          HelloComponent hello = new HelloComponent();
        public void paintComponent(Graphics g){
            g.drawString("Hello, Java!", 125, 95);
        
            
        
        }
        
       
        
    }
        
      
        
        
    }
    I'm using NetBeans and it's saying "Cannot find symbol" on the line that has
    Java Code:
    frame.add( hello );
    on it

    When I try and run it I get an error message saying:

    run:
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>
    at hellojava.Hellojava.main(Hellojava.java:14)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 2 seconds)

    Can someone help I'm thinking there's a really simple solution to this but when I try and search for an answer it doesn't help because I don't understand what anybody's talking about!!

    Thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: frame.add( hello );

    First off, you're trying to run code that won't compile -- never do that, but instead fix the compilation errors first and then run the app. So you will need to compile your code first, then come back with your exact compiler's error messages.

  3. #3
    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Default Re: frame.add( hello );

    So if I compiled it right (I pressed f9) this is the error

    ant -f C:\\Users\\Danny\\Documents\\NetBeansProjects\\hel lojava -Djavac.includes=hellojava/Hellojava.java compile-single
    init:
    Deleting: C:\Users\Danny\Documents\NetBeansProjects\hellojav a\build\built-jar.properties
    deps-jar:
    Updating property file: C:\Users\Danny\Documents\NetBeansProjects\hellojav a\build\built-jar.properties
    Compiling 1 source file to C:\Users\Danny\Documents\NetBeansProjects\hellojav a\build\classes
    C:\Users\Danny\Documents\NetBeansProjects\hellojav a\src\hellojava\Hellojava.java:14: error: cannot find symbol
    frame.add( hello );
    symbol: variable hello
    location: class Hellojava
    1 error
    C:\Users\Danny\Documents\NetBeansProjects\hellojav a\nbproject\build-impl.xml:938: The following error occurred while executing this line:
    C:\Users\Danny\Documents\NetBeansProjects\hellojav a\nbproject\build-impl.xml:268: Compile failed; see the compiler error output for details.
    BUILD FAILED (total time: 0 seconds)

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: frame.add( hello );

    Quote Originally Posted by itsdannylalala View Post
    C:\Users\Danny\Documents\NetBeansProjects\hellojav a\src\hellojava\Hellojava.java:14: error: cannot find symbol
    frame.add( hello );
    symbol: variable hello
    Well, I can't find it either so it's no surprise that the compiler is complaining.

    What did you intend hello to be?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Default Re: frame.add( hello );

    It's meant to just say "Hello, Java!" in a grey box. The first example creates it using a JLabel and now it's saying "We're going to take over the job of the JLabel class and replace it with our own geographical class: HelloComponent"

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: frame.add( hello );

    You appear to be trying to use a variable that doesn't exist. The variable hello doesn't exist in the main method and so can't be used.

    You also appear to create this variable in the HelloComponent class itself, but this is local to the class and doesn't exist in the main method. Not only that, if your code did compile, you'd be creating HelloComponent objects inside of a HelloComponent object to infinity and you'd crash the Java Virtual Machine.

    Perhaps instead you want to declare and initialize the HelloComponent variable, hello, in the main method so you can use it there. Next, you'll want this class to extend some Swing component so that it can actually be placed inside of a JFrame.

    Most important, read, study, and re-study again since learning this stuff isn't easy and will take lots of effort, but you'll find eventually that the effort is worthwhile.

Similar Threads

  1. Replies: 7
    Last Post: 04-01-2012, 04:11 AM
  2. how can i do frame to frame?:(call):
    By papyon in forum Java Applets
    Replies: 3
    Last Post: 02-24-2012, 03:49 AM
  3. Replies: 4
    Last Post: 01-18-2011, 08:22 AM
  4. Java slave Frame access to its owner main frame problem
    By cagdaseckin in forum New To Java
    Replies: 0
    Last Post: 12-10-2010, 10:40 AM
  5. Replies: 7
    Last Post: 11-02-2009, 06:31 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •