Android: My application crashes when there is an empty editText field

I have a problem with my code. It continues to crash when I have an empty editText field. This bit of code is in the settings of my application and works fine with data, but when there is an empty field, it causes a program crash. Here is the code for it. Please don't be harsh because this is my first Android app. Therefore, if anyone knows how to solve a problem with an empty edit box, that would be very helpful! (Any other comments on how to improve the application will be helpful.)

Greetings

package com.cleanyet.cyv100fp; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class sTasks extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.tasks); /*Sets Up the Variables*/ Button done = (Button) findViewById(R.id.done1); EditText t1 = (EditText)findViewById(R.id.tbTask1); EditText t2 = (EditText)findViewById(R.id.tbTask2); EditText t3 = (EditText)findViewById(R.id.tbTask3); EditText t4 = (EditText)findViewById(R.id.tbTask4); EditText t5 = (EditText)findViewById(R.id.tbTask5); String FILENAME1 = "stask1"; String FILENAME2 = "stask2"; String FILENAME3 = "stask3"; String FILENAME4 = "stask4"; String FILENAME5 = "stask5"; String task1 = null; String task2 = null; String task3 = null; String task4 = null; String task5 = null; String edit = "Edit Task"; /*Fixes the Blank Field bug*/ /*Sets up the file input*/ FileInputStream fis = null; /*Gets the tasks set previously*/ /*Task 1 set up*/ try { fis = openFileInput(FILENAME1); byte[] dataArray = new byte[fis.available()]; while (fis.read(dataArray) != -1){ task1 = new String(dataArray); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (task1.toString().length() < 0) { task1.toString(); t1.setText(task1); } else { t1.setText(edit); } /*Task 2 set up*/ try { fis = openFileInput(FILENAME2); byte[] dataArray = new byte[fis.available()]; while (fis.read(dataArray) != -1){ task2 = new String(dataArray); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (task2.toString().length() < 0) { task2.toString(); t2.setText(task2); } else { t2.setText(edit); } /*Task 3 set up*/ try { fis = openFileInput(FILENAME3); byte[] dataArray = new byte[fis.available()]; while (fis.read(dataArray) != -1){ task3 = new String(dataArray); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (task3.toString().length() < 0) { task3.toString(); t3.setText(task3); } else { t3.setText(edit); } /*Task 4 set up*/ try { fis = openFileInput(FILENAME4); byte[] dataArray = new byte[fis.available()]; while (fis.read(dataArray) != -1){ task4 = new String(dataArray); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (task4.toString().length() < 0) { task4.toString(); t4.setText(task4); } else { t4.setText(edit); } /*Task 5 set up*/ try { fis = openFileInput(FILENAME5); byte[] dataArray = new byte[fis.available()]; while (fis.read(dataArray) != -1){ task5 = new String(dataArray); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (task5.toString().length() < 0) { task5.toString(); t5.setText(task5); } else { t5.setText(edit); } /*When changes have been made and done is clicked*/ done.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub /*Sets up the Variables*/ EditText t1 = (EditText)findViewById(R.id.tbTask1); EditText t2 = (EditText)findViewById(R.id.tbTask2); EditText t3 = (EditText)findViewById(R.id.tbTask3); EditText t4 = (EditText)findViewById(R.id.tbTask4); EditText t5 = (EditText)findViewById(R.id.tbTask5); String tasks1 = t1.getText().toString(); String tasks2 = t2.getText().toString(); String tasks3 = t3.getText().toString(); String tasks4 = t4.getText().toString(); String tasks5 = t5.getText().toString(); String FILENAME1 = "stask1"; String FILENAME2 = "stask2"; String FILENAME3 = "stask3"; String FILENAME4 = "stask4"; String FILENAME5 = "stask5"; String task1 = tasks1; String task2 = tasks2; String task3 = tasks3; String task4 = tasks4; String task5 = tasks5; String edit = "Go to settings to make this task."; if (t1 != null){ t1.setText(edit); /*t2.setText(edit); t3.setText(edit); t4.setText(edit); t5.setText(edit);*/ }; /*Put if statement here to catch the empty field*/ /*Makes The Changes to the Tasks*/ try { FileOutputStream fos1 = openFileOutput(FILENAME1, Context.MODE_PRIVATE); fos1.write(task1.getBytes()); fos1.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { FileOutputStream fos2 = openFileOutput(FILENAME2, Context.MODE_PRIVATE); fos2.write(task2.getBytes()); fos2.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { FileOutputStream fos3 = openFileOutput(FILENAME3, Context.MODE_PRIVATE); fos3.write(task3.getBytes()); fos3.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { FileOutputStream fos4 = openFileOutput(FILENAME4, Context.MODE_PRIVATE); fos4.write(task4.getBytes()); fos4.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { FileOutputStream fos5 = openFileOutput(FILENAME5, Context.MODE_PRIVATE); fos5.write(task5.getBytes()); fos5.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } startActivity(new Intent("com.checkin.cyv100fp.settings")); } }); } } 
+4
source share
1 answer
 if (task1.toString().length() < 0) { task1.toString(); t1.setText(task1); } else { t1.setText(edit); } 

The above makes no sense.

First, task1 is a string, so there is no need to call toString() to convert it to one.

Secondly, your conditional statement ( if ) checks if task1 less than zero .... think about it.

Thirdly, if it has an impossible length less than zero, you call toString() again (without a variable to get an impossible result less than zero), and then try setting the text of your t1 EditText .

The likelihood that reading a file does not work (possibly because you only save lines later in the onClick(...) method). Since your task strings will be null , if the file reading fails, then you need to test null before trying to use them.

In other words, you do this in your code ...

 String task1 = null; 

To fix the bit of code that I concluded at the beginning, use ...

 if (task1 != null) { t1.setText(task1); } else { t1.setText(edit); } 

... but most importantly, make sure there are lines in your files that you need to read.

+2
source

Source: https://habr.com/ru/post/1393670/


All Articles