-2

im trying to do a query, but shows an error. this is my code

public String c(){ 
    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;
}

this is the activity where i want to display it

protected void onCreate(Bundle savedInstanceState) {
    TextView 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);

    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();

}

this is the error

  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

im trying to do it in a blank activity but always have that error, thank you for help me.

this is the error

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.oscar.app/com.example.oscar.app.consulta}: 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(consulta.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)

oscar ramirez
  • 11
  • 1
  • 4

1 Answers1

0

The problem seems to be the following

TextView Lab=(TextView)findViewById(R.id.lab_con);

It seems like you are trying to find a view which is not yet drawn on the screen. Try moving the line to after the activity layout has been drawn like this:

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_consulta);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TextView Lab=(TextView)findViewById(R.id.lab_con);
ArchiFloyd
  • 1,274
  • 13
  • 20