Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4005386
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:23:14+00:00 2026-05-20T08:23:14+00:00

When trying to call on a method from another class my method as a

  • 0

When trying to call on a method from another class my method as a error at

private class equalsButton implements ActionListener {

    // makes the equals button listener
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("=");
        InfixToPostfixParens.convert(operator); // Line with error
    }
}

Saying Non Static String cannot be referenced from a static context. The variable isn’t static but I still get this problem any ideas?

The Bulk of my main program as it stands now (Sorry for the sloppy coding in places)

/**
 * Graphics of the calculator
 * 
 * @author Collin Blake
 * @version (2-28-11)
 */
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class SimpleCalculator extends JFrame {

    JTextField textField;
    String operator = "";

    public SimpleCalculator() {
        setTitle("Simple Calculator"); // makes the window for the calculator
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridLayout(0, 1));
        JPanel topPanel = new JPanel(); // Initilaized for the text window
        topPanel.setLayout(new GridLayout());
        textField = new JTextField(10);
        textField.addActionListener(new NumEntered());
        topPanel.add(textField);
        add(topPanel);
        JPanel centerPanel = new JPanel(); // center panel initilaized
        centerPanel.setLayout(new GridLayout(0, 4));
        JButton oneButton = new JButton("1"); // initilaized values for each
                                                // button in their menus
        JButton twoButton = new JButton("2");
        JButton threeButton = new JButton("3");
        JButton plusButton = new JButton("+");
        JButton fourButton = new JButton("4");
        JButton fiveButton = new JButton("5");
        JButton sixButton = new JButton("6");
        JButton minusButton = new JButton("-");
        JButton sevenButton = new JButton("7");
        JButton eightButton = new JButton("8");
        JButton nineButton = new JButton("9");
        JButton timesButton = new JButton("*");
        JButton zeroButton = new JButton("0");
        JButton decimalButton = new JButton(".");
        JButton powerButton = new JButton("^");
        JButton divideButton = new JButton("/");
        JButton LParButton = new JButton("(");
        JButton RParButton = new JButton(")");
        JButton equalsButton = new JButton("="); // creates equals button
        JButton allClearButton = new JButton("AC");
        oneButton.addActionListener(new oneButton()); // initilaized values for
                                                        // each button's
                                                        // actionlistener
        twoButton.addActionListener(new twoButton());
        threeButton.addActionListener(new threeButton());
        plusButton.addActionListener(new plusButton());
        fourButton.addActionListener(new fourButton());
        fiveButton.addActionListener(new fiveButton());
        sixButton.addActionListener(new sixButton());
        minusButton.addActionListener(new minusButton());
        sevenButton.addActionListener(new sevenButton());
        eightButton.addActionListener(new eightButton());
        nineButton.addActionListener(new nineButton());
        timesButton.addActionListener(new timesButton());
        zeroButton.addActionListener(new zeroButton());
        decimalButton.addActionListener(new decimalButton());
        powerButton.addActionListener(new powerButton());
        divideButton.addActionListener(new divideButton());
        LParButton.addActionListener(new LParButton());
        RParButton.addActionListener(new RParButton());
        equalsButton.addActionListener(new equalsButton()); // creates action
                                                            // listener for
                                                            // equals
        allClearButton.addActionListener(new allClearButton());
        centerPanel.add(oneButton); // Adds the buttons to the sub panel
        centerPanel.add(twoButton);
        centerPanel.add(threeButton);
        centerPanel.add(plusButton);
        centerPanel.add(fourButton);
        centerPanel.add(fiveButton);
        centerPanel.add(sixButton);
        centerPanel.add(minusButton);
        centerPanel.add(sevenButton);
        centerPanel.add(eightButton);
        centerPanel.add(nineButton);
        centerPanel.add(timesButton);
        centerPanel.add(zeroButton);
        centerPanel.add(decimalButton);
        centerPanel.add(powerButton);
        centerPanel.add(divideButton);
        centerPanel.add(LParButton);
        centerPanel.add(RParButton);
        centerPanel.add(equalsButton);
        centerPanel.add(allClearButton);
        add(centerPanel); // Creates the panel with buttons
        pack();
        setLocationRelativeTo(null);
    }

    private class NumEntered implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            // do nothing
        }
    }

    private class oneButton implements ActionListener {

        // makes the one button listener
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("1");
            operator += "1";
            textField.setText(operator);
        }
    }

    private class twoButton implements ActionListener {

        // makes the two button listener
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("2");
            operator += "2";
            textField.setText(operator);
        }
    }

    private class threeButton implements ActionListener {

        // makes the three button listener
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("3");
            operator += "3";
            textField.setText(operator);
        }
    }

    private class fourButton implements ActionListener {

        // makes the four button listener
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("4");
            operator += "4";
            textField.setText(operator);
        }
    }

    private class fiveButton implements ActionListener {

        // makes the five button listener
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("5");
            operator += "5";
            textField.setText(operator);
        }
    }

    private class sixButton implements ActionListener {

        // makes the six button listener
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("6");
            operator += "6";
            textField.setText(operator);
        }
    }

    private class sevenButton implements ActionListener {

        // makes the seven button listener
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("7");
            operator += "7";
            textField.setText(operator);
        }
    }

    private class eightButton implements ActionListener {

        // makes the eight button listener
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("8");
            operator += "8";
            textField.setText(operator);
        }
    }

    private class nineButton implements ActionListener {

        // makes the nine button listener
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("9");
            operator += "9";
            textField.setText(operator);
        }
    }

    private class zeroButton implements ActionListener {

        // makes the zerobutton listener
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("0");
            operator += "0";
            textField.setText(operator);
        }
    }

    private class plusButton implements ActionListener {

        // makes the plus button listener
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("+");
            operator += "+";
            textField.setText(operator);
        }
    }

    private class minusButton implements ActionListener {

        // makes the minus button listener
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("-");
            operator += "-";
            textField.setText(operator);
        }
    }

    private class timesButton implements ActionListener {

        // makes the times button listener
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("*");
            operator += "*";
            textField.setText(operator);
        }
    }

    private class divideButton implements ActionListener {

        // makes the divide button listener
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("/");
            operator += "/";
            textField.setText(operator);
        }
    }

    private class allClearButton implements ActionListener {

        // makes the all clear button listener
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("AC");
            operator = "";
            textField.setText(operator);
        }
    }

    private class decimalButton implements ActionListener {

        // makes the decimal button listener
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println(".");
            operator += ".";
            textField.setText(operator);
        }
    }

    private class equalsButton implements ActionListener {

        // makes the equals button listener
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("=");
            InfixToPostfixParens.convert(operator);
        }
    }

    private class LParButton implements ActionListener {

        // makes the ( button listener
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("(");
            operator += "(";
            textField.setText(operator);
        }
    }

    private class RParButton implements ActionListener {

        // makes the ) button listener
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println(")");
            operator += ")";
            textField.setText(operator);
        }
    }

    private class powerButton implements ActionListener {

        // makes the ^ button listener
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("^");
            operator += "^";
            textField.setText(operator);
        }
    }
}

