My program gave this problem when I tried to create a stack (database), to store strings in my Android app
package com.project.meuapp2;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Principal extends AppCompatActivity {
private Button btn;
private EditText texto;
BancodeDados DB;
@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.InsereInicio(y);
}
});
}
@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);
}
}
package com.project.meuapp2;
public class BancodeDados {
elemento primeiro = new elemento();
elemento ultimo = new elemento();
public void InsereInicio(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 removeInicio(){
String x;
x = primeiro.dado;
primeiro = primeiro.proximo;
return x;
}
}
Android Studio Log
03-27 18: 14: 28,017 5239-5239 / com.project.meuapp2 E / AndroidRuntime: FATAL EXCEPTION: main Process: com.project.meuapp2, PID: 5239 java.lang.NullPointerException: Attempt to invoke virtual method 'void com.project.meuapp2.InstanceInstance (java.lang.String) 'on a null object reference at com.project.meuapp2.Principal $ 1.onClick (Main.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 have tried to initialize the variable primeiro
, but it has the same error.