import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


/*
*Created by ************
*on Dec 28
*to calculate the area of multiple shapes 
*/

public class AreaCalculator extends JFrame implements ActionListener
{
    static JTextField triwidth, trihieght, recwidth, reclength, cirradius;
    static Container contentPane;
    static JPanel infoPane;
    static double circlearea;
    static JLabel circleanswer;



    public AreaCalculator ()
    {

        JMenuBar MenuBar;
        JMenu menu;
        JMenuItem MenuItem;

        MenuBar = new JMenuBar ();
        setJMenuBar (MenuBar);
        //adding menu
        menu = new JMenu ("File");
        MenuBar.add (menu);
        MenuItem = new JMenuItem ("Quit");
        MenuItem.addActionListener (this);
        menu.add (MenuItem);


        infoPane = new JPanel ();
        infoPane.setLayout (null); //setting layout to null so i can use absolute positioning with coordinates
        contentPane = getContentPane ();

        JLabel L1, L2, L3, L4, L5, circleanswer;
        //all the JLabels
        L1 = new JLabel ("Welcome to Jonathan's Area Calculator");
        L1.setBounds (275, 0, 300, 15);
        infoPane.add (L1);

        L2 = new JLabel ("Find area of 1 shape at a time");
        L2.setBounds (300, 20, 275, 15);
        infoPane.add (L2);

        L3 = new JLabel ("Circle...(Enter Radius)");
        L3.setBounds (100, 100, 300, 15);
        infoPane.add (L3);

        L4 = new JLabel ("Triangle...(Enter base and hieght)");
        L4.setBounds (100, 150, 400, 15);
        infoPane.add (L4);

        L5 = new JLabel ("Square/Rectangle...(Enter Length and Width)");
        L5.setBounds (100, 200, 400, 15);
        infoPane.add (L5);


        //all JTextFields
        cirradius = new JTextField (20);
        cirradius.setToolTipText ("Radius"); //tip
        cirradius.setBounds (450, 100, 80, 20);
        infoPane.add (cirradius);

        triwidth = new JTextField (20);
        triwidth.setToolTipText ("Base"); //tip
        triwidth.setBounds (450, 150, 80, 20);
        infoPane.add (triwidth);

        trihieght = new JTextField (20);
        trihieght.setToolTipText ("Hieght"); //tip
        trihieght.setBounds (550, 150, 80, 20);
        infoPane.add (trihieght);

        reclength = new JTextField (20);
        reclength.setToolTipText ("Length"); //tip
        reclength.setBounds (450, 200, 80, 20);
        infoPane.add (reclength);

        recwidth = new JTextField (20);
        recwidth.setToolTipText ("Width"); //tip
        recwidth.setBounds (550, 200, 80, 20);
        infoPane.add (recwidth);

        //all JButtons
        JButton areacircle, areatriangle, arearecsquare;

        areacircle = new JButton ("Calculate......");
        areacircle.addActionListener (this);
        areacircle.setBounds (650, 100, 100, 20);
        infoPane.add (areacircle);

        areatriangle = new JButton ("Calculate.......");
        areatriangle.addActionListener (this);
        areatriangle.setBounds (650, 150, 100, 20);
        infoPane.add (areatriangle);

        arearecsquare = new JButton ("Calculate........");
        arearecsquare.setBounds (650, 200, 100, 20);
        arearecsquare.addActionListener (this);
        infoPane.add (arearecsquare);
        //
        //
        //this is were i need help, its not updating circle area and its static
        circleanswer = new JLabel ();
        circleanswer.setText ("The Area is: " + circlearea);
        circleanswer.setBounds (450, 250, 100, 20);
        infoPane.add (circleanswer);
        //
        //
        //
        contentPane.add (infoPane);
        validate ();
    }


    public void actionPerformed (ActionEvent e)
    {
        String event = e.getActionCommand ();
        if (event.equals ("Quit"))
        {
            hide ();
            System.exit (0);
        }

        if (event.equals ("Calculate......")) //"circle"
        {
            circleanswer.setText("The Area is: " + circlearea);

        }
        if (event.equals ("Calculate.......")) //"triangle"
        {

        }
        if (event.equals ("Calculate........")) //"sqaure/rectangle"
        {

        }

    }


    class Calculate extends JPanel   //method for finding the area of a circle
    {
        
            public void circle ()
            {
                double radius;
                JTextField area;

                String a = cirradius.getText (); //getting info that user inputed
                radius = Double.parseDouble (a); //changing the string to an double
                circlearea = radius * radius * Math.PI; //equation for circle area
            }
        }

    


    public static void main (String[] args)  //main
    {
        AreaCalculator window = new AreaCalculator ();
        window.setTitle ("Universal Area Calculator");
        window.setSize (800, 600);
        window.setVisible (true);

    }
}

I'm trying to make a program that will calculate the area of the shape that the user
inputs the dimensions for, but the circlearea Jlabel wont update. I got this far by looking at other programs, but now I'm just stuck.
I need help
The code runs fine. Right now I'm just working on calculating the area for the circle.
So when I enter a radius, and then click calculate I get an error

the circlearea Jlabel wont update.

The only variable I find with that name is:

static double circlearea;

Can you say what JLabel you are talking about?

I get an error

Please post the full text of the error message.

sorry, i meant

JLabel circleanswer;

and the error I'm recieving is


java.lang.NullPointerException
at AreaCalculator.actionPerformed(AreaCalculator.java:135)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

What about the error message?

Print out the value of the String that is controlling whether the label is set or not and make sure that it's value matches EXACTLY the String it is being compared to.

java.lang.NullPointerException
at AreaCalculator.actionPerformed(AreaCalculator.java:135)