The code for the InfixToPostfixParens

import java.util.Stack;
import java.util.EmptyStackException;
import java.util.regex.Pattern;
import java.util.Scanner;

/**
 * Translates an infix expression with parentheses to a postfix expression.
 * 
 * @author Koffman & Wolfgang
 */
public class InfixToPostfixParens {

    // Nested Class
    /** Class to report a syntax error. */
    public static class SyntaxErrorException extends Exception {

        /**
         * Construct a SyntaxErrorException with the specified message.
         * 
         * @param message
         *            The message
         */
        SyntaxErrorException(String message) {
            super(message);
        }
    }

    // Data Fields
    /** The operator stack */
    private Stack<Character> operatorStack;
    /** The operators */
    private static final String OPERATORS = "-+*/()";
    /**
     * The Pattern to extract tokens A token is either a string of digits (\d+)
     * or a JavaIdentifier or an operator
     */
    private static final Pattern pattern = Pattern.compile("\\d+\\.\\d*|\\d+|"
            + "\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*" + "|["
            + OPERATORS + "]");
    /** The precedence of the operators, matches order of OPERATORS. */
    private static final int[] PRECEDENCE = { 1, 1, 2, 2, -1, -1 };
    /** The postfix string */
    private StringBuilder postfix;

    /**
     * Convert a string from infix to postfix.
     * 
     * @param infix
     *            The infix expression
     * @throws SyntaxErrorException
     */
    public String convert(String infix) throws SyntaxErrorException {
        operatorStack = new Stack<Character>();
        postfix = new StringBuilder();
        Scanner scan = new Scanner(infix);
        try {
            // Process each token in the infix string.
            String nextToken;
            while ((nextToken = scan.findInLine(pattern)) != null) {
                char firstChar = nextToken.charAt(0);
                // Is it an operand?
                if (Character.isJavaIdentifierStart(firstChar)
                        || Character.isDigit(firstChar)) {
                    postfix.append(nextToken);
                    postfix.append(' ');
                } // Is it an operator?
                else if (isOperator(firstChar)) {
                    processOperator(firstChar);
                } else {
                    throw new SyntaxErrorException(
                            "Unexpected Character Encountered: " + firstChar);
                }
            } // End while.
                // Pop any remaining operators
                // and append them to postfix.
            while (!operatorStack.empty()) {
                char op = operatorStack.pop();
                // Any '(' on the stack is not matched.
                if (op == '(') {
                    throw new SyntaxErrorException(
                            "Unmatched opening parenthesis");
                }
                postfix.append(op);
                postfix.append(' ');
            }
            // assert: Stack is empty, return result.
            return postfix.toString();
        } catch (EmptyStackException ex) {
            throw new SyntaxErrorException("Syntax Error: The stack is empty");
        }
    }

