I get this error.What's wrong?
java.lang.NullPointerException: Attempt to invoice virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on an all object reference
The code is as follows:
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 extensions AppCompatActivity{
private TextView textView;
privateEditTexteditText;
private String fileName[] = new String[15];
private inti;
private Button Button Read [] = 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(newView.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(newView.OnClickListener(){
@ Override
public void onClick (View v) {
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=read.readLine())!=null){
str = str + tmp + "\n";
}
text = str;
reader.close();
} catch(IOExceptione){
e.printStackTrace();
}
return text;
}
}
Error Message: "java.lang.NullPointerException: Attempt to Invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on an all object reference"
[Direct translation] Empty pointer exception: Android.widget.Button.setOnClickListener method tried to call for reference to empty object
In the code of the question, you are trying to invoke setOnClickListener.
ButtonRead[i]=findViewById(viewId);
buttonRead[i].setOnClickListener(newView.OnClickListener(){
and
Button return_file_Button1=findViewById(R.id.return_file_button1);
return_file_Button1.setOnClickListener(newView.OnClickListener(){
There are two locations, so
findViewById(viewId);
and
findViewById(R.id.return_file_button1);
If you print the arguments in and the returned values, you will be able to determine where null was returned.
That is what caused the null object reference.
If you return a proper object reference, no exceptions should occur.
Please do your best to find out.
© 2024 OneMinuteCode. All rights reserved.