NullPointerException error using Boolean type

0

I'm having problems again with this project, the application runs normally opens the screen for registration and everything else, but when you click the button to save it simply closes the app, but in the console of the firebase is registered the data ... I already analyzed and I did not find the error, does anyone know where this error is?

Thank you in advance ...

// Error:

  java.lang.NullPointerException: Attempt to invoke virtual method 
  'boolean    java.lang.Boolean.booleanValue()' on a null object reference at
   com.example.matheus.kypyy.MainActivity$2.onClick(MainActivity.java:98)

// User Class

public class Usuarios {

private String nome;
private String Data;
private String Rg;
private String Cpf;
private String Endereco;
private String Doenca;
private String Profissao;

public void Usuarios(){

}

public String getCpf() {
    return Cpf;
}

public void setCpf(String cpf) {
    Cpf = cpf;
}

public String getData() {
    return Data;
}

public void setData(String data) {
    Data = data;
}

public String getDoenca() {
    return Doenca;
}

public void setDoenca(String doenca) {
    Doenca = doenca;
}

public String getEndereco() {
    return Endereco;
}

public void setEndereco(String endereco) {
    Endereco = endereco;
}

public String getNome() {
    return nome;
}

public void setNome(String nome) {
    this.nome = nome;
}

public String getProfissao() {
    return Profissao;
}

public void setProfissao(String profissao) {
    Profissao = profissao;
}

public String getRg() {
    return Rg;
}

public void setRg(String rg) {
    Rg = rg;
 }
}

// BD Firebase class

public class FireHelper {
DatabaseReference bd;
Boolean saved;
ArrayList<Usuarios> usuarioss = new ArrayList<>();

public FireHelper(DatabaseReference bd) {
    this.bd = bd;
}
public Boolean save(Usuarios usuarios){
    if(usuarios == null){
        saved = false;
    }
    else
    {
        try{
            bd.child("Usuarios").push().setValue(usuarios);
        }catch (DatabaseException e){
            e.printStackTrace();
            saved = false;
        }
    }
    return saved;
}

private void fetchData(DataSnapshot dataSnapshot){
    usuarioss.clear();

    for(DataSnapshot ds : dataSnapshot.getChildren()){
        Usuarios usuarios = ds.getValue(Usuarios.class);
        usuarioss.add(usuarios);
    }
}

public ArrayList<Usuarios> retorno(){
    bd.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            fetchData(dataSnapshot);
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            fetchData(dataSnapshot);
        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
    return usuarioss;
 }
}

// Main class where you will display the data

 public class MainActivity extends AppCompatActivity {
DatabaseReference bd;
FireHelper helper;
AdapterCustom adapter;
ListView lv;
EditText textViewNome, textViewData, textViewRg, textViewCpf, textViewEndereco, textViewDoenca, textViewProfissao;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    //Banco de dados
    bd = FirebaseDatabase.getInstance().getReference();
    helper = new FireHelper(bd);
    //layout
    lv = (ListView) findViewById(R.id.lv);
    //Adaptador
    adapter = new AdapterCustom(this, helper.retorno());
    lv.setAdapter(adapter);


    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            displayInputDialog();
        }
    });
}



