The program for solving square, biquadratic equations. There is one activity and 4 fragments. When the program starts, the main activity loads the first fragment. On the first fragment, everything is solved perfectly. When switching to fragment 2 (biquadratic equations) and pressing the "solve" button, the application crashes with the following error:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference at helmus.myapplication.MainActivity$1.onClick(MainActivity.java:115), 

where 115 line is:

 xa.setText("Корней нет"); 

+ On top of that, if you click on the "clear" button at startup (erases all entered and set data from the text fields), the program crashes. Before

  @Override public void onClick(View view) { 

is coming

 FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { 

activit

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.setDrawerListener(toggle); toggle.syncState(); NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this); @Override public void onClick(View view) { a1 = (EditText) findViewById(R.id.editText); b1 = (EditText) findViewById(R.id.editText2); c1 = (EditText) findViewById(R.id.editText3); d1 = (EditText) findViewById(R.id.editText4); xa = (EditText) findViewById(R.id.editText5); xb = (EditText) findViewById(R.id.editText6); xa1 = (EditText) findViewById(R.id.editText7); xa2 = (EditText) findViewById(R.id.editText8); xa3 = (EditText) findViewById(R.id.editText9); xa4 = (EditText) findViewById(R.id.editText10); double x1 = 0, x2 = 0, x11 = 0, x22 = 0, x33 = 0, x44 = 0; if ((a1.getText().toString().equals("")) & (b1.getText().toString().equals("")) & (c1.getText().toString().equals(""))) { Toast toast = Toast.makeText(getApplicationContext(), "Введи хотя бы 1 коэффициент :)", Toast.LENGTH_SHORT); toast.show(); } else { if (a1.getText().toString().equals("")) { a = 1; a1.setText("1"); } else { a = Double.parseDouble(a1.getText().toString()); } if (b1.getText().toString().equals("")) { b = 1; b1.setText("1"); } else { b = Double.parseDouble(b1.getText().toString()); } if (c1.getText().toString().equals("")) { c = 0; c1.setText("0"); } else { c = Double.parseDouble(c1.getText().toString()); } double d = (b * b - 4 * a * c); String discr = String.valueOf(d); d1.setText(discr); if (d < 0) { xa.setText("Корней нет"); xb.setText("Корней нет"); xa1.setText("Корней нет"); xa2.setText("Корней нет"); xa3.setText("Корней нет"); xa4.setText("Корней нет"); } else { x1 = ((-1) * b + Math.sqrt(d)) / (2 * a); x2 = ((-1) * b - Math.sqrt(d)) / (2 * a); x11 = (Math.sqrt(((-1) * b - Math.sqrt(d)) / (2 * a))); x22 = (-1) * (Math.sqrt(((-1) * b - Math.sqrt(d)) / (2 * a))); x33 = (Math.sqrt(((-1) * b + Math.sqrt(d)) / (2 * a))); x44 = ((-1) * (Math.sqrt(((-1) * b + Math.sqrt(d)) / (2 * a)))); String coren1 = String.valueOf(x1); String coren2 = String.valueOf(x2); String coren11 = String.valueOf(x11); String coren22 = String.valueOf(x22); String coren33 = String.valueOf(x33); String coren44 = String.valueOf(x44); xa.setText(coren1); xb.setText(coren2); xa1.setText(coren11); xa2.setText(coren22); xa3.setText(coren33); xa4.setText(coren44); } Snackbar.make(view, "Уравнение решено :)", Snackbar.LENGTH_SHORT) .setAction("Action", null).show();} } }); FloatingActionButton fab2 = (FloatingActionButton) findViewById(R.id.fab2); fab2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { d1.setText(null); xa.setText(null); xb.setText(null); a1.setText(null); b1.setText(null); c1.setText(null); xa1.setText(null); xa2.setText(null); xa3.setText(null); xa4.setText(null); } }); f_quad = new BlankFragment(); f_biquad = new BlankFragment_1(); f_triquad = new BlankFragment_2(); f_teor = new BlankFragment_3(); f_trans = getSupportFragmentManager().beginTransaction(); f_trans.add(R.id.cont, f_quad).commit(); 
  • one
    At the moment, nothing is clear, because you have xa null . Is it in the fragment? If yes, then there is no need to refer to the markup of the fragment from the activation. - Yuriy SPb
  • such code will not compile. You lost something between your IDE and RU.SO - Vladyslav Matviienko
  • @YuriSPb, yes, xa, xb, xa1 ... xa4, are in fragments. If they cannot be accessed from the activit, then what to do in this case? Work as Visibility Parameters? - Igor Gorokhov

2 answers 2

As you can on fab2.setOnClickListener(new View.OnClickListener() contact your EditText if they are initialized in navigationView.setNavigationItemSelectedListener(this); when you click on fab2 you most likely have EditText not yet initialized.

  • Thanks, corrected by the banal copying of the initialization of EditText in onClick of the second fab. True, this is the answer to the second problem, deciding which, I ran into the first: when I click on fab2 now, the program crashes with an indication of xa.settext (null); - Igor Gorokhov 6:26 pm
  • setText (null) replace with setText ("") - Alexey Soloviev
  • Introduce EditText globally into Activity and initialize them in the body of the onCreate method. - Alexey Soloviev

The transfer of all computations to Java classes of all fragments and reference to fragment markup from there helped. Forgive the stupidity of a beginner and thanks, @Yuriy SPb)