Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null Object Reference

Asked

Viewed 293 times

1

I am developing an application that aims to consult prices and deadlines of orders consuming the Post Office service. However, I’m having trouble implementing the service in the application.When I put a Toast to see if I can capture the entries it returns an error stating that the variable is null. In case, all entries made with Textview are coming null, while checkbos and seekbar I can capture the entries, someone can help me solve this problem?

package com.example.diego.precoeprazocorreios;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

public class SegundaActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

private TextView vLarguraCX;
private TextView vAlturaCX;
private TextView vComprimentoCX;
private EditText vLarguraCX1 ;
private EditText vAlturaCX1;
private EditText vComprimentoCX1;
private SeekBar vPeso;
private Button btnProximo2;
private TextView kilo;
private TextView vlarguraEnv;
private TextView vComprimentoEnv;
private EditText vLarguraEnv1;
private EditText vComprimentoEnv1;
private TextView vDiametroRolo;
private EditText vDiametroRolo1;
private TextView vComprimentoRolo;
private EditText vComprimentoRolo1;
private RadioGroup RadioGroup1;
private TextView txtDeclaracao;
private EditText valorDeclaracao;
private int progress_value;
Encomenda enc = new Encomenda();

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

    RadioGroup1 = (RadioGroup)findViewById(R.id.rg);
    //Variáveis de entradas para o PACOTE
    vLarguraCX = (TextView)findViewById(R.id.larguraCaixa);
    vLarguraCX1 = (EditText) findViewById(R.id.larguraCaixa1);
    vAlturaCX = (TextView)findViewById(R.id.alturaCaixa);
    vAlturaCX1 = (EditText) findViewById(R.id.alturaCaixa1);
    vComprimentoCX = (TextView)findViewById(R.id.comprCaixa);
    vComprimentoCX1 = (EditText) findViewById(R.id.comprCaixa1);
    //Variáveis de entradas para o ENVELOPE
    vlarguraEnv = (TextView)findViewById(R.id.larguraEnv);
    vLarguraEnv1 = (EditText)findViewById(R.id.larguraEnv1);
    vComprimentoEnv = (TextView)findViewById(R.id.comprEnv);
    vComprimentoEnv1 = (EditText)findViewById(R.id.comprEnv1);
    //Variáveis de entradas para o ROLO
    vDiametroRolo = (TextView)findViewById(R.id.diametroRolo);
    vDiametroRolo1 = (EditText)findViewById(R.id.diametroRolo1);
    vComprimentoRolo = (TextView)findViewById(R.id.comprRolo);
    vComprimentoRolo1 = (EditText)findViewById(R.id.comprRolo1);
    //Checkbox e radioButtons
    txtDeclaracao = (TextView)findViewById(R.id.txtValorDeclarado);
    valorDeclaracao = (EditText)findViewById(R.id.valorDeclarado);
    btnProximo2 = (Button) findViewById(R.id.proximo2);
    final CheckBox chmaoPropria = (CheckBox)findViewById(R.id.maopropria);
    final CheckBox chDeclaracao = (CheckBox)findViewById(R.id.declaracao);
    final CheckBox chAR = (CheckBox)findViewById(R.id.AR);

    Intent it = getIntent();
    Encomenda encomenda = (Encomenda)it.getSerializableExtra("objeto1");
    String cepOrigem1 = encomenda.getCepOrigem();
    String cepDestino1 = encomenda.getCepDestino();

    enc.setCepOrigem(cepOrigem1);
    enc.setCepDestino(cepDestino1);
    RadioGroup1.setOnCheckedChangeListener(this);
    seebar();

    //Verifica se o checkbox Aviso de Recebimento foi clicado
    chAR.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(chAR.isChecked()){
                txtDeclaracao.setVisibility(View.VISIBLE);
                valorDeclaracao.setVisibility(View.VISIBLE);
                enc.setAvisoRecibo(valorDeclaracao.getText().toString());
            }else{
                txtDeclaracao.setVisibility(View.GONE);
                valorDeclaracao.setVisibility(View.GONE);
            }
        }
    });
    //Verifica se o checkbox Mão Própria foi clicado
    chmaoPropria.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(chmaoPropria.isChecked()){
                enc.setMaoPropria("S");
            }else if(!chmaoPropria.isChecked()){
                enc.setMaoPropria("N");
            }
        }
    });
    //Verifica se o checkbox Aviso de recebimento foi clicado
    chDeclaracao.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(chDeclaracao.isChecked()){
                enc.setAvisoRecibo("S");
            }else {
                enc.setAvisoRecibo("N");
            }
        }
    });
}

public void seebar(){

    vPeso = (SeekBar) findViewById(R.id.peso);
    kilo = (TextView)findViewById(R.id.kg);
    kilo.setText(vPeso.getProgress() + "kgs / " + vPeso.getMax() + "kgs");
    vPeso.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            progress_value = progress;
            kilo.setText(progress + "kgs / " + vPeso.getMax() + "kgs");
            enc.setPeso(String.valueOf(progress));
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            kilo.setText(progress_value + "kgs / " + vPeso.getMax() + "kgs");

        }
    });
}

