Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 23 of 23

Thread: Illegal start of expression

  1. #1
    Member
    Join Date
    Mar 2012
    Posts
    37
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Illegal start of expression

    import javax.swing.*;
    class Window extends JFrame
    {
    public static void main ( String[] args)
    {
    JPanel pnl = new JPanel() ;
    public Window()
    {
    super( "Swing Window" ) ;
    setSize( 500, 200 ) ;
    setDefaultCloseOperation( EXIT_ON_CLOSE ) ;
    add( pnl ) ;
    setVisible( true ) ;
    }
    Window gui = new Window() ;
    }
    }

    According to Bluej, the public Window() is in correct, with the specific error being "illegal start of expression". Any ideas where I went wrong?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Illegal start of expression

    error being "illegal start of expression"
    Please copy full text of error message and paste it here.
    The message should show the source with a ^ under the location of the error.
    Here is a sample from the javac compiler:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    BTW Window is the name of a java SE class. You should use a unique name for your class.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Mar 2012
    Posts
    37
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Illegal start of expression

    import javax.swing.*;
    class Window extends JFrame
    {
        public static void main ( String[] args)
        {
            JPanel pnl = new JPanel() ;
            public Window()
            {
                super( "Swing Window" ) ;
                setSize( 500, 200 ) ;
                setDefaultCloseOperation( EXIT_ON_CLOSE ) ;
                add( pnl ) ;
                setVisible( true ) ;
            }
            Window gui = new Window() ;
        }
    }

    The exact error I get is "illegal start of expression" with public Window() being highlighted. Thanks in advance for the help.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Illegal start of expression

    Window is the name of a java SE class. You should use a unique name for your class.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Mar 2012
    Posts
    37
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Illegal start of expression

    I tried changing the class name, but it didn't seem to make a difference.

    I tried compiling it through the command prompt, and got the following results:

    Window.java:7: error: ';' expected
    public Window()
    ^
    2 errors


    So I added the ';' and got the following error back.

    Window.java:7: error illegal start of expression
    public Window();
    ^
    1 error

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Illegal start of expression

    Methods can not be defined inside of other methods. Make sure the closing } for a method's definition is present before starting the definition a new method.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Mar 2012
    Posts
    37
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Illegal start of expression

    the book I'm using reads as follows:

    1) Start a new program that imports all Swing components

    import javax.swing.* ;

    2)Create a subclass of the JFrame class named "Window" containing the standard main method

    class Window extends JFrame
    {
               public static void main ( String[] args ) {}
    }

    3) before the main method, create a JPanel container object

    JPanel pnl = new JPanel();

    4) Next insert this constructor method to specify window requirements and to add the JPanel object to the JFrame

    public Window()
    {
                super( "Swing Window" ) ;
                setSize( 500, 200 ) ;
                setDefaultCloseOperation( EXIT_ON_CLOSE ) ;
                add( pnl ) ;
                setVisible( true ) ;
            }

    5) Create an instance of the Window class by inserting this line into the main method

    Window gui = new Window();

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Illegal start of expression

    Did you look at the instructions closely and follow them exactly? {s and }s are easy to miss

    The start of the statements that define the constructors and methods in a class should all be in the same column.
    class Constr {
       public Constr() {
       ...
       }  //  end Constr()
     
       private void aMethod() {
       ...
       }  //  end aMethod()
     
       public static void main(String[] args) {
       ...
       }  // end main()
    }  // end class Constr
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Mar 2012
    Posts
    37
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Illegal start of expression

    Sorry Norm, but I'm having allot of trouble with this.....I gather my original code is arranged wrong due to the placement of my {s? How should they be arranged?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Illegal start of expression

    Post#8 is an example.
    Methods can not be defined inside of other methods. Make sure the closing } for a method's definition is present before starting the definition a new method.

    Start with each method's definition, find its first { and then find its ending }.

    It helps if you add a comment on the ending }. see post#8
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Mar 2012
    Posts
    37
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Illegal start of expression

    Quote Originally Posted by Tedstriker View Post
    the book I'm using reads as follows:

    1) Start a new program that imports all Swing components

    import javax.swing.* ;

    2)Create a subclass of the JFrame class named "Window" containing the standard main method

    class Window extends JFrame
    {
               public static void main ( String[] args ) {}
    }

    3) before the main method, create a JPanel container object

    JPanel pnl = new JPanel();

    4) Next insert this constructor method to specify window requirements and to add the JPanel object to the JFrame

    public Window()
    {
                super( "Swing Window" ) ;
                setSize( 500, 200 ) ;
                setDefaultCloseOperation( EXIT_ON_CLOSE ) ;
                add( pnl ) ;
                setVisible( true ) ;
            }

    5) Create an instance of the Window class by inserting this line into the main method

    Window gui = new Window();
    So I've been messing around with this. While its still wrong, at least I got a different error (class, interface, or enum expected with the ^ under the word void) with this effort:

    import javax.swing.*;
    class Note extends JFrame
    {
          JPanel pnl = new JPanel() ;
        }
        public static void main ( String[] args)
        {
     
     
            public Window() 
            {
                super( "Swing Window" ) ;
                setSize( 500, 200 ) ;
                setDefaultCloseOperation( EXIT_ON_CLOSE ) ;
                add( pnl ) ;
                setVisible( true ) ;
            }
            Window gui = new Window() ;
        }

    am I getting warmer?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Illegal start of expression

    When trying to fix your problem, did you try this:
    It helps if you add a comment on the method's ending } and on a class's ending }.

    See the code in post#8 Besides the labels on the } note how they are positioned beneath the start of the statement with the pairing {

    The order of {s and }s should be like this:
    { for the class
    { for a method
    } end of a method
    { for method
    } end of method
    } end of class
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Mar 2012
    Posts
    37
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Illegal start of expression

    Thanks Norm, but reading that just doesn't make sense to me. When it says JPanel pnl = newJPanel(); goes before the main method, where would that be? where would the different methods begin and end. Sorry, but I'm having trouble with this one.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Illegal start of expression

    Put a comment on the { for the start of the class definition
    and a comment on the } at the end of the class definition

    Put a comment on the { for the start of a method definition
    and a comment on the } at the end of that method definition

    See post#8

    where would the different methods begin and end.
    This is the beginning of a method definition:
      public void aMethod() {

    If you can not find the ending }, delete all the statements and start over.
    TYpe in the beginning of a method as shown above and on the next line add the } ending:
       public void aMethod() {
     
       } // end aMethod()
    Then go back and type in the statements that go in the method between the { and the }


    Make sure the }s are inline as shown in post#8


    Next loop at the where the { and } are positioned as described in post#12
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Mar 2012
    Posts
    37
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Illegal start of expression

    import javax.swing.*;
    class Note extends JFrame
    //beginning of class
    {       
        public static void main ( String[] args) 
        //beginning of main method
        {
           JPanel pnl = new JPanel() ;
            Window gui = new Window() ;
            //end of main method
        }
            public Window()
            //beginning of next method
            {
                super( "Swing Window" ) ;
                setSize( 500, 200 ) ;
                setDefaultCloseOperation( EXIT_ON_CLOSE ) ;
                add( pnl ) ;
                setVisible( true ) ;
                //end of next method
            }
     
     
        //end of the class
    }

    added some notes

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Illegal start of expression

    The comments should be on the lines with the } so they can not be separated from them.

    Does the code compile without errors now?

    The Window method (its really the class constructor) is indented too far. It should be indented the same amount as the main() method so that it does not look like it is inside the main() method.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Member
    Join Date
    Mar 2012
    Posts
    37
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Illegal start of expression

    Now I get this error:
    public Window()
    ^

    invalid method declaration; return type required

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Illegal start of expression

    You changed the name of the class from Window to Note, so that statement is no longer a valid constructor.
    Change the name (Window) to match the class name (Note).

    See the tutorial:
    Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Creating Objects (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Member
    Join Date
    Mar 2012
    Posts
    37
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Illegal start of expression

    Ok, corrected that...looks like its getting better. However, I now get the following error:

    add ( pnl ) ;
    ^

    cannot find symbol - variable pnl

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Illegal start of expression

    The compiler can not find a definition of the variable: pnl that is defined in scope (within the same pair of {}) with where it is being used/referenced.

    See 3) in post#7. Especially the before part.
    If you don't understand my answer, don't ignore it, ask a question.

  21. The Following User Says Thank You to Norm For This Useful Post:

    Tedstriker (April 12th, 2013)

  22. #21
    Member
    Join Date
    Mar 2012
    Posts
    37
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Illegal start of expression

    import javax.swing.*;
    class Window extends JFrame
    //beginning of class
    {     
        JPanel pnl = new JPanel() ;
        public static void main ( String[] args) 
        //beginning of main method
        {
     
           Window gui = new Window() ;
            //end of main method
        }
        public Window()
            //beginning of next method
        {
           super( "Swing Window" ) ;
           setSize( 500, 200 ) ;
           setDefaultCloseOperation( EXIT_ON_CLOSE ) ;
           add( pnl ) ;
           setVisible( true ) ;
                //end of next method
        }
     
     
        //end of the class
    }

    This worked!! I created my little window! Thanks Norm, I appreciate the help. Till next time.....

  23. #22
    Member
    Join Date
    Mar 2012
    Posts
    37
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Illegal start of expression

    Thought I would post in this thread instead of creating a new one, as my question is related. The next part of the book I'm working with is to add buttons to the window created. The following is my code:

    import javax.swing.*;
    class Buttons extends JFrame
    //beginning of class
    {     
     
        JPanel pnl = new JPanel() ;
        public static void main ( String[] args) 
        //beginning of main method
        {
     
           Buttons gui = new Buttons() ;
     
            //end of main method
        }
        ImageIcon tick = new ImageIcon( "tick.png" );
        ImageIcon cross = new ImageIcon( "cross.png" );
        JButton btn = new JButton( "Click Me" );
        JButton tickBtn = new JButton( tick );
        JButton crossBtn = new JButton( "Stop", cross );
        public Buttons()
            //beginning of next method
        {
           super( "Swing Window" ) ;
           setSize( 500, 200 ) ;
           setDefaultCloseOperation( EXIT_ON_CLOSE ) ;
           add( pnl ) ;
           setVisible( true ) ;
           pnl.add( btn );
           pnl.add( tickBtn );
           pnl.add( crossBtn );
                //end of next method
        }
     
     
        //end of the class
    }

    Everything works, except the images of the cross and the tick do not load. any suggestions would be appreciated.

  24. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Illegal start of expression

    the images of the cross and the tick do not load
    That happens when the image files are not in the right folder when the program executes.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Illegal start of expression
    By bad_newbie in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 16th, 2013, 08:57 PM
  2. Illegal start of expression!
    By NoobOfTheMonth in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 11th, 2013, 09:53 PM
  3. Help With illegal start of expression
    By inshal in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 9th, 2013, 01:20 PM
  4. need help with illegal start of expression
    By inshal in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 9th, 2013, 11:25 AM
  5. Help With illegal start of expression
    By RaceRed in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 7th, 2013, 09:02 PM