Android java.lang.IllegalStateException: Could not execute method of the activity

Android java.lang.IllegalStateException: Could not execute method of the activity

This line is throwing anNPE, producing an IllegalStateException in the exception chain:

int stopPortRange = Integer.parseInt(stopPort.getText().toString());

as you dont initialize stopPort (or startPort) in your onCreate method. You should have something like:

stopPort = (EditText) findViewById(R.id.stop_port_field);

The statement

EditText stopPort;

simply declares the variable which is null by default. Only instances that have been instantiated can have their methods invoked.

Kind of late reply but still, @Pheonix2105, As mentioned by Reimeus,

EditText startPort, stopPort;

just declares the variables. This does not initialize the values for these declared variables. Hence the value of these variables are currently null.

stopPort = (EditText) findViewById (R.id.stop_port_field)

initializes (or in your terms, ties the stopPort to a UI component) the value of stopPort, thereby avoiding the NullPointerException (which in turn causes the IllegalStatementException in your case).

Hope that clarifies your doubt.

Android java.lang.IllegalStateException: Could not execute method of the activity

Either

  • Didnt initialise a variable
    or
  • Initialised a variable and overwrote it when you were declaring them

Often people duplicate the same line in anticipation to change one variable name in each line, to try and make it faster for you, but might forget one line, or miss one word change

EditText stopPort;

That is your problem

Leave a Reply

Your email address will not be published.