There is a variable at line 135 with a null value.
Look at line 135 and see what variable is null. Then backtrack in the program to see why that variable does not have a valid value. Then change the code so it has a valid value.

I cant figure it out, the

circlearea

isnt null,

can

circleanswer.setText

be null?
Could you run the program and see what is going on, couse I'm doing this course online via amdec, and my teacher is on holidays for 2 weeks, 1 week left. I dont know if Its just simple mistake, I'm going to keep at it.

isnt null,

Add a println just before line 135 and print out its (circleanswer) value to see if it is null.

Where is circleanswer defined? Where do you give it a value?

Add a println just before line 135 and print out its (circleanswer) value to see if it is null.

Where is circleanswer defined? Where do you give it a value?

circle answer is defined after the user inputs the radius of the circle, i just dont know how to make the program go to another section of the program, come back with the answer, and then display it. circle answer hasnt been defined yet, it is suposed to define it by going to public void circle
Im so stuck on this
did you run the program?

I see you are a Retired IBM assembler programmer
so you will try to help me till i figure this out?
I got to go to bed now
I'll be back on tomorrow

Did you see that the variable circleanswer is defined in two places?
The syntax for a definition is the datatype followed by the name.
This is a definition:
JLabel circleanswer;

The syntax for giving a value to a variable is the variable name followed by an = followed by a value. This gives a value to a the variable:
circleanswer = new JLabel ();

How many places in your program do you define the variable: circleanswer?
Post the lines where that variable is defined.

did you run the program?

No, you need to find the error and change it so that it does not get the error.

Did you see that the variable circleanswer is defined in two places?
The syntax for a definition is the datatype followed by the name.
This is a definition:
JLabel circleanswer;

The syntax for giving a value to a variable is the variable name followed by an = followed by a value. This gives a value to a the variable:
circleanswer = new JLabel ();

How many places in your program do you define the variable: circleanswer?
Post the lines where that variable is defined.


No, you need to find the error and change it so that it does not get the error.

circleanswer is defined on lines 109 - 118

//
        //
        //this is were i need help, its not updating circle area and its static
        circleanswer = new JLabel ();
        circleanswer.setText ("The Area is: " + circlearea);
        circleanswer.setBounds (450, 250, 100, 20);
        infoPane.add (circleanswer);
        //
        //
        //

And then on the screen it just says "The area is:0"
because circle area hasn't been given a value yet
then circleanswer is defined on lines 135 again

circleanswer.setText("The Area is: " + circlearea);

Line 135 is suposed to go here and find circle area, then updated the jlabel and contunue the program till the user types in another radius and clicks calculate

class Calculate extends JPanel   //method for finding the area of a circle
    {
 
            public void circle ()
            {
                double radius;
                JTextField area;
 
                String a = cirradius.getText (); //getting info that user inputed
                radius = Double.parseDouble (a); //changing the string to an double
                circlearea = radius * radius * Math.PI; //equation for circle area
            }
        }

circleanswer is defined on lines 109 - 118

Those lines are not the definition of the variable.
Please read my description of how a variable is defined. A variable is defined on one line/statement. Go back to your code and see where circleanswer is defined.

Your understanding of how a program executes is incorrect. There are no instructions in your program to call the circle method in the Calculate class. It will never be called.
If it is not called, circlearea will not be given a value.

Those lines are not the definition of the variable.
Please read my description of how a variable is defined. A variable is defined on one line/statement. Go back to your code and see where circleanswer is defined.

Your understanding of how a program executes is incorrect. There are no instructions in your program to call the circle method in the Calculate class. It will never be called.
If it is not called, circlearea will not be given a value.

Circle answer is defined here

line 18 static JLabel circleanswer; line 43 JLabel L1, L2, L3, L4, L5, circleanswer; wow, just saw that thanks!!

so I removed circle answer from line 43, and now i get no error

but the JLabel circleanswer wont update after I type in a radius and click calculate
how do I make it update by going calling the circle method in the calculate class so that circle area can be given a value?

What is the purpose of the Calculate class and its circle method?
Why does it exist if it is not used?
Could you move the computations from the circle method to the listener method where you detect which button was pressed?

If all the buttons have the same label, how will your code tell which button was pressed?

What is the purpose of the Calculate class and its circle method?
Why does it exist if it is not used?
Could you move the computations from the circle method to the listener method where you detect which button was pressed?

If all the buttons have the same label, how will your code tell which button was pressed?

Each button is different, look carefully

and I'll try to move everything from the circle-method directly too the listener method I'll get back to you

Each button is different, look carefully

That is a very sure way to put bugs in your code and to confuse anyone reading your code and trying to understand what it is doing. In other words, that is a poor technique.
A better way would be to define a String to be used as a label and for testing against:
final String CalcCircleBtnLbl = "Calculate.."; // Define once at beginning of program
...
areacircle = new JButton (CalcCircleBtnLbl); // Use it here
...
if (event.equals (CalcCircleBtnLbl)) // Use it here

That is a very sure way to put bugs in your code and to confuse anyone reading your code and trying to understand what it is doing. In other words, that is a poor technique.

It's just a simple program, and I moved everything to the Listener method, and its works fine, thanks!!

The circleanswer updates and everthing,thanks alot

Glad you got it working.

It's just a simple program

As long as you're not studying to be a programmer, then sloppy programming is not too bad.

Glad you got it working.


As long as you're not studying to be a programmer, then sloppy programming is not too bad.

right now I'm just happy its working, but the next assignment is to take this program, add more menu options and all, and make it more universal, I'll look into making some separate methods and call on those

thanks allot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.