1
I’m developing an app that reads the barcode of an item and compares whether or not it exists in a list with database data. When any of these items is not present in the database it should save the data in an array and send them to another Activity where they will appear in a Spinner. When only one code needs to be sent it works ok. However, when I try to send two or more, the following error appears when trying to start the second Activity:
E/Androidruntime: FATAL EXCEPTION: main Process: com.weebly.wlhtech.confitem, PID: 24955 java.lang.Nullpointerexception: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null Object Reference
Follow the code of the two Acesvity:
Activity that reads and sends data:
public void compareData(String patrimonio) {
for(i=0;i<(lstItens.getAdapter().getCount());i++) {
Log.d("String ", lstItens.getItemAtPosition(i).toString());
if(patrimonio.contentEquals(lstItens.getItemAtPosition(i).toString())) {
Log.d("Resultado: igual", lstItens.getItemAtPosition(i).toString());
ok = true;
break;
} else {
Log.d("Resultado: diferente", patrimonio);
}
}
if(ok == true) {
Log.d("Resultado encontrado", String.valueOf(i));
} else {
Toast.makeText(this, "Patrimônio não encontrado.", Toast.LENGTH_SHORT).show();
naoencontrados = new String[100];
naoencontrados[posicao] = patrimonio;
posicao +=1;
}
android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);
builder.setTitle("Continuar conferência");
builder.setPositiveButton("Sim", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
camera();
}
});
builder.setNeutralButton("Finalizar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
scannerView.stopCameraPreview();
Intent confSetorResult = new Intent(ListaConf.this, confSetorResult.class);
confSetorResult.putExtra("nEncontrados", naoencontrados);
confSetorResult.putExtra("setor", setor);
startActivity(confSetorResult);
}
});
builder.setMessage("Deseja adicionar mais algum item?");
android.app.AlertDialog alert = builder.create();
alert.show();
Activity that receives the data:
package com.weebly.wlhtech.confitem;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class confSetorResult extends AppCompatActivity {
Spinner spnNEncontrados;
String setor;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_conf_setor_result);
spnNEncontrados = findViewById(R.id.spnNEncontrados);
//Pegar dados da outra Activity
Intent intent = getIntent();
String[] nEncontrados = intent.getStringArrayExtra("nEncontrados");
setor = intent.getStringExtra("setor");
//Popular Spinner
if(nEncontrados == null) {
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, 0);
spnNEncontrados.setAdapter(adapter);
} else {
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, nEncontrados);
spnNEncontrados.setAdapter(adapter);
}
}
public void alterar(View v) {
Intent altera = new Intent(this, AlteraSetor.class);
altera.putExtra("Setor", setor);
altera.putExtra("Patrimonio", spnNEncontrados.getSelectedItem().toString());
altera.putExtra("Nome", 0);
startActivity(altera);
}
}
Someone can help me What’s wrong?
If possible, indicate which part of the code, the cited error occurs.
– LSA
I edited the question. The error happens when starting Activity, it does not open the second screen.
– Nicolas Souza