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