-1

I have been trying to create a gui that calculates trigonometric functions based off of the user's input. I have had success in the GUI part, but my class that I wrote to hold information using inheritance seems to be messed up, because when I run it gives an error saying:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:181)
at TrigCalc.TrigCalcGUI$CalcButtonListener.actionPerformed(TrigCalcGUI.java:191)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at  javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6288)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6053)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4651)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4481)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4481)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:643)
at java.awt.EventQueue.access$000(EventQueue.java:84)
at java.awt.EventQueue$1.run(EventQueue.java:602)
at java.awt.EventQueue$1.run(EventQueue.java:600)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:616)
at java.awt.EventQueue$2.run(EventQueue.java:614)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:613)
at     java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

I changed the order of programs to make it easier to read:

public class TrigCalcCon
{
public double sine;
public double cosine;
public double tangent;

public TrigCalcCon(double sin, double cos, double tan)
{
    sine = sin;
    cosine = cos;
    tangent = tan;
}

public void setSin(double s)
{
    sine = s;
}

public void setCos(double cs)
{
    cosine = cs;
}

public void setTan(double t)
{
    tangent = t;
}

public void set(double s, double cs, double t)
{
    sine = s;
    cosine = cs;
    tangent = t;
}

public double getSin()
{
    return Math.sin(sine);
}

public double getCos()
{
    return Math.cos(cosine);
}

public double getTan()
{
    return Math.tan(tangent);
}
}

Here is the inheritance class.

public class ArcTrigCalcCon extends TrigCalcCon
{
// Instance Variables
public double cosecant;
public double secant;
public double cotangent;

public ArcTrigCalcCon(double s, double cs, double t)
{
    // Inherit from the Trig Calc class
    super(s, cs, t);
    cosecant = 1/s;
    secant = 1/cs;
    cotangent = 1/t;
}

public void setCsc(double csc)
{
    cosecant = csc;
}

public void setSec(double sc)
{
    secant = sc;
}

public void setCot(double ct)
{
    cotangent = ct;
}

}

and here is the demo class to run the gui:

