Results 1 to 13 of 13
  1. #1
    Join Date
    Apr 2011
    Posts
    6
    Rep Power
    0

    Default Err: invalid method declaration; return type required

    I dunno what i am doing wrong.... i know it saying i need to put in a return type, but im kinda new and i dont see where it is supposed to go or what it is supposed to be.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Hw09_msj extends JFrame
    {
      private Container cont;
      private JRadioButton uCase;
      private JRadioButton lCase;
      private ButtonGroup buttonGroup;
      private JLabel label;
    
      public changeCase()
      {
        super("Case Changer");
        cont = getContentPane();
        cont.setLayout(new FlowLayout());
    
        uCase = new JRadioButton("UPPERCASE", true);
        lCase = new JRadioButton("lowercase");
    
        label = new JLabel("WATCH ME CHANGE CASE");
    
        cont.add(uCase);
        cont.add(lCase);
        cont.add(label);
    
        buttonGroup = new ButtonGroup();
        buttonGroup.add(uCase);
        buttonGroup.add(lCase);
    
        changeCase.RadioButtonHandler rbh = new changeCase.RadioButtonHandler(null);
        uCase.addItemListener(rbh);
        lCase.addItemListener(rbh);
    
        setSize(300, 200);
        setVisible(true);
      }
    
      public static void main(String[] args)
      {
        changeCase cc = new changeCase();
        cc.setDefaultCloseOperation(3);
      }
    
      private class RadioButtonHandler
        implements ItemListener
      {
        private RadioButtonHandler()
        {
        }
    
        public void itemStateChanged(ItemEvent ie)
        {
          if (ie.getSource() == changeCase.uCase)
            changeCase.label.setText(changeCase.label.getText().toUpperCase());
          else if (ie.getSource() == changeCase.lCase)
            changeCase.label.setText(changeCase.label.getText().toLowerCase());
        }
      }
    }

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

    Default

    The name of the constructor needs to be the same as the name of the class. Please check your tutorial for how to create constructors and classes for the details on this and other issues with creating classes and constructors.

  3. #3
    Join Date
    Apr 2011
    Posts
    6
    Rep Power
    0

    Default

    I'm sorry would you be willing to copy and paste the part your talking about, please forgive my naivety

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

    Default

    Quote Originally Posted by Die The Villain View Post
    I'm sorry would you be willing to copy and paste the part your talking about, please forgive my naivety
    No, I want you to gain an understanding enough to fix it for yourself.

    Do you understand what part above is the name of the class? Please post it for us.

    Do you know what a constructor is? If so, what is the name of your constructor above?

    If you don't understand these concepts, you absolutely must read the tutorials first to be able to understand our advice. I'm asking you to expend a little effort in solving this, but it is effort well spent and will help you in the future as you will have a deeper understanding of what you are doing.

  5. #5
    Join Date
    Apr 2011
    Posts
    6
    Rep Power
    0

    Default

    My class is Hw09_msj and changeCase cc = new changeCase is my constructor, but what im having a problem with is my class HAS to be Hw09_msj and i tried changing my constructor to Hw09_msj cc = new Hw09_msj abd then making the changes through out my code, but that gave me even more errors....

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

    Default

    Quote Originally Posted by Die The Villain View Post
    My class is Hw09_msj and changeCase cc = new changeCase is my constructor, but what im having a problem with is my class HAS to be Hw09_msj and i tried changing my constructor to Hw09_msj cc = new Hw09_msj abd then making the changes through out my code, but that gave me even more errors....
    Close, but "Hw09_msj cc = new Hw09_msj abd" is not the constructor. It is trying to create an instance of the class by calling the constructor.

    Rather the constructor is in the your code posted above and is like a method, but without a return type... this line:

    Java Code:
    public changeCase()
    So to answer my question, your constructor name is "changeCase". What happens if you change this to the class name?

  7. #7
    Join Date
    Apr 2011
    Posts
    6
    Rep Power
    0

    Default

    i get 14 errors, so then i change ALL cases of changeCase to Hw09_msj and i get 7 errors...


    Java Code:
    --------------------Configuration: <Default>--------------------
    C:\Users\Matt\Desktop\COP 2551\Hw09_msj.java:32: cannot find symbol
    symbol  : constructor RadioButtonHandler(<nulltype>)
    location: class Hw09_msj.RadioButtonHandler
        Hw09_msj.RadioButtonHandler rbh = new Hw09_msj.RadioButtonHandler(null);
                                          ^
    C:\Users\Matt\Desktop\COP 2551\Hw09_msj.java:55: non-static variable uCase cannot be referenced from a static context
          if (ie.getSource() == Hw09_msj.uCase)
                                        ^
    C:\Users\Matt\Desktop\COP 2551\Hw09_msj.java:56: non-static variable label cannot be referenced from a static context
            Hw09_msj.label.setText(Hw09_msj.label.getText().toUpperCase());
                                           ^
    C:\Users\Matt\Desktop\COP 2551\Hw09_msj.java:56: non-static variable label cannot be referenced from a static context
            Hw09_msj.label.setText(Hw09_msj.label.getText().toUpperCase());
                    ^
    C:\Users\Matt\Desktop\COP 2551\Hw09_msj.java:57: non-static variable lCase cannot be referenced from a static context
          else if (ie.getSource() == Hw09_msj.lCase)
                                             ^
    C:\Users\Matt\Desktop\COP 2551\Hw09_msj.java:58: non-static variable label cannot be referenced from a static context
            Hw09_msj.label.setText(Hw09_msj.label.getText().toLowerCase());
                                           ^
    C:\Users\Matt\Desktop\COP 2551\Hw09_msj.java:58: non-static variable label cannot be referenced from a static context
            Hw09_msj.label.setText(Hw09_msj.label.getText().toLowerCase());
                    ^
    7 errors
    
    Process completed.

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

    Default

    Quote Originally Posted by Die The Villain View Post
    i get 14 errors, so then i change ALL cases of changeCase to Hw09_msj and i get 7 errors...

    Please show the latest code, with code tags.

  9. #9
    Join Date
    Apr 2011
    Posts
    6
    Rep Power
    0

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Hw09_msj extends JFrame
    {
      private Container cont;
      private JRadioButton uCase;
      private JRadioButton lCase;
      private ButtonGroup buttonGroup;
      private JLabel label;
    
      public Hw09_msj()
      {
        super("Case Changer");
        cont = getContentPane();
        cont.setLayout(new FlowLayout());
    
        uCase = new JRadioButton("UPPERCASE", true);
        lCase = new JRadioButton("lowercase");
    
        label = new JLabel("WATCH ME CHANGE CASE");
    
        cont.add(uCase);
        cont.add(lCase);
        cont.add(label);
    
        buttonGroup = new ButtonGroup();
        buttonGroup.add(uCase);
        buttonGroup.add(lCase);
    
        Hw09_msj.RadioButtonHandler rbh = new Hw09_msj.RadioButtonHandler(null);
        uCase.addItemListener(rbh);
        lCase.addItemListener(rbh);
    
        setSize(300, 200);
        setVisible(true);
      }
    
      public static void main(String[] args)
      {
        Hw09_msj cc = new Hw09_msj();
        cc.setDefaultCloseOperation(3);
      }
    
      private class RadioButtonHandler
        implements ItemListener
      {
        private RadioButtonHandler()
        {
        }
    
        public void itemStateChanged(ItemEvent ie)
        {
          if (ie.getSource() == Hw09_msj.uCase)
            Hw09_msj.label.setText(Hw09_msj.label.getText().toUpperCase());
          else if (ie.getSource() == Hw09_msj.lCase)
            Hw09_msj.label.setText(Hw09_msj.label.getText().toLowerCase());
        }
      }
    }

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

    Default

    OK, so you see that you've fixed your initial problem, right?

    So now change:

    Java Code:
    Hw09_msj.RadioButtonHandler rbh = new Hw09_msj.RadioButtonHandler(null);

    to

    Java Code:
    RadioButtonHandler rbh = new RadioButtonHandler();

    For your other problems, get rid of the "Hw09_msj." stuck in front of variables and methods.

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Since you are using an inner class the inner class can access all of the class variables. You can change the label directly in the handler with just callig it label, no need to add the outer class in there. This also applies when creating an instance of the handler in the main class.

  12. #12
    Join Date
    Apr 2011
    Posts
    6
    Rep Power
    0

    Default

    This is what i have now

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Hw09_msj extends JFrame
    {
      private Container cont;
      private JRadioButton uCase;
      private JRadioButton lCase;
      private ButtonGroup buttonGroup;
      private JLabel label;
    
      public Hw09_msj()
      {
        super("Case Changer");
        cont = getContentPane();
        cont.setLayout(new FlowLayout());
    
        uCase = new JRadioButton("UPPERCASE", true);
        lCase = new JRadioButton("lowercase");
    
        label = new JLabel("WATCH ME CHANGE CASE");
    
        cont.add(uCase);
        cont.add(lCase);
        cont.add(label);
    
        buttonGroup = new ButtonGroup();
        buttonGroup.add(uCase);
        buttonGroup.add(lCase);
    
        RadioButtonHandler rbh = new RadioButtonHandler(null);
        uCase.addItemListener(rbh);
        lCase.addItemListener(rbh);
    
        setSize(300, 200);
        setVisible(true);
      }
    
      public static void main(String[] args)
      {
        Hw09_msj cc = new Hw09_msj();
        cc.setDefaultCloseOperation(3);
      }
    
      private class RadioButtonHandler
        implements ItemListener
      {
        private RadioButtonHandler()
        {
        }
    
        public void itemStateChanged(ItemEvent ie)
        {
          if (ie.getSource() == uCase)
            label.setText(label.getText().toUpperCase());
          else if (ie.getSource() == lCase)
            label.setText(label.getText().toLowerCase());
        }
      }
    }
    and im getting this error

    Java Code:
    --------------------Configuration: <Default>--------------------
    C:\Users\Matt\Desktop\COP 2551\Hw09_msj.java:32: cannot find symbol
    symbol  : constructor RadioButtonHandler(<nulltype>)
    location: class Hw09_msj.RadioButtonHandler
        RadioButtonHandler rbh = new RadioButtonHandler(null);
                                 ^
    1 error
    
    Process completed.

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

    Default

    You passing a parameter into the RadioButtonHandler constructor, but if you look at the RadioButtonHandler you'll see that the constructor you've created for it takes no parameter -- so pass it none.

Similar Threads

  1. JavaBean - invalid method declaration and return type required
    By jprog in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 05-07-2011, 06:13 PM
  2. invalid method declaration; return type required
    By XaviannNJ in forum JCreator
    Replies: 7
    Last Post: 11-25-2010, 01:59 PM
  3. Replies: 3
    Last Post: 06-05-2010, 08:05 PM
  4. invalid method declaration error
    By bsarules in forum Java Applets
    Replies: 7
    Last Post: 05-25-2010, 06:06 PM
  5. Error: invalid method declaration
    By silvia in forum New To Java
    Replies: 1
    Last Post: 07-27-2007, 12:10 PM

Posting Permissions

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