private void displayInputDialog() {
    Dialog d = new Dialog(this);
    d.setTitle("Salvar no banco de dados");
    d.setContentView(R.layout.input_dialog);

    //Declarando objetos
    final TextView textViewNome = (TextView) d.findViewById(R.id.editTextNome);
    final TextView textViewData = (TextView) d.findViewById(R.id.editTextData);
    final TextView textViewRg = (TextView) d.findViewById(R.id.editTextRg);
    final TextView textViewCpf = (TextView) d.findViewById(R.id.editTextCpf);
    final TextView textViewEndereco = (TextView) d.findViewById(R.id.editTextEndereco);
    final TextView textViewDoenca = (TextView) d.findViewById(R.id.editTextDoenca);
    final TextView textViewProfissao = (TextView) d.findViewById(R.id.editTextProfissao);

    Button saveButton = (Button) d.findViewById(R.id.savebutton);


    //Salvar...
    saveButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String nome = textViewNome.getText().toString();
            String data = textViewData.getText().toString();
            String rg = textViewRg.getText().toString();
            String cpf = textViewCpf.getText().toString();
            String endereco = textViewEndereco.getText().toString();
            String doenca = textViewDoenca.getText().toString();
            String profissao = textViewProfissao.getText().toString();
            Usuarios u = new Usuarios();
            u.setNome(nome);
            u.setData(data);
            u.setRg(rg);
            u.setCpf(cpf);
            u.setEndereco(endereco);
            u.setDoenca(doenca);
            u.setProfissao(profissao);

            if (nome != null && nome.length() > 0) {
                if (helper.save(u)) {
                    textViewNome.setText("");
                    textViewData.setText("");
                    textViewRg.setText("");
                    textViewCpf.setText("");
                    textViewEndereco.setText("");
                    textViewDoenca.setText("");
                    textViewProfissao.setText("");

                    adapter = new AdapterCustom(MainActivity.this, helper.retorno());
                    lv.setAdapter(adapter);

                } else {
                    Toast.makeText(MainActivity.this, "sem dados", Toast.LENGTH_SHORT).show();
                }
            }
        }
    });
    d.show();
  }

}

// Adapter Class

public class AdapterCustom extends BaseAdapter {
Context c;
ArrayList<Usuarios> usuarioss;

public AdapterCustom(Context c, ArrayList<Usuarios> usuarioss) {
    this.c = c;
    this.usuarioss = usuarioss;
}

@Override
public int getCount() {
    return usuarioss.size();
}

@Override
public Object getItem(int position) {
    return usuarioss.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(convertView== null){
        convertView = LayoutInflater.from(c).inflate(R.layout.model,parent,false);
    }
    //Declarando objetos
    TextView textViewNome = (TextView)convertView.findViewById(R.id.textViewNome);
    TextView textViewData = (TextView)convertView.findViewById(R.id.textViewData);
    TextView textViewRg = (TextView)convertView.findViewById(R.id.textViewRg);
    TextView textViewCpf = (TextView)convertView.findViewById(R.id.textViewCpf);
    TextView textViewEndereco = (TextView)convertView.findViewById(R.id.textViewEndereco);
    TextView textViewDoenca = (TextView)convertView.findViewById(R.id.textViewDoenca);
    TextView textViewProfissao = (TextView)convertView.findViewById(R.id.textViewProfissao);

    final Usuarios u = (Usuarios) this.getItem(position);

    textViewNome.setText(u.getNome());
    textViewData.setText(u.getData());
    textViewRg.setText(u.getRg());
    textViewCpf.setText(u.getCpf());
    textViewEndereco.setText(u.getEndereco());
    textViewDoenca.setText(u.getDoenca());
    textViewProfissao.setText(u.getProfissao());

    //Função onclick

    convertView.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Toast.makeText(c, u.getNome(), Toast.LENGTH_SHORT).show();
        }
    });



    return convertView;
 }
}
    
asked by anonymous 23.10.2016 / 17:58

2 answers

4

The saved attribute is declared to be of type Boolean .

Unlike the boolean primitive type, which is automatically initialized with the false value, the Boolean type (uppercase B) is null , if it is not initialized or instantiated.

The reason for the error is the same: you are accessing a method ( booleanValue() ) of the Boolean class in an object that is null.

From what I see in the code, nothing indicates that you need to use the class instead of the primitive type.

So, change the declaration of saved from

Boolean saved;

for

boolean saved;

I also see no reason to give the error that indicates.

This error would be thrown if you used saved , before initializing it, in situations like these:

if(saved){
    ...
    ...
}

or

boolean qualquerCoisa = saved;

I can not find any of these (or other) cases in your code, I only see initializations.

    
24.10.2016 / 15:22
1

Try to put a default value for your saved variable:

private Boolean saved = true;
    
24.10.2016 / 14:29