public class TrigCalcGUI extends JFrame implements ActionListener
{
// Instance Variables
private String input;
private double s = 0, cs = 0, t = 0;
private JPanel mainPanel, sinPanel, cosPanel, tanPanel, cscPanel, secPanel, 
        cotPanel, buttonPanel, inputPanel, displayPanel; // Panel Display
private JLabel inputLabel;
private JTextField sinTF, cosTF, tanTF, secTF,
        cscTF, cotTF, inputTF; //Text Fields for sin, cos, and tan, and inverse
private JButton calcButton, clearButton; // Calculate and Exit Buttons

// Object
ArcTrigCalcCon trC = new ArcTrigCalcCon(s, cs, t);

public TrigCalcGUI()
{
    // title bar text.
    super("Trig Calculator");
    // Corner exit button action.
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Create main panel to add each panel to
    mainPanel = new JPanel();
    mainPanel.setLayout(new GridLayout(3,2));
    displayPanel = new JPanel();
    displayPanel.setLayout(new GridLayout(3,2));

    // Assign Panel to each variable
    inputPanel = new JPanel();
    sinPanel = new JPanel();
    cosPanel = new JPanel();
    tanPanel = new JPanel();
    cscPanel = new JPanel();
    secPanel = new JPanel();
    cotPanel = new JPanel();
    buttonPanel = new JPanel();

    // Call each constructor
    buildInputPanel();
    buildSinCosTanPanels();
    buildCscSecCotPanels();
    buildButtonPanel();

    // Add each panel to content pane
    displayPanel.add(sinPanel);
    displayPanel.add(cscPanel);
    displayPanel.add(cosPanel);
    displayPanel.add(secPanel);
    displayPanel.add(tanPanel);
    displayPanel.add(cotPanel);

    // Add three content panes to GUI
    mainPanel.add(inputPanel, BorderLayout.NORTH);
    mainPanel.add(displayPanel, BorderLayout.CENTER);
    mainPanel.add(buttonPanel, BorderLayout.SOUTH);


    //add mainPanel
    this.add(mainPanel);
    // size of window to content
    this.pack();

    // display window
    setVisible(true);
}

public static void main(String[] args)
{
    new TrigCalcGUI();
}

private void buildInputPanel()
{
    inputLabel = new JLabel("Enter a Value: ");
    inputTF = new JTextField(5);

    inputPanel.add(inputLabel);
    inputPanel.add(inputTF);
}

// Building Constructor for sinPanel cosPanel, and tanPanel
private void buildSinCosTanPanels()
{
    // Set layout and border for sinPanel
    sinPanel.setLayout(new GridLayout(2,2));
    sinPanel.setBorder(BorderFactory.createTitledBorder("Sine"));

    // 
    sinTF = new JTextField(5);
    sinTF.setEditable(false);

    sinPanel.add(sinTF);

    // Set layout and border for cosPanel
    cosPanel.setLayout(new GridLayout(2,2));
    cosPanel.setBorder(BorderFactory.createTitledBorder("Cosine"));

    cosTF = new JTextField(5);
    cosTF.setEditable(false);

    cosPanel.add(cosTF);

    // Set layout and border for tanPanel
    tanPanel.setLayout(new GridLayout(2,2));
    tanPanel.setBorder(BorderFactory.createTitledBorder("Tangent"));

    tanTF = new JTextField(5);
    tanTF.setEditable(false);

    tanPanel.add(tanTF);
}

// Building Constructor for cscPanel secPanel, and cotPanel
private void buildCscSecCotPanels()
{
    // Set layout and border for cscPanel
    cscPanel.setLayout(new GridLayout(2,2));
    cscPanel.setBorder(BorderFactory.createTitledBorder("Cosecant"));

    // 
    cscTF = new JTextField(5);
    cscTF.setEditable(false);

    cscPanel.add(cscTF);

    // Set layout and border for secPanel
    secPanel.setLayout(new GridLayout(2,2));
    secPanel.setBorder(BorderFactory.createTitledBorder("Secant"));

    secTF = new JTextField(5);
    secTF.setEditable(false);

    secPanel.add(secTF);

    // Set layout and border for cotPanel
    cotPanel.setLayout(new GridLayout(2,2));
    cotPanel.setBorder(BorderFactory.createTitledBorder("Cotangent"));

    cotTF = new JTextField(5);
    cotTF.setEditable(false);

    cotPanel.add(cotTF);
}

private void buildButtonPanel()
{
    // Create buttons and add events
    calcButton = new JButton("Calculate");
    calcButton.addActionListener(new CalcButtonListener());
    clearButton = new JButton("Clear");
    clearButton.addActionListener(new ClearButtonListener());

    buttonPanel.add(calcButton);
    buttonPanel.add(clearButton);
}

@Override
public void actionPerformed(ActionEvent e)
{

}

private class CalcButtonListener implements ActionListener
{
      public void actionPerformed(ActionEvent ae)
      {
          // Declare boolean variable
          boolean incorrect = true;

          // Set input variable to input text field text
          input = inputTF.getText();

          ImageIcon newIcon;
          ImageIcon frowny = new 
                          ImageIcon(TrigCalcGUI.class.getResource("/Sad_Face.png"));
                  Image gm = frowny.getImage();
                  Image newFrowny = gm.getScaledInstance(100, 100,
                          java.awt.Image.SCALE_FAST);
                  newIcon = new ImageIcon(newFrowny); 

          // If boolean is true, throw exception
          if(incorrect)
          {
              try{Double.parseDouble(input); incorrect = false;}

              catch(NumberFormatException nfe)
              {
                  String s = "Invalid Input "
                          + "/n Input Must Be a Numerical value."
                          + "/nPlease Press Ok and Try Again";

                  JOptionPane.showMessageDialog(null, s, "Invalid",
                      JOptionPane.ERROR_MESSAGE, newIcon);

                  inputTF.setText("");
                  inputTF.requestFocus();
              }
          }

          // If boolean is not true, proceed with output
          if (incorrect != true)
          {

              /* Set each text field's output to the String double value 
               * of inputTF
               */
              sinTF.setText(input);
              cosTF.setText(input);
              tanTF.setText(input);
              cscTF.setText(input);
              secTF.setText(input);
              cotTF.setText(input);
          }
      }
}

/**
 *  Private inner class that handles the event when
 *  the user clicks the Exit button. 
 */

private class ClearButtonListener implements ActionListener
{
  public void actionPerformed(ActionEvent ae)
  {
      // Clear field
      sinTF.setText("");
      cosTF.setText("");
      tanTF.setText("");
      cscTF.setText("");
      secTF.setText("");
      cotTF.setText("");

        // Clear textfield and set cursor focus to field
        inputTF.setText("");
        inputTF.requestFocus();
   }
}
}