    /**
     * Method to process operators.
     * 
     * @param op
     *            The operator
     * @throws EmptyStackException
     */
    private void processOperator(char op) {
        if (operatorStack.empty() || op == '(') {
            operatorStack.push(op);
        } else {
            // Peek the operator stack and
            // let topOp be the top operator.
            char topOp = operatorStack.peek();
            if (precedence(op) > precedence(topOp)) {
                operatorStack.push(op);
            } else {
                // Pop all stacked operators with equal
                // or higher precedence than op.
                while (!operatorStack.empty()
                        && precedence(op) <= precedence(topOp)) {
                    operatorStack.pop();
                    if (topOp == '(') {
                        // Matching '(' popped - exit loop.
                        break;
                    }
                    postfix.append(topOp);
                    postfix.append(' ');
                    if (!operatorStack.empty()) {
                        // Reset topOp.
                        topOp = operatorStack.peek();
                    }
                }
                // assert: Operator stack is empty or
                // current operator precedence >
                // top of stack operator precedence.
                if (op != ')') {
                    operatorStack.push(op);
                }
            }
        }
    }

    /**
     * Determine whether a character is an operator.
     * 
     * @param ch
     *            The character to be tested
     * @return true if ch is an operator
     */
    private boolean isOperator(char ch) {
        return OPERATORS.indexOf(ch) != -1;
    }

    /**
     * Determine the precedence of an operator.
     * 
     * @param op
     *            The operator
     * @return the precedence
     */
    private int precedence(char op) {
        return PRECEDENCE[OPERATORS.indexOf(op)];
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T08:23:14+00:00Added an answer on May 20, 2026 at 8:23 am

    How does your code differ from the following (which compiles)?

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class Example1 {
        private String operator;
    
        private class equalsButton implements ActionListener {
            public void actionPerformed(ActionEvent e) {
               System.out.println("=");
               InfixToPostfixParens.convert(operator);
            }
        }
    }
    
    public class InfixToPostfixParens {
        public static void convert(String s) {
          // Do something useful
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to call a method from another class with a simple button in
I'm trying to call a method from another class and from what I've gathered,
I am trying to call method getDetails() from another class which in turns calls
I am trying to call a method from another class with list but it
I'm trying to call a return type method from another class but I keep
I am trying to make a call to a Parent class method from a
I am trying to call a static method from another activity to find a
I'm trying to call a method of another class but this causes my app
I'm trying to use a method from within another method in a class. I
I am trying to use a method inside class, from another class. namespace Crystal.Utilities

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.