0

Here is the MainActivity.java code of my calculator app:

package com.example.android.calculatorrecursion;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.ArrayList;

public class MainActivity extends ActionBarActivity {
    private String numString = "";
    private ArrayList<String> keyArrayList;
    EditText editTextView;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editTextView = (EditText)findViewById(R.id.textView);
    }

    //when number buttons are clicked.
    public void onButtonClick(View v) {
        button = (Button) v;
        numString += button.getText().toString();
        editTextView.setText(numString);
    }

    //operation buttons which will also add a space before and after the      operator.
    public void onButtonSpaceGenerator(View view)
    {
        button = (Button)view;
        numString += " ";
        numString += button.getText().toString();
        numString += " ";
        editTextView.setText(numString);
    }

    public void onClear(View v){
        keyArrayList.clear();
        numString = "";
        editTextView.setText("");
    }

    public void createArray(){
        String tempString;
        int index;
        int begin = 0;
        while(begin < numString.length())
        {
          if(numString.contains(" ")) {
            index = numString.indexOf(" ", begin);
            tempString = numString.substring(begin, index);
            keyArrayList.add(tempString);
            begin += index + 1;
           }
        }
    }

    public void equals(View v){
        createArray();
        double total = calcResult(keyArrayList);
        editTextView.setText(Double.toString(total));
        numString = "";
    }

    public double calcResult(ArrayList<String> tempKeyArrayListArrayList){
         double tempNum Double.parseDouble(tempKeyArrayListArrayList.get(0));
         tempKeyArrayListArrayList.remove(0);
         if(tempKeyArrayListArrayList.size() >= 1) {
            String operator = tempKeyArrayListArrayList.get(0);
            tempKeyArrayListArrayList.remove(0);
            if (operator.equals("+"))
                return tempNum + calcResult(tempKeyArrayListArrayList);
            if (operator.equals("*"))
                return tempNum * calcResult(tempKeyArrayListArrayList);
            if (operator.equals("-"))
                return tempNum - calcResult(tempKeyArrayListArrayList);
            if (operator.equals("/"))
                return tempNum / calcResult(tempKeyArrayListArrayList);
         }
      return tempNum;
     }
}

But, I currently have an error that says,

java.lang.IllegalStateException: Could not execute method of the activity
        at android.view.View$1.onClick(View.java:3679)
        at android.view.View.performClick(View.java:4203)
        at android.view.View$PerformClick.run(View.java:17189)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4950)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at android.view.View$1.onClick(View.java:3674)
        at android.view.View.performClick(View.java:4203)
        at android.view.View$PerformClick.run(View.java:17189)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4950)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.example.android.calculatorrecursion.MainActivity.createArray(MainActivity.java:57)
        at com.example.android.calculatorrecursion.MainActivity.equals(MainActivity.java:64)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at android.view.View$1.onClick(View.java:3674)
        at android.view.View.performClick(View.java:4203)
        at android.view.View$PerformClick.run(View.java:17189)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4950)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at   com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java
       :1004)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
        at dalvik.system.NativeStart.main(Native Method)

And i'm not sure why the computer throws the third error when i'm not trying to initialize any variable in that line of code. If anybody would be able to explain/clarify what is wrong with the code and what those first two errors mean, I would really appreciate it. Thanks!


The new error:

    java.lang.IllegalStateException: Could not execute method of the activity
        at android.view.View$1.onClick(View.java:3679)
        at android.view.View.performClick(View.java:4203)
        at android.view.View$PerformClick.run(View.java:17189)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4950)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at android.view.View$1.onClick(View.java:3674)
        at android.view.View.performClick(View.java:4203)
        at android.view.View$PerformClick.run(View.java:17189)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4950)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
        at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
        at java.util.ArrayList.get(ArrayList.java:304)
        at com.example.android.calculatorrecursion.MainActivity.calcResult(MainActivity.java:72)
        at com.example.android.calculatorrecursion.MainActivity.calcResult(MainActivity.java:82)
        at com.example.android.calculatorrecursion.MainActivity.equals(MainActivity.java:66)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at android.view.View$1.onClick(View.java:3674)
        at android.view.View.performClick(View.java:4203)
        at android.view.View$PerformClick.run(View.java:17189)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4950)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
        at dalvik.system.NativeStart.main(Native Method)
Voctave
  • 141
  • 7

1 Answers1

0

You're getting a NullPointerException because you never initialize keyArrayList.

The NPE happens on this line: keyArrayList.add(tempString);

Just initialize it in onCreate().

public class MainActivity extends ActionBarActivity {
    private String numString = "";
    private ArrayList<String> keyArrayList;
    EditText editTextView;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editTextView = (EditText)findViewById(R.id.textView);

        keyArrayList = new ArrayList<String>(); //added
    }

Suggested reading: What is a NullPointerException, and how do I fix it?

Community
  • 1
  • 1
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
  • Thank you! But now i'm getting an ArrayIndexOutOfBounds error on line 72 which doesn't make sense because the Arraylist has the 2 numbers and the operator in it. – Voctave Jul 09 '15 at 01:51
  • Which line is line 72 now? Is it `double tempNum Double.parseDouble(tempKeyArrayListArrayList.get(0));`? – Daniel Nugent Jul 09 '15 at 01:56
  • @ZivBrodie Well then `tempKeyArrayListArrayList` must be empty... Can you add your new error stacktrace to the end of your question, I'll take a look. – Daniel Nugent Jul 09 '15 at 02:01
  • @ZivBrodie Yep, it's empty: `IndexOutOfBoundsException: Invalid index 0, size is 0` Just debug your code, something itsn't right in the logic. Add lots of logging, log the size and contents of `tempKeyArrayListArrayList` after each operation. You'll figure it out! – Daniel Nugent Jul 09 '15 at 02:12