0

I have a problem when I try to update a table. But the problem is not for sentence sql, is calling the method for update. This is the code of button that make the action

public void onClick(View v) 
{
    switch (v.getId())
    {
    case R.id.btnsave:
        String id="Act1";           
        datos.Actualizar(id,punactfis);
        Toast.makeText(MainProgreso.this, "Bien Hecho", Toast.LENGTH_SHORT).show();
        datos.close();
    }
}   

the code method Actualizar

public void Actualizar(String id, int puntos)
{
    Log.i("Variables", id);
    String query="UPDATE Actividades SET puntos="+puntos+" ,total_puntos=total_puntos"+puntos+" WHERE id_actividad="+id+";";
    Log.i("Consulta", query);
    db.execSQL(query);
}

This is the log

02-24 15:07:00.474: E/AndroidRuntime(858): FATAL EXCEPTION: main 02-24 15:07:00.474: E/AndroidRuntime(858): java.lang.NullPointerException 02-24 15:07:00.474: E/AndroidRuntime(858): at com.example.app8semanas.MainProgreso.onClick(MainProgreso.java:133) 02-24 15:07:00.474: E/AndroidRuntime(858): at android.view.View.performClick(View.java:3480) 02-24 15:07:00.474: E/AndroidRuntime(858): at android.view.View$PerformClick.run(View.java:13983) 02-24 15:07:00.474: E/AndroidRuntime(858): at android.os.Handler.handleCallback(Handler.java:605) 02-24 15:07:00.474: E/AndroidRuntime(858): at android.os.Handler.dispatchMessage(Handler.java:92) 02-24 15:07:00.474: E/AndroidRuntime(858): at android.os.Looper.loop(Looper.java:137) 02-24 15:07:00.474: E/AndroidRuntime(858): at android.app.ActivityThread.main(ActivityThread.java:4340) 02-24 15:07:00.474: E/AndroidRuntime(858): at java.lang.reflect.Method.invokeNative(Native Method) 02-24 15:07:00.474: E/AndroidRuntime(858): at java.lang.reflect.Method.invoke(Method.java:511) 02-24 15:07:00.474: E/AndroidRuntime(858): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 02-24 15:07:00.474: E/AndroidRuntime(858): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 02-24 15:07:00.474: E/AndroidRuntime(858): at dalvik.system.NativeStart.main(Native Method)

thanks for your help.

All code of the class

public class MainProgreso extends Activity implements OnClickListener, OnCheckedChangeListener {
private TabHost th;
private TextView dia;
private RadioGroup rg1, rg2, rg3;   
private ImageButton btns1, btns2, btns3;

String fecha;
int punactfis, punfrve, pundor;
OperacionesBD datos;
protected void onCreate(Bundle savedInstanceState) 
{       
    super.onCreate(savedInstanceState);
    setContentView(R.layout.progreso);

    fecha = getIntent().getStringExtra("fecha");
    dia=(TextView)findViewById(R.id.txt2);
    dia.setText(fecha);

    rg1=(RadioGroup)findViewById(R.id.rgactfis);
    rg2=(RadioGroup)findViewById(R.id.rgfruveg);
    rg3=(RadioGroup)findViewById(R.id.rgdormir);
    rg1.setOnCheckedChangeListener(this);
    rg2.setOnCheckedChangeListener(this);
    rg3.setOnCheckedChangeListener(this);

    btns1=(ImageButton)findViewById(R.id.btnsave);
    btns2=(ImageButton)findViewById(R.id.btnsave1);
    btns3=(ImageButton)findViewById(R.id.btnsave2);
    btns1.setOnClickListener(this);
    btns2.setOnClickListener(this);
    btns3.setOnClickListener(this);

    th=(TabHost)findViewById(android.R.id.tabhost);     
    th.setup();     

    TabSpec ts1=th.newTabSpec("Tab1");
    ts1.setIndicator("Actividad_Fisica");
    ts1.setContent(R.id.tab1);  
    th.addTab(ts1);

    TabSpec ts2=th.newTabSpec("Tab2");
    ts2.setIndicator("Frutas_Verduras");
    ts2.setContent(R.id.tab2);              
    th.addTab(ts2);

    TabSpec ts3=th.newTabSpec("Tab3");
    ts3.setIndicator("Sueño");
    ts3.setContent(R.id.tab3);      
    th.addTab(ts3);

}

public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) 
{
    switch(checkedId)
    {
    case R.id.rbaf1:
        punactfis=2;
        break;
    case R.id.rbaf2:
        punactfis=4;
        break;
    case R.id.rbaf3:
        punactfis=6;
        break;
    case R.id.rbaf4:
        punactfis=8;
        break;
    }
    switch(checkedId)
    {
    case R.id.rbfv1:
        punfrve=3;
        break;
    case R.id.rbfv2:
        punfrve=0;
        break;
    }
    switch (checkedId)
    {
    case R.id.rbd1:
        pundor=3;
        break;
    case R.id.rbd2:
        pundor=0;
        break;
    }
}

@Override
public void onClick(View v) 
{
    switch (v.getId())
    {
    case R.id.btnsave:
        String  query="UPDATE Actividades SET puntos="+punactfis+" ,total_puntos=total_puntos"+punactfis+" WHERE id_actividad=Act1;";           
        datos.InsertarMeta(query);
        Toast.makeText(MainProgreso.this, "Bien Hecho", Toast.LENGTH_SHORT).show();
        datos.close();
    }
}   

}

1 Answers1

-1

It seems "datos" is null. Did you create this object?

Btw, also, consider binding your sql parameters.

JHH
  • 8,567
  • 8
  • 47
  • 91
  • Yes, i created the object. – Angel Angelus Feb 25 '15 at 17:43
  • Well you didn't state which line of code threw the exception, but it is in the onClick method and the only thing I can think about that would throw a NPE there is if datos is null or v is null,and onClick would never be called for a null view. Can you add information on which line threw the exception? – JHH Feb 25 '15 at 17:53
  • The line that threw the exception was this datos.InsertarMeta(query); – Angel Angelus Feb 26 '15 at 13:38
  • That strongly suggests that "datos" is null, like I suggested in my answer, which was downvoted. Looking at the code you added, it's clear that datos is never assigned to any objec. There is no "datos = new OperacionesDB" or similar. But you state above that you did in fact create the object..? – JHH Feb 26 '15 at 20:56