I wanted to convert this segment of code into something slightly different

package expression;

import java.util.*;

public class Expression{
    /*
    * Strings used for storing expression.
    */
    String s, x;

    /*
    * Term evaluator for number literals.
    */
    double term(){
        double ans = 0;
        StringBuffer temp = new StringBuffer();
        while( s.length() > 0 && Character.isDigit( s.charAt( 0 ) ) ){
            temp.append(Integer.parseInt( "" + s.charAt( 0 ) ));
            s = s.substring( 1 );
        }
        if( s.length() > 0 && s.charAt( 0 ) == '.' ){
            temp.append( '.' );
            s = s.substring( 1 );
            while( s.length() > 0 && Character.isDigit( s.charAt( 0 ) ) ){
                temp.append(Integer.parseInt( "" + s.charAt( 0 ) ));
                s = s.substring( 1 );
            }
        }
        if( s.length() > 0 && (s.charAt(0) == 'e' || s.charAt(0) == 'E') ){
            temp.append( 'e' );
            s = s.substring( 1 );
            temp.append( s.charAt( 0 ) );
            s = s.substring( 1 );
            while( s.length() > 0 && Character.isDigit( s.charAt( 0 ) ) ){
                temp.append(Integer.parseInt( "" + s.charAt( 0 ) ));
                s = s.substring( 1 );
            }
        }
        ans = Double.valueOf( temp.toString() ).doubleValue();
        return ans;
    }

    /*
    * Parentheses solver.
    */
    double paren(){
        double ans;
        if( s.charAt( 0 ) == '(' ){
            s = s.substring( 1 );
            ans = add();
            s = s.substring( 1 );
        } else {
            ans = term();
        }
        return ans;
    }

    /*
    * Exponentiation solver.
    */
    double exp(){
        boolean neg = false;
        if( s.charAt( 0 ) == '-' ){
            neg = true;
            s = s.substring( 1 );
        }
        double ans = paren();
        while( s.length() > 0 ){
            if( s.charAt( 0 ) == '^' ){
                s = s.substring( 1 );
                boolean expNeg = false;
                if( s.charAt( 0 ) == '-' ){
                    expNeg = true;
                    s = s.substring( 1 );
                }
                double e = paren();
                if( ans < 0 ){       // if it's negative
                    double x = 1;
                    if( Math.ceil(e) == e ){    // only raise to an integer
                        if( expNeg )
                            e *= -1;
                        if( e == 0 )
                            ans = 1;
                        else if( e > 0 )
                            for( int i = 0; i < e; i++ )
                                x *= ans;
                        else
                            for( int i = 0; i < -e; i++ )
                                x /= ans;
                        ans = x;
                    } else {
                        ans = Math.log(-1); // otherwise make it NaN
                    }
                }
                else if( expNeg )
                    ans = Math.exp( -e*Math.log( ans ) );
                else
                    ans = Math.exp( e*Math.log( ans ) );
            } else
                break;
        }
        if( neg )
            ans *= -1;
        return ans;
    }

    /*
    * Trigonometric function solver.
    */
    double trig(){
        double ans = 0;
        boolean found = false;
        if( s.indexOf( "sin" ) == 0 ){
            s = s.substring( 3 );
            ans = Math.sin( trig() );
            found = true;
        } else if( s.indexOf( "cos" ) == 0 ){
            s = s.substring( 3 );
            ans = Math.cos( trig() );
            found = true;
        } else if( s.indexOf( "tan" ) == 0 ){
            s = s.substring( 3 );
            ans = Math.tan( trig() );
            found = true;
        }
        if( !found ){
            ans = exp();
        }
        return ans;
    }

    /*
    * Multiplication, division expression solver.
    */
    double mul(){
        double ans = trig();
        if( s.length() > 0 ){
            while( s.length() > 0 ){
                if( s.charAt( 0 ) == '*' ){
                    s = s.substring( 1 );
                    ans *= trig();
                } else if( s.charAt( 0 ) == '/' ){
                    s = s.substring( 1 );
                    ans /= trig();
                } else break;
            }
        }
        return ans;
    }

    /*
    * Addition, subtraction expression solver.
    */
    double add(){
        double ans = mul();
        while( s.length() > 0 ){
            if( s.charAt( 0 ) == '+' ){
                s = s.substring( 1 );
                ans += mul();
            } else if( s.charAt( 0 ) == '-' ){
                s = s.substring( 1 );
                ans -= mul();
            } else{
                break;
            }
        }
        return ans;
    }

