What's wrong with my compiler?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kid Programmer
    New Member
    • Mar 2008
    • 176

    What's wrong with my compiler?

    Hello guys. I have a question. What's wrong with my compiler. In a simple number averaging program in a GUI window have way into the project I compile the program and I get the following errors:

    Severity and Description Path Resource Location Creation Time Id
    The local variable average is never read NumberAveragerG UI/src/NumberAverager NumberAverager. java line 19 1211932107534 648
    The local variable calculate_avera ge is never read NumberAveragerG UI/src/NumberAverager NumberAverager. java line 17 1211932107534 647
    The local variable contentPane is never read NumberAveragerG UI/src/NumberAverager NumberAverager. java line 12 1211932107534 644
    The local variable num1 is never read NumberAveragerG UI/src/NumberAverager NumberAverager. java line 14 1211932107534 645
    The local variable num2 is never read NumberAveragerG UI/src/NumberAverager NumberAverager. java line 15 1211932107534 646


    Here is my code:
    Code:
    package NumberAverager;
    
    import javax.swing.*;
    import java.awt.*;
    
    public class NumberAverager {
    	
    	public static void main(String[] args) {
    		//declare variables
    		JFrame frame;
    		FlowLayout layout;
    		Container contentPane;
    	
    		JTextField num1;
    		JTextField num2;
    		
    		JButton calculate_average;
    		
    		JLabel average;
    		
    		//create a new JFrame and set it's title
    		frame = new JFrame();
    		frame.setTitle("Number Averaging Program");
    		
    		//create a new container
    		contentPane = frame.getContentPane();
    		
    		//create and apply a layout
    		layout = new FlowLayout();
    		frame.setLayout(layout);
    		
    		//create components
    		num1 = new JTextField("Number 1");
    		num2 = new JTextField("Number 2");
    		
    		calculate_average = new JButton("Calculate average");
    		
    		average = new JLabel("Average");
    		
    		//frame settings
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.pack();
    		frame.setVisible(true);
    	}
    }
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    You never add any of the widgets you create to the contentPane. I realize this is code in progress, but I think that's your problem.

    Comment

    • Kid Programmer
      New Member
      • Mar 2008
      • 176

      #3
      Originally posted by Laharl
      You never add any of the widgets you create to the contentPane. I realize this is code in progress, but I think that's your problem.
      Thanks. That worked :-)

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Lesson learned: there is nothing wrong with the compiler. If a compiler complains
        about something you can be darn sure that the fault is in your code, not the
        compiler's code. Most if not all nowadays' compilers are generated by parser-
        and code-generators; these things either utterly fail on the first attempt to set
        them to work or they simply work. So if a compiler complains: check your code.

        kind regards,

        Jos

        Comment

        • Kid Programmer
          New Member
          • Mar 2008
          • 176

          #5
          I said number averaging program but I accidently made a calculator. Woops.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by Kid Programmer
            I said number averaging program but I accidently made a calculator. Woops.
            That is called: bad design.

            kind regards,

            Jos

            Comment

            • Kid Programmer
              New Member
              • Mar 2008
              • 176

              #7
              Originally posted by JosAH
              That is called: bad design.

              kind regards,

              Jos
              LOL And I thought something was wrong with my compiler because I thought the variables had been read because I declared them.

              Comment

              Working...