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 7 of 7

Thread: I can't understand the error.

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    20
    Thanks
    4
    Thanked 3 Times in 3 Posts

    Default I can't understand the error.

    When i try running my program i get this error:
    java.lang.NoSuchMethodError: main
    Exception in thread "main" Java Result: 1

    This is my code:
    package button_game;
     
    import javax.swing.*; 
    import java.awt.*;
    import java.awt.event.*;
    public class Button_Game  {
     
     static int k;
     
     
        private static class A1 implements ActionListener{
     
        public void actionPerformed(ActionEvent e){
     
     
         k = 1;
     
     
        }
     
     private static class B1 implements ActionListener {
            public void actionPerformed(ActionEvent e){  
                System.exit(0);
                                                      }  
     }
     
     //addComponentsToPane positions the buttons on the screen randomly
       public static void addComponentsToPane(Container pane){
           int r = 1;
           while (r == 1)
           {               r = r +1;
           int x;
           int y;
            x = (int)(200*Math.random());
        y = (int)(200*Math.random());
     
     
            pane.setLayout(null);
           int a = (int)(1000*Math.random());
            int b = a%2;
            if (b == 0){
                String c = Integer.toString(a);
        JButton A2 = new JButton(c);
        JButton B2 = new JButton();
        A1 listener = new A1();
        A2.addActionListener(listener);                
        B2.addActionListener(listener);
        pane.add(B2);
        pane.add(A2);
        Insets insets = pane.getInsets();
        Dimension size = B2.getPreferredSize();
        B2.setBounds(x + insets.left, x + insets.top, size.width, size.height);
        size = A2.getPreferredSize();
        A2.setBounds(y + insets.left, y + insets.top, size.width, size.height);
                       }
            else if(b != 0);
            int c = a +1;
            String d = Integer.toString(c);
     JButton A2 = new JButton(d);
        JButton B2 = new JButton();
        A1 listener = new A1();
        A2.addActionListener(listener);                
        B2.addActionListener(listener);
        pane.add(B2);
        pane.add(A2);
        Insets insets = pane.getInsets();
        Dimension size = B2.getPreferredSize();
        B2.setBounds(x + insets.left, x + insets.top, size.width, size.height);
        size = A2.getPreferredSize();
        A2.setBounds(y + insets.left, y + insets.top, size.width, size.height); 
                           while (k == 1){
     
            x = (int)(200*Math.random());
        y = (int)(200*Math.random());
     
     
            pane.setLayout(null);
     
            if (b == 0){
     
        A2.addActionListener(listener);                
        B2.addActionListener(listener);
        pane.add(B2);
        pane.add(A2);
     
        B2.setBounds(x + insets.left, x + insets.top, size.width, size.height);
        size = A2.getPreferredSize();
        A2.setBounds(y + insets.left, y + insets.top, size.width, size.height);
                       }
            else if(b != 0);
     
        A2.addActionListener(listener);                
        B2.addActionListener(listener);
        pane.add(B2);
        pane.add(A2);
     
        B2.setBounds(x + insets.left, x + insets.top, size.width, size.height);
        size = A2.getPreferredSize();
        A2.setBounds(y + insets.left, y + insets.top, size.width, size.height); 
     
           }                                   
           }
     
                   } 
        //this runs the game
     
        private static void runGUI(){
     
            JFrame window = new JFrame("Button Game");
            addComponentsToPane(window.getContentPane());
     
            window.setSize(300,300);
            window.setVisible(true);
     
     
        }
     
     
     
     
    //this part runs the subroutine which runs the game
     
       public static void main(String Args[]){
     
      javax.swing.SwingUtilities.invokeLater(new Runnable(){
     
          public void run(){
     
              runGUI();
     
     
              } 
     
     
     
      });
     
      }
        }
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: I can't understand the error.

    You have to compile your code first. Since it doesn't compile you will need to fix the errors first before you can run your program.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    20
    Thanks
    4
    Thanked 3 Times in 3 Posts

    Default Re: I can't understand the error.

    what are the errors? forgive me for being a noob at java programming

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: I can't understand the error.

    I'm not a compiler. The compiler is better suited to tell you what they are and where they are. Does your code compile?

    The error I thought was there was due to the browser not correctly displaying the underscore in your class name.
    Improving the world one idiot at a time!

  5. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: I can't understand the error.

    I can see a main method in this code and it compiled fine for me in Eclipse. It pops up the GUI with 2 buttons on but they don't do anything.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I can't understand the error.

    This looks like a continuation of a problem the OP posted on another thread. I think that The posted code is not what he was executing. The code from the other thread has a call to the main() which was left out of the code posted here. If any of you had your crystal balls working you would have known that.

  7. #7
    Junior Member
    Join Date
    Jul 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I can't understand the error.

    main method defined in the Inner subclass.... check it out once


    Quote Originally Posted by Shivam24 View Post
    When i try running my program i get this error:
    java.lang.NoSuchMethodError: main
    Exception in thread "main" Java Result: 1

    This is my code:
    package button_game;
     
    import javax.swing.*; 
    import java.awt.*;
    import java.awt.event.*;
    public class Button_Game  {
     
     static int k;
     
     
        private static class A1 implements ActionListener{
     
        public void actionPerformed(ActionEvent e){
     
     
         k = 1;
     
     
        }
     
     private static class B1 implements ActionListener {
            public void actionPerformed(ActionEvent e){  
                System.exit(0);
                                                      }  
     }
     
     //addComponentsToPane positions the buttons on the screen randomly
       public static void addComponentsToPane(Container pane){
           int r = 1;
           while (r == 1)
           {               r = r +1;
           int x;
           int y;
            x = (int)(200*Math.random());
        y = (int)(200*Math.random());
     
     
            pane.setLayout(null);
           int a = (int)(1000*Math.random());
            int b = a%2;
            if (b == 0){
                String c = Integer.toString(a);
        JButton A2 = new JButton(c);
        JButton B2 = new JButton();
        A1 listener = new A1();
        A2.addActionListener(listener);                
        B2.addActionListener(listener);
        pane.add(B2);
        pane.add(A2);
        Insets insets = pane.getInsets();
        Dimension size = B2.getPreferredSize();
        B2.setBounds(x + insets.left, x + insets.top, size.width, size.height);
        size = A2.getPreferredSize();
        A2.setBounds(y + insets.left, y + insets.top, size.width, size.height);
                       }
            else if(b != 0);
            int c = a +1;
            String d = Integer.toString(c);
     JButton A2 = new JButton(d);
        JButton B2 = new JButton();
        A1 listener = new A1();
        A2.addActionListener(listener);                
        B2.addActionListener(listener);
        pane.add(B2);
        pane.add(A2);
        Insets insets = pane.getInsets();
        Dimension size = B2.getPreferredSize();
        B2.setBounds(x + insets.left, x + insets.top, size.width, size.height);
        size = A2.getPreferredSize();
        A2.setBounds(y + insets.left, y + insets.top, size.width, size.height); 
                           while (k == 1){
     
            x = (int)(200*Math.random());
        y = (int)(200*Math.random());
     
     
            pane.setLayout(null);
     
            if (b == 0){
     
        A2.addActionListener(listener);                
        B2.addActionListener(listener);
        pane.add(B2);
        pane.add(A2);
     
        B2.setBounds(x + insets.left, x + insets.top, size.width, size.height);
        size = A2.getPreferredSize();
        A2.setBounds(y + insets.left, y + insets.top, size.width, size.height);
                       }
            else if(b != 0);
     
        A2.addActionListener(listener);                
        B2.addActionListener(listener);
        pane.add(B2);
        pane.add(A2);
     
        B2.setBounds(x + insets.left, x + insets.top, size.width, size.height);
        size = A2.getPreferredSize();
        A2.setBounds(y + insets.left, y + insets.top, size.width, size.height); 
     
           }                                   
           }
     
                   } 
        //this runs the game
     
        private static void runGUI(){
     
            JFrame window = new JFrame("Button Game");
            addComponentsToPane(window.getContentPane());
     
            window.setSize(300,300);
            window.setVisible(true);
     
     
        }
     
     
     
     
    //this part runs the subroutine which runs the game
     
       public static void main(String Args[]){
     
      javax.swing.SwingUtilities.invokeLater(new Runnable(){
     
          public void run(){
     
              runGUI();
     
     
              } 
     
     
     
      });
     
      }
        }
    }

Similar Threads

  1. I don't understand what I'm supposed to do
    By dmcettrick in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 11th, 2011, 09:34 AM
  2. I dont understand why this happens....
    By ashenwolf in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 10th, 2011, 09:31 PM
  3. Can't understand why Interfaces are there
    By vortexnl in forum Object Oriented Programming
    Replies: 9
    Last Post: February 14th, 2011, 01:06 PM
  4. I can''t understand this error
    By ragingdemon in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 8th, 2011, 06:07 PM
  5. 2 errors I can't understand
    By Brock in forum What's Wrong With My Code?
    Replies: 8
    Last Post: December 27th, 2010, 12:56 AM