I have a problem trying to make a query in Android . My code is as follows:
Method to make the query:
public String c(){ /*ArrayList<String> getItemDetails(){
ArrayList<String> itemdetails=new ArrayList<String>();*/
objBD = new Bd(nContext, "Laboratorios", null, 1);
db = objBD.getReadableDatabase();
Cursor c=db.rawQuery("SELECT * FROM Laboratorios WHERE ID = (SELECT MAX(ID) FROM Laboratorios);", null);
if(c!=null) {
while (c.moveToNext()) {
String iLab = c.getString(c.getColumnIndex("Descripcion"));
//.add(iLab);
return iLab;
}
}
return null;
}
Activity where I want to send the information obtained:
private TextView Lab;
@Override
protected void onCreate(Bundle savedInstanceState) {
Lab=(TextView)findViewById(R.id.lab_con);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_consulta);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ConexionBD O1=new ConexionBD(this);
O1.c();
Lab.setText(O1.tostring());
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
/*String texto ="bien";
Toast toast = Toast.makeText(this, texto, Toast.LENGTH_LONG);
toast.show();
Intent i =new Intent(this,Labs.class);
startActivity(i);*/
}
public void salir (View view){
String texto ="Se ha guardado su registro";
Toast toast = Toast.makeText(this, texto, Toast.LENGTH_LONG);
toast.show();
Intent salida=new Intent( Intent.ACTION_MAIN); //Llamando a la activity principal
finish();
}
}
The error that appears to me is the following.
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText (java.lang.CharSequence)' on a null object reference
The complete error that appears to me
java.lang.RuntimeException: Unable to start activity ComponentInfo {com.example.oscar.app/com.example.oscar.app.query}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support .design.widget.FloatingActionButton.setOnClickListener (android.view.View $ OnClickListener) 'on a null object reference at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2646) at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2707) at android.app.ActivityThread.-wrap12 (ActivityThread.java) at android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1460) at android.os.Handler.dispatchMessage (Handler.java:102) at android.os.Looper.loop (Looper.java:154) at android.app.ActivityThread.main (ActivityThread.java:6077) at java.lang.reflect.Method.invoke (Native Method) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:755) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.FloatingActionButton.setOnClickListener (android.view.View $ OnClickListener)' on a null object reference at com.example.oscar.app.consulta.onCreate (query.java:31) at android.app.Activity.performCreate (Activity.java:6664) at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2599) at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2707) at android.app.ActivityThread.-wrap12 (ActivityThread.java) at android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1460) at android.os.Handler.dispatchMessage (Handler.java:102) at android.os.Looper.loop (Looper.java:154) at android.app.ActivityThread.main (ActivityThread.java:6077) at java.lang.reflect.Method.invoke (Native Method) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:755)