[JAVA] The Android app crashes. Just click a button and it will fall.

I get an error like this. What's wrong?

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

Below is the code.

package com.example.tokoroshingo.myapplication;

import android.content.Intent; import android.content.res.Resources; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.content.Context; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView;

import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader;

public class FileActivity1 extends AppCompatActivity {

private TextView textView;
private EditText editText;
private String fileName[] = new String[15] ;
private int i;
private Button buttonRead[] = new Button[15];

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_file1);

    textView = findViewById(R.id.text_view);


    for(i=0; i<15; i++){
        fileName[i] = "CH1_"+i+1+".txt";
        int viewId;
        String resViewName;
        resViewName = "button_ch" + i+1;
        viewId = getResources().getIdentifier(resViewName, "id", getPackageName());
        buttonRead[i] = findViewById(viewId);
        buttonRead[i].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str = readFile(fileName[i]);
                if (str != null) {
                    textView.setText(str);
                } else {
                    textView.setText(R.string.read_error);
                }
                TextView textReceive1 = (TextView) findViewById(R.id.text_view1);
                textReceive1.setText("CH"+ i);
            }
        });

    }


    //  editText = findViewById(R.id.edit_text);



    Button return_file_Button1 = findViewById(R.id.return_file_button1);
    return_file_Button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getApplication(), SubActivity5.class);
            startActivity(intent);
        }
    });


}

// read the file public String readFile(String file) { String text = null;

    try {
        FileInputStream in = openFileInput(file);
        BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
        String str = "";
        String tmp;
        while ((tmp = reader.readLine()) != null) {
            str = str + tmp + "\n";
        }
        text = str;
        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return text;
}

}

Recommended Posts

The Android app crashes. Just click a button and it will fall.
Click the [rails] button to create a random alphanumeric password and enter it in the password field
Get YouTube video information with Retrofit and keep it in the Android app.
21 Load the script from a file and execute it
Android app to select and display images from the gallery
Add a shadow to the Swift Button (and also the circle)
[Android] Change the app name and app icon for each Flavor
Is it an Android app?
I opened the menu bar (option menu) on Android and saw it.
Android Studio shows only one method for button onClick and Java throws an exception and ruins the app