0
I am trying to implement a stack(database),the problem is that I am not able to send my data to the other Activitys of my application, so when I open another Activity to check if the data is there,she gives a nullPointerExepetion error,I have already tried to send the stack by reference to the other to Activity,but the error program likewise,can anyone help me? I want to be able to access data from my Activity independent stack that I am (without them being erased)
Main
public class Principal extends AppCompatActivity {
    private Button btn;
    private EditText texto;
    public BancodeDados DB = new BancodeDados();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_principal);
        texto = (EditText) findViewById(R.id.texto);
        btn = (Button) findViewById(R.id.btn);
        texto.setText("Insira um dado");
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String y = texto.getText().toString();
                DB.InserirDado(y);
                pagina2 receber = new pagina2();
                receber.recebe(DB);
                Toast.makeText(v.getContext(),"Enviado",Toast.LENGTH_LONG).show();
            }
        });
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }
    @Override
    public boolean onOptionsItemSelected (MenuItem item){
        Intent i = null;
        switch(item.getItemId()) {
            case R.id.mnTela1:
                i = new Intent(this, pagina2.class);
                Toast.makeText(this, "item1", Toast.LENGTH_LONG).show();
                startActivity(i);
                break;
            case R.id.mnTela2:
                Toast.makeText(this, "item2", Toast.LENGTH_LONG).show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
Secondary Activity
public class pagina2 extends AppCompatActivity{
    private EditText data;
    private Button btn;
    BancodeDados DataBase = new BancodeDados();
    public void recebe(BancodeDados DB){
        DataBase = DB;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pagina2);
        btn = (Button) findViewById(R.id.btn2);
        data = (EditText) findViewById(R.id.Data);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    data.setText(DataBase.removeDado());
            }
        });
    }
}
Stack(Bancoded)
public class BancodeDados {
    elemento primeiro;
    elemento ultimo;
    public void InserirDado(String x) {
        elemento novodadoinicio = new elemento();
        novodadoinicio.dado = x;
        novodadoinicio.proximo = primeiro;
        if(primeiro==null){
            ultimo = novodadoinicio;
        }
        primeiro = novodadoinicio;
    }
    public void InsereFinal(String x){
        elemento novodadoultimo = new elemento();
        novodadoultimo.dado = x;
        novodadoultimo.proximo = primeiro;
        if(ultimo==null){
            primeiro = novodadoultimo;
        }
        ultimo = novodadoultimo;
    }
    public String removeDado(){
        String x;
        x = primeiro.dado;
        primeiro = primeiro.proximo;
        return x;
    }
}
android log
03-28 01:05:01.431 20564-20564/com.project.meuapp2 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.project.meuapp2, PID: 20564
java.lang.NullPointerException: Attempt to read from field 'java.lang.String com.project.meuapp2.elemento.dado' on a null object reference
 at com.project.meuapp2.BancodeDados.removeDado(BancodeDados.java:43)
 at com.project.meuapp2.pagina2$1.onClick(pagina2.java:30)
 at android.view.View.performClick(View.java:5198)
 at android.view.View$PerformClick.run(View.java:21147)
 at android.os.Handler.handleCallback(Handler.java:739)
 at android.os.Handler.dispatchMessage(Handler.java:95)
 at android.os.Looper.loop(Looper.java:148)
 at android.app.ActivityThread.main(ActivityThread.java:5417)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Note:I’ve tried to create a text file in a folder within the android project, but when I call the java functions to create a file and read the file,it doesn’t read much less creates,and the Android log shows no errors
Rez, don’t need the java tag, android is java, is subtendido.
– user28595
was bad :S.....
– ReZ
(String x;) this statement is wrong, either you declare (String x = something) or you do a (new Stringbuilder();) concatenating the data.
– Eduardo