java.lang.Nullpointerexception: Attempt to read from field 'java.lang.String

Asked

Viewed 923 times

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.

  • was bad :S.....

  • (String x;) this statement is wrong, either you declare (String x = something) or you do a (new Stringbuilder();) concatenating the data.

2 answers

5

The Error shows the following :

to read from field 'java.lang.String com.project.meuapp2.elemento.dado' on a null object reference

I mean, an object is null! Following the stack, the error occurs when there is a click on pagina2, when he calls the BancodeDados.removeDado

 at com.project.meuapp2.BancodeDados.removeDado(BancodeDados.java:43)
 at com.project.meuapp2.pagina2$1.onClick(pagina2.java:30)
 public String removeDado(){
        String x;
        x = primeiro.dado;
        primeiro = primeiro.proximo;
        return x;
    }

When you call this method the object primeiro he is null! It is necessary to instantiate it before calling your property!

1

To pass the information from one Activity to another, what you must do is add information to the info that opens that Activity.

This can be achieved with the Intent.putExtra(TAG, information) method. Below is an example:

    Intent intent = new Intent(this, pagina2.class);
    String mensagem = "Olá Mundo!";
    intent.putExtra("MINHA_MENSAGEM", message);
    startActivity(intent);

In the Activity that receives the information, you must access this information in the getIntent() method. getExtras(), which returns a Bundle (map that contains the information sent in the Intent). Following example:

    Intent intent = getIntent();
    String mensagem = intent.getStringExtra("MINHA_MENSAGEM");

NOTE: To send an object it is necessary to send it as a parelable or a serializable. A useful library to achieve this result is the one available in https://github.com/johncarl81/parceler

Browser other questions tagged

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