0

I am building a simple calculator in android, i already made it work with simple x operator x form but i am trying to make it work with more than 2 tokens, and while using parenthesis.

I am using an add-on Library to do the interpretation. EDIT: doccumentation for Interpreter Class: http://www.beanshell.org/javadoc/bsh/Interpreter.html

I tried using this code in java standalone and it worked:

public double evaluate(String express) {
    double value = 0;
    String answer = null;

    String base = "result = ";
    Interpreter interpreter = new Interpreter();

    try {
        interpreter.eval(base + express); 
        answer = interpreter.get("result").toString();//this line
        value = Double.parseDouble(answer);
    } catch (EvalError e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

But when i used it in android, the toString() method is throwing a nullpointerexception, why is that? why in android specifically?

Tao
  • 1
  • 3
  • 1
    Simply put the object you are calling toString() on is null... – Larry McKenzie Jan 08 '14 at 18:44
  • I know that, but why this problem is happening in android only? – Tao Jan 08 '14 at 18:46
  • 1
    What does Interpreter do? what's the code? – Martin Marconcini Jan 08 '14 at 18:48
  • 1
    I don't think Interpreter is available in android as I search for it in android library does not find it there. Just for information, android takes some of java part, not the full of java part. – Mustakimur Khandaker Jan 08 '14 at 18:50
  • It's an addon library (Jar), you can find it here, http://www.beanshell.org/download.html the second link – Tao Jan 08 '14 at 18:54
  • check this post please http://stackoverflow.com/questions/3422673/evaluating-a-math-expression-given-in-string-form – Tao Jan 08 '14 at 18:58
  • Please edit your code to include your call to evaluate. Standing alone, this code is a method declaration with no invocation. The result will depend upon how you call evaluate. Can you include the smallest possible set of working software ? – OYRM Jan 08 '14 at 20:52

1 Answers1

8

But when I used it in Android, the toString() method is throwing a nullpointerexception

It is not. You even don't get into that method.

Because the interpreter.get() method returned null. It is like writing:

null.toString();

That is what happens.

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287