I may need some help breaking this down if possible as I am a visual learner. I understand this is probably really simple, and since I am a beginner it's a bit difficult to understand.

Clinton Scott
  • 45
  • 1
  • 9

3 Answers3

0

You are creating your variables as 'Double' but defining the functions that use them to accept parameters that are 'double'. You can go in at least 3 directions to fix this issue; change the variable definitions to be 'double' or change the function definition to use 'Double', or leave the declarations alone and call the function with the doubleValue of the Doubles.

ArcTrigCalcCon trC = new ArcTrigCalcCon(s.doubleValue(), cs.doubleValue(), t.doubleValue());
GregA100k
  • 1,385
  • 1
  • 11
  • 16
0

Right here:

ArcTrigCalcCon trC = new ArcTrigCalcCon(s, cs, t);

You're trying to create a new ArcTrigCalcCon object with the TrigCalcCon constructor and constructors aren't inherited in Java. You either have to create a constructor with parameters identical to the superclass constructor and pass them along with super(s, cs, t); or use the 6 double constructor you've provided.

Radiodef
  • 37,180
  • 14
  • 90
  • 125
0

Your constructor for ArcTrigCalcCon has six parameters, but you only actually use three of them. The last three parameters are not used at all in that constructor, so you could delete them if you wanted to.

Now, when you try to create an ArcTrigCalcCon object in your TrigCalcGui class, you're trying to pass only three arguments to the constructor, instead of all six; and that is why the compiler is giving you an error. You can do one of two things

  • EITHER change the line where you're creating the object, to pass in all six arguments rather than just three,
  • OR change the constructor as I recommended in the first paragraph above, so that it only needs three arguments.

Either course of action will make the error go away.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
  • I did what you have said and it does seem the error is still there when I run the program. – Clinton Scott Oct 29 '13 at 00:39
  • It still says "Uncompilable source code?" Or is it a different error? – Dawood ibn Kareem Oct 29 '13 at 01:09
  • It looks like you still have an error because there is another problem. You are allowed to pass `Double` as an argument in place of a `double` because the compiler automatically 'unboxes' the value. Right now you haven't instantiated your s, cs and t Double objects so by trying to pass them to the constructor you are trying to pass null which can't be a double. You'd have to instantiate them before you can pass them to the constructor. – Radiodef Oct 29 '13 at 01:11
  • I instantiated my variables to 0, and from what I got, this should allow it to pass into the constructor. There may be something else wrong because I am getting access to my gui again, but upon hitting thecalc button another error appears. – Clinton Scott Oct 29 '13 at 01:26
  • And what is that "other error" that appears? Please, Clinton, when you tell us about an error, don't just say "an error" and let us all guess. Tell us the error. – Dawood ibn Kareem Oct 29 '13 at 01:37
  • Ill enter it last up above. – Clinton Scott Oct 29 '13 at 01:50
  • Ooh, that would be the "division by zero" error that you're not handling. Not the best of plans to set three variables to zero, then divide by each of them in turn. – Dawood ibn Kareem Oct 29 '13 at 01:54
  • I gotcha. Do you have a suggestion if I should set them to a specific number? I had set them to zero but type in a random number such as 1 to normally output sin(1) etc. I would like to input any positive integer for now without an error. – Clinton Scott Oct 29 '13 at 02:16