    /*
    * Public access method to evaluate this expression.
    */
    public double evaluate(){
        s = x.intern();
    double  last = add();
        return last;
    }

    /*
    * Creates new Expression.
    */
    public Expression( String s ){
        // remove white space, assume only spaces or tabs
        StringBuffer b = new StringBuffer();
        StringTokenizer t = new StringTokenizer( s, " " );
        while( t.hasMoreElements() )
            b.append( t.nextToken() );
        t = new StringTokenizer( b.toString(), "\t" );
        b = new StringBuffer();
        while( t.hasMoreElements() )
            b.append( t.nextToken() );

        x = b.toString();
    }

    /*
    * The String value of this Expression.
    */
    public String toString(){
        return x.intern();
    }

    /*
    * Test our Expression class by evaluating the command-line
    * argument and then returning.
    */



/* 

 *** to use without GUI uncomment bellow and comment the ExpressionApplet ***   

        */

    public static void main( String[] args ){

        Scanner sc=new Scanner(System.in);


      Expression e = new Expression( sc.nextLine() );
System.out.println( e + " = " + e.evaluate() );


    }

}

i converted it into this
class Opration.java

abstract public class Opration {

    public String s, x;

public double term() {
        double ans = 0;
        return ans;
    }

    public double paren() {
        double ans;
        if (s.charAt(0) == '(') {
            s = s.substring(1);
            ans = add();
            s = s.substring(1); // we assume this is a ')'
        } else {
            ans = term();
        }
        return ans;
    }

    public double add() {
        double ans = term();
        while (s.length() > 0) {
            if (s.charAt(0) == '+') {
                s = s.substring(1);
                ans += term();
            } else if (s.charAt(0) == '-') {
                s = s.substring(1);
                ans -= term();
            } else {
                break;
            }
        }
        return ans;
    }

    public  double exp() {
        double ans = term();
        return ans;
    }

    public double trig() {
        double ans = 0;
        return ans;
    }

    public  double mul() {
        double ans = term();
        return ans;
    }

   public double evaluate(){
        s = x.intern();
        double last = add();
        return last;
    }

}

