Helpful Information
 
 
Category: Java
Equation program

I am trying to write a program where the user inputs a number and presses a button and then it plugs the number into an equation and gives an answer. Here is the code I am working with. Im getting errors all over the place. any help would be great.

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

// panel contianing components
public class CalculatorPanel extends JPanel{
// declare the parts of the equasion
private JLabel valueLabel, resultLabel, imageLabel, answerLabel;
private JTextField posInt;
private JButton calculateButton;
private double solution;
ImageIcon icon = new ImageIcon ("expression.GIF");


// set up the GUI
public CalculatorPanel() {

// create labels
valueLabel = new JLabel("Enter a positive value:");
resultLabel = new JLabel("Value of expression: ");
imageLabel = new JLabel(icon);



// add text field
posInt = new JTextField (10);

// add listener and button
calculateButton = new JButton("Calculate Expression");
calculateButton.addActionListener (new PositiveIntListener());

// add to panel
add (imageLabel);
add (valueLabel);
add (posInt);
add (calculateButton);
add (resultLabel);
add (answerLabel);

// set size and background colo
setPreferredSize(new Dimension(300, 150));
setBackground(Color.green);
}

//*******************************
// Listener functions
//*******************************
private class PositiveIntListener implements ActionListener
{
// plug in the int into the equation
public void actionPerformed(ActionEvent event)
{
int integer;
integer = Integer.parseInt (text);
double result = Math.sqrt (Math.abs( 3* Math.pow((Integer), 5) - 12* Math.pow((Integer), 4) - 9* Math.pow((Integer), 2) + 2*Integer));
answerLabel.setText(result);
}
}
}

I have the driver program and im pretty sure it works. the 1st error im getting is around the integer = Integer.parseInt....

ANy advice will be great.

Based on my limited Java knowledge I think there are some issues in the source code you have furnished in your posting.

1.
public class CalculatorPanel extends JPanel
There is no main method in this class.

2.
integer = Integer.parseInt (text);
I wonder where you've declared the variable text. What is it?

3.
double result = Math.sqrt (Math.abs( 3* Math.pow((Integer), 5) - 12* Math.pow((Integer), 4) - 9* Math.pow((Integer), 2) + 2*Integer));

Java is case sensitive you used Integer (java.lang.Integer) instead of integer (small case letter) in the above expression.

OK. 2 and 3 were right. Main is in my driver script. I fixed those 2 things and now im getting the error

edit--------------
i got it to compile but its not working. everything else is the same except this

int integer;
String text = posInt.getText();
integer = Integer.parseInt (text);
double result = Math.sqrt (Math.abs( 3* Math.pow((integer), 5) - 12* Math.pow((integer), 4) - 9* Math.pow((integer), 2) + 2*integer));
answerLabel.setText(Double.toString(result));

I'm surprised you got it to compile. Integer.parseInt() can throw a NumberFormatException, which needs to be caught at some point.

"Not working" isn't an adequate description of a problem. What's the output? What's the expected output? What was the original equation? If no output, what happens instead?

As codeexploiter said, you haven't defined a public static void main(String[]). Are you trying to call the class directly? If so, that won't work without a main().

Ok this is the error i am getting when i try and run it.

Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1019)
at java.awt.Container.add(Container.java:351)
at CalculatorPanel.<init>(CalculatorPanel.java:38)
at Project4b.main(Project4b.java:15)


ANd here is my code.

import javax.swing.*;
// main driver class
import javax.swing.JFrame;

public class Project4b
{
//---------
// creates the main program frame
//---------
public static void main(String[]args)
{
JFrame frame = new JFrame("Calculate Expression");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().add(new CalculatorPanel());

frame.pack();
frame.setVisible(true);
}
}
and

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

// panel contianing components
public class CalculatorPanel extends JPanel{
// declare the parts of the equasion
private JLabel valueLabel, resultLabel, imageLabel, answerLabel;
private JTextField posInt;
private JButton calculateButton;
private double solution;
ImageIcon icon = new ImageIcon ("expression.GIF");


// set up the GUI
public CalculatorPanel() {

// create labels
valueLabel = new JLabel("Enter a positive value:");
resultLabel = new JLabel("Value of expression: ");
imageLabel = new JLabel(icon);



// add text field
posInt = new JTextField (10);

// add listener and button
calculateButton = new JButton("Calculate Expression");
calculateButton.addActionListener (new PositiveIntListener());

// add to panel
add (imageLabel);
add (valueLabel);
add (posInt);
add (calculateButton);
add (resultLabel);
add (answerLabel);

// set size and background colo
setPreferredSize(new Dimension(300, 150));
setBackground(Color.green);
}

//*******************************
// Listener functions
//*******************************
private class PositiveIntListener implements ActionListener
{
// plug in the int into the equation
public void actionPerformed(ActionEvent event)
{
int integer;
String text = posInt.getText();
integer = Integer.parseInt (text);
double result = Math.sqrt (Math.abs( 3* Math.pow((integer), 5) - 12* Math.pow((integer), 4) - 9* Math.pow((integer), 2) + 2*integer));
answerLabel.setText(Double.toString(result));
}
}
}
sorry i was very undescript in my last post, i was in a bit of a rush.

and the output needs to have a picture being displayed, a box for the user to type in, a button, then the answer after its calculated.

thanks

NullPointerException is the segfault of the Java world: in a complex application, it usually means long hours of caffeine-fuelled debugging await, and the sight of it sends grown men crying in fear :)

Well, OK, maybe not quite that bad, but you get the idea.

In your application, thankfully, it's simple: answerLabel hasn't been initialised at the point where you try to add it to the panel.

I am confused by what your saying. What needs to be done to initialize it?

***edit***
Nevermind i understand what you are saying. THANKS!!!










privacy (GDPR)