The application crashes here:

names = new String[NewArithm.index]; for (int i = 0; i <= NewArithm.index; i++){ names[i] = NewArithm.resultNumber[0 + i][0] + " " + NewArithm.resultNumber[0 + i][1]; } 

Full MainActivity code:

 package home.examapp; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { int firstNumber; int secondNumber; EditText editFirst; EditText editSecond; String[] names; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editFirst = (EditText)findViewById(R.id.One); editSecond = (EditText)findViewById(R.id.Two); } public void onOKClick(View v) { firstNumber = Integer.valueOf(editFirst.getText().toString()); secondNumber = Integer.valueOf(editSecond.getText().toString()); //Arithmetic NewArithm = new Arithmetic(); Arith NewArithm = new Arith(); NewArithm.setFirst(firstNumber); NewArithm.setSecond(secondNumber); names = new String[NewArithm.index]; for (int i = 0; i <= NewArithm.index; i++){ names[i] = NewArithm.resultNumber[0 + i][0] + " " + NewArithm.resultNumber[0 + i][1]; } // находим список ListView lvMain = (ListView) findViewById(R.id.lvMain); // создаем адаптер ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, names); // присваиваем адаптер списку lvMain.setAdapter(adapter); } } 

Errors:

 java.lang.IllegalStateException: Could not execute method for android:onClick at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293) at android.view.View.performClick(View.java:4780) at android.view.View$PerformClick.run(View.java:19866) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) at android.view.View.performClick(View.java:4780) at android.view.View$PerformClick.run(View.java:19866) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) Caused by: java.lang.NullPointerException: Attempt to read from null array at home.examapp.MainActivity.onOKClick(MainActivity.java:41) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) at android.view.View.performClick(View.java:4780) at android.view.View$PerformClick.run(View.java:19866) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

Arith Class:

 package home.examapp; public class Arith { private int first; private int second; public void setFirst(int number){ this.first = number; } public int getFirst(){ return this.first; } public void setSecond(int number){ this.second = number; } public int getSecond(){ return this.second; } public int[][] resultNumber; public int index = 0; public void createResultNumber() { for (int i = 0; i <= second; i++) for (int j = 0; j <=second; j++) if(Math.pow(i, 3) + Math.pow(j, 3) <= second){ resultNumber[0 + this.index][0] = i; resultNumber[0 + this.index][1] = j; this.index++; } } } 

    1 answer 1

    Of the timeline:

     Caused by: java.lang.NullPointerException: Attempt to read from null array 

    stack-tracing it becomes clear that in the line of code:

     names[i] = NewArithm.resultNumber[0 + i][0] + " " + NewArithm.resultNumber[0 + i][1]; 

    array NewArithm.resultNumber == null , hence the NullPointerException .

    This problem can be solved by creating the array NewArithm.resultNumber .

    UPD:

    In the createResultNumber() method, you initialize the elements of the array, but you do not create the resultNumber itself.

    Try this:

     public void createResultNumber() { resultNumber = new int[second][second]; for (int i = 0; i <= second; i++) { for (int j = 0; j <=second; j++) { if (Math.pow(i, 3) + Math.pow(j, 3) <= second) { resultNumber[0 + this.index][0] = i; resultNumber[0 + this.index][1] = j; this.index++; } } } } 

    And yes, the createResultNumber() method is not called anywhere.

    • Added code with a class where exactly the resultNumber array is filled - Kazer
    • I understood the mistake. A bit dumb that she did not notice. Thanks for the help - Kazer