Error trying to query on Android

4

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)

    
asked by oscar ramirez 10.11.2016 в 17:47
source

3 answers

1

You should define your TextView as well since the error is telling you that you are trying to use a zero reference method:

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

Another problem that occurs to me is that you have no TextView with that id ( R.id.lab_con ). Check the settings of your XML .

    
answered by 10.11.2016 в 17:48
1

Your error message is correctly raised in your question,

  

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

The error is not due to the query, it is because you are getting the reference of TextView before loading the Layout that contains it:

@Override
protected void onCreate(Bundle savedInstanceState) {

 Lab=(TextView)findViewById(R.id.lab_con); //NO se ga cargado el layout: activity_consulta.xml
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_consulta);//Este layout debe ser cargado antes de buscar un widget dentro de el!
.....

You must load the Layout before searching the reference of an element within , simply ensure that the TextView lab_con exists within the file activity_consulta.xml .

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_consulta);

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

Another error is similar:

  

Attempt to invoke virtual method 'void   android.support.design.widget.FloatingActionButton.setOnClickListener (android.view.View $ OnClickListener) '   on a null object

It is caused by the same problem you must first load the container layout

setContentView(R.layout.activity_consulta);

And then look for the FloatingButton reference within it, ensure it exists within your layout.

    
answered by 10.11.2016 в 23:13
0

The problem is with the FloatingActionButton as you can see in:

  

Attempt to invoke virtual method 'void android.support.design.widget.FloatingActionButton.setOnClickListener (android.view.View $ OnClickListener)' on a null object reference at ...

Verify that the ID is correct since the FAB is not initializing correctly, so when trying to assign the ClickListener to be null, throw the exception

    
answered by 11.11.2016 в 14:03