Expression.java

   public class Expression {

/*
 * Strings used for storing expression.
 */

String s, x;
/*
 * Term evaluator for number literals.
 */
Opration opration = new Opration() {

    @Override
    public double term() {
        double ans = 0;
        StringBuffer temp = new StringBuffer();
        while (s.length() > 0 && Character.isDigit(s.charAt(0))) {
            temp.append(Integer.parseInt("" + s.charAt(0)));
            s = s.substring(1);
        }
        if (s.length() > 0 && s.charAt(0) == '.') {
            temp.append('.');
            s = s.substring(1);
            while (s.length() > 0 && Character.isDigit(s.charAt(0))) {
                temp.append(Integer.parseInt("" + s.charAt(0)));
                s = s.substring(1);
            }
        }
        if (s.length() > 0 && (s.charAt(0) == 'e' || s.charAt(0) == 'E')) {
            temp.append('e');
            s = s.substring(1);
            temp.append(s.charAt(0));
            s = s.substring(1);
            while (s.length() > 0 && Character.isDigit(s.charAt(0))) {
                temp.append(Integer.parseInt("" + s.charAt(0)));
                s = s.substring(1);
            }
        }
        ans = Double.valueOf(temp.toString()).doubleValue();
        return ans;
    }

    @Override
    public double paren() {
        double ans;
        if (s.charAt(0) == '(') {
            s = s.substring(1);
            ans = add();
            s = s.substring(1); // we assume this is a ')'
        } else {
            ans = term();
        }
        return ans;
    }

    @Override
    public double exp() {
        boolean neg = false;
        if (s.charAt(0) == '-') {
            neg = true;
            s = s.substring(1);
        }
        double ans = term();
        while (s.length() > 0) {
            if (s.charAt(0) == '^') {
                s = s.substring(1);
                boolean expNeg = false;
                if (s.charAt(0) == '-') {
                    expNeg = true;
                    s = s.substring(1);
                }
                double e = term();
                if (ans < 0) {       // if it's negative
                    double x = 1;
                    if (Math.ceil(e) == e) {    // only raise to an integer
                        if (expNeg) {
                            e *= -1;
                        }
                        if (e == 0) {
                            ans = 1;
                        } else if (e > 0) {
                            for (int i = 0; i < e; i++) {
                                x *= ans;
                            }
                        } else {
                            for (int i = 0; i < -e; i++) {
                                x /= ans;
                            }
                        }
                        ans = x;
                    } else {
                        ans = Math.log(-1); // otherwise make it NaN
                    }
                } else if (expNeg) {
                    ans = Math.exp(-e * Math.log(ans));
                } else {
                    ans = Math.exp(e * Math.log(ans));
                }
            } else {
                break;
            }
        }
        if (neg) {
            ans *= -1;
        }
        return ans;
    }

    @Override
    public double trig() {
        double ans = 0;
        boolean found = false;
        if (s.indexOf("sin") == 0) {
            s = s.substring(3);
            ans = Math.sin(trig());
            found = true;
        } else if (s.indexOf("cos") == 0) {
            s = s.substring(3);
            ans = Math.cos(trig());
            found = true;
        } else if (s.indexOf("tan") == 0) {
            s = s.substring(3);
            ans = Math.tan(trig());
            found = true;
        }
        if (!found) {
            ans = term();
        }
        return ans;
    }

    @Override
    public double mul() {
        double ans = term();
        while (s.length() > 0) {
            if (s.charAt(0) == '*') {
                s = s.substring(1);
                ans *= term();
            } else if (s.charAt(0) == '/') {
                s = s.substring(1);
                ans /= term();
            } else {
                break;
            }
        }
        return ans;
    }

    @Override
    public double add() {
        double ans = term();
        while (s.length() > 0) {
            if (s.charAt(0) == '+') {
                s = s.substring(1);
                ans += term();
            } else if (s.charAt(0) == '-') {
                s = s.substring(1);
                ans -= term();
            } else {
                break;
            }
        }
        return ans;
    }

    @Override
    public double evaluate() {
        s = x.intern();
        return term();
    }
};

/*
 * Parentheses solver.
 */
/*
 * Exponentiation solver.
 */

/*
 * Trigonometric function solver.
 */
/*
 * Multiplication, division expression solver.
 */
/*
 * Addition, subtraction expression solver.
 */
/*
 * Public access method to evaluate this expression.
 */
/*
 * Creates new Expression.
 */
public Expression(String s) {
    // remove white space, assume only spaces or tabs
    StringBuffer b = new StringBuffer();
    StringTokenizer t = new StringTokenizer(s, " ");
    while (t.hasMoreElements()) {
        b.append(t.nextToken());
    }
    t = new StringTokenizer(b.toString(), "\t");
    b = new StringBuffer();
    while (t.hasMoreElements()) {
        b.append(t.nextToken());
    }

    x = b.toString();
}

/*
 * The String value of this Expression.
 */
@Override
public String toString() {
    return x.intern();
}

}

This is the Applet

package expression;

/**
 *
 * @author Sumal
 */
import java.awt.*;
import java.awt.event.*;

public class ExpressionApplet extends java.applet.Applet {

    TextField input;
    TextField output;
    Opration opration = new Opration() {
    };

    @Override
    public void init() {



        input = new TextField("3 + 4^2*7.5E-1*sin(22)");
        output = new TextField(23);
        Button b = new Button("Evaluate");
        b.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Expression exp = new Expression(input.getText());
                output.setText("" + opration.evaluate());
            }
        });
        input.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Expression exp = new Expression(input.getText());
                output.setText("" + opration.evaluate());
            }
        });

        add(input);
        add(b);
        add(output);
    }
}

why do i get a null point exception at this line ?

   public double evaluate(){
        s = x.intern();
        double last = add();
        return last;

cane some one please help me out ?

and please help me fix

public double term() {
        double ans = 0;
        return ans;
    }

i know this is alot to ask but would mean alot if some1 could help me out.

OK, you have posted about 500 lines of code, including some very long and very nested logic. It's unlikely that anyone will have the time or inclination to study all that just to fix an NPE. If you want general advice about how you are structuring your code you need to provide a much smaller example for people to work with, and be as specific as possible in your questions.
As for the NPE: which line? You say "at this line" then post 4 lines. But looking at it, the only way to get an NPE thrown in those 4 lines is if x is null.
Finally, what exactly do you want "fixed" with your term method?

You will find plenty of help in this forum, but you need to help us help you by being as specific as possible in your questions.

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.