public void onCheckedChanged(RadioGroup group, int checkedId) {

    //Verifica se o radioButtom Caixa foi clicado
    if(checkedId ==  R.id.caixa){
        //Exibe os campos referentes a caixa
        vLarguraCX.setVisibility(View.VISIBLE);
        vLarguraCX1.setVisibility(View.VISIBLE);
        vAlturaCX.setVisibility(View.VISIBLE);
        vAlturaCX1.setVisibility(View.VISIBLE);
        vComprimentoCX.setVisibility(View.VISIBLE);
        vComprimentoCX1.setVisibility(View.VISIBLE);
        //Oculta os demais campos
        vlarguraEnv.setVisibility(View.GONE);
        vLarguraEnv1.setVisibility(View.GONE);
        vComprimentoEnv.setVisibility(View.GONE);
        vComprimentoEnv1.setVisibility(View.GONE);
        vDiametroRolo.setVisibility(View.GONE);
        vDiametroRolo1.setVisibility(View.GONE);
        vComprimentoRolo.setVisibility(View.GONE);
        vComprimentoRolo1.setVisibility(View.GONE);

        String largura = vLarguraCX1.getText().toString();
        //double altura = Double.parseDouble(vAlturaCX1.getText().toString());
        //double comprimento = Double.parseDouble(vComprimentoCX1.getText().toString());

        enc.setFormato(1);
        enc.setLargura(largura);
        enc.setAltura(String.valueOf((vAlturaCX1.getText())));
        enc.setComprimento(String.valueOf(vComprimentoCX1.getText()));


        //Verifica se o radioButtom Envelope foi clicado
    }else if(checkedId == R.id.envelope) {
        //Exibe os campos referentes a envelope
        vlarguraEnv.setVisibility(View.VISIBLE);
        vLarguraEnv1.setVisibility(View.VISIBLE);
        vComprimentoEnv.setVisibility(View.VISIBLE);
        vComprimentoEnv1.setVisibility(View.VISIBLE);
        //Oculta os demais campos
        vLarguraCX.setVisibility(View.GONE);
        vLarguraCX1.setVisibility(View.GONE);
        vAlturaCX.setVisibility(View.GONE);
        vAlturaCX1.setVisibility(View.GONE);
        vComprimentoCX.setVisibility(View.GONE);
        vComprimentoCX1.setVisibility(View.GONE);
        vDiametroRolo.setVisibility(View.GONE);
        vDiametroRolo1.setVisibility(View.GONE);
        vComprimentoRolo.setVisibility(View.GONE);
        vComprimentoRolo1.setVisibility(View.GONE);

        enc.setFormato(2);
        enc.setLargura(vLarguraEnv1.getText().toString());
        enc.setComprimento(vComprimentoEnv1.getText().toString());


        //Verifica se o radioButtom Rolo foi clicado
    }else if(checkedId == R.id.rolo) {
        //Exibe os campos referentes a rolo
        vDiametroRolo.setVisibility(View.VISIBLE);
        vDiametroRolo1.setVisibility(View.VISIBLE);
        vComprimentoRolo.setVisibility(View.VISIBLE);
        vComprimentoRolo1.setVisibility(View.VISIBLE);
        //Oculta os demais campos
        vLarguraCX.setVisibility(View.GONE);
        vLarguraCX1.setVisibility(View.GONE);
        vAlturaCX.setVisibility(View.GONE);
        vAlturaCX1.setVisibility(View.GONE);
        vComprimentoCX.setVisibility(View.GONE);
        vComprimentoCX1.setVisibility(View.GONE);
        vlarguraEnv.setVisibility(View.GONE);
        vLarguraEnv1.setVisibility(View.GONE);
        vComprimentoEnv.setVisibility(View.GONE);
        vComprimentoEnv1.setVisibility(View.GONE);

        enc.setFormato(3);
        enc.setDiametro(vDiametroRolo1.getText().toString());
        enc.setComprimento(vComprimentoRolo1.getText().toString());
    }

    btnProximo2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent myIntent = new Intent(SegundaActivity.this, TerceiraActivity.class);
            myIntent.putExtra("objeto2", enc);
            Toast toast = Toast.makeText(getApplicationContext(), enc.getValorDeclarado().toString(), Toast.LENGTH_LONG);

            toast.show();
            startActivity(myIntent);
        }
    });
}

inserir a descrição da imagem aqui

  • Toast toast = Toast.makeText(getApplicationContext(), enc.getValorDeclarado().toString(), Toast.LENGTH_LONG);

  • This is where you’re with NPE. Do you know if getValorDeclarado can return a null?

  • this Toast I put in just to prove that I’m not actually getting the entries of Edittexts.

  • Removing it the error continues or does not?

  • if I take out Toast the error message disappears.. but I will have another error up ahead by not passing these values

  • by not capturing the values of height, width, length, therefore I will not have to carry out the query in the post office.

  • But the error happens by invoking toString into something empty

  • enc.setFormato(1);
 enc.setLargura(largura);
 enc.setAltura(String.valueOf((vAlturaCX1.getText())));
 enc.setComprimento(String.valueOf(vComprimentoCX1.getText())); I mean, this is where the real problem is

  • You can’t make a ternary that would stop that NPE?

Show 5 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.