I have an error that I do not understand because it shows, I have two classes one is the class called Dialogue that extends from a DialogFragment, in this class I have a method called onClickSavePersona , this class uses it to create a dialog box but at the moment of clicking on the button that executes the onClickGuardPersona method it marks me an error:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.FileOutputStream android.app.Activity.openFileOutput (java.lang.String, int) 'on a null object reference atcom.example.enriq.myapplication.ServicioArchivo.guardar (ServicioArchivo.java:41)
From the Dialog class, the onClickSavePersona method is executed. This method calls the FileService class to execute the Save method, that's where it marks the error.
This is the full Dialog class, the onClickSavePersona method is at the end of the class:
package com.example.enriq.myapplication;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.example.enriq.myapplication.adapter.NotaAdapter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
/**
* Created by Enriq on 30/01/2018.
*/
public class Dialogo extends android.support.v4.app.DialogFragment {
private ArrayList<Registro_parqueo> lista;
private String archivo = "parquimetro.obj";
View view;
private RecyclerView notasRecyclerView;
private EditText editNota;
private NotaAdapter adapter;
private ServicioArchivo servicio = new ServicioArchivo(getActivity());
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View MyView = inflater.inflate(R.layout.dialog_signin, null);
final EditText matricula = (EditText)MyView.findViewById(R.id.parqueo);
final EditText clienesillo = (EditText)MyView.findViewById(R.id.cliente);
builder.setView(MyView)
.setPositiveButton("Registrar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
onClickGuardarPersona(matricula.getText().toString(),clienesillo.getText().toString());
}
})
.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Dialogo.this.getDialog().cancel();
}
});
return builder.create();
}
public void onClickGuardarPersona(String matricula, String clienesillo) {
try {
servicio.agregarNota(matricula+"\n"+clienesillo);
adapter.setNotas(Arrays.asList(servicio.leerNotas()));
adapter.notifyDataSetChanged();
Toast.makeText(getContext(), "Nota agregada con éxito", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(getContext(), "Error al agregar la nota", Toast.LENGTH_SHORT).show();
}
}
}
The class where the save method is:
package com.example.enriq.myapplication;
import android.app.Activity;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Created by Next University.
*/
public class ServicioArchivo {
private Activity activity;
private String notas="";
private static final String FILE_NAME = "notas.txt";
public ServicioArchivo(Activity activity){
this.activity = activity;
}
public void agregarNota(String nota) throws IOException {
notas = notas.concat("".equals(notas) ? "" : ";");
notas = notas.concat(nota);
guardar();
}
public String[] leerNotas() throws IOException {
cargar();
return notas.split(";");
}
public String leerNotas(int posicion) throws IOException {
cargar();
String[] listaNotas = notas.split(";");
return listaNotas[posicion];
}
private void guardar() throws IOException {
FileOutputStream fos = activity.openFileOutput(FILE_NAME, Activity.MODE_PRIVATE);
fos.write(notas.getBytes());
fos.close();
}
private void cargar() throws IOException {
FileInputStream fis = activity.openFileInput(FILE_NAME);
int c;
notas = "";
while ( (c = fis.read()) != -1) notas += String.valueOf((char)c);
fis.close();
}
public void eliminar(){
activity.deleteFile(FILE_NAME);
notas="";
}
}