Bluesir9
Bluesir9

Reputation: 121

Android Cant execute Update Query

I am a little new in this so, I am trying to execute an update query to change the value of a field on the basis of selection made in a radio group. However its always giving me a NullPointerException and i havent been able to figure out the cause exactly as yet. This is the code:

GenderFragment:

package com.example.yipeedo;


import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class GenderFragment extends Fragment 
{
int mCurrentPage;
Button next;
RadioGroup genderGroup;
int x;
String choice;
RadioButton gender;
View v;
public SQLiteDatabase db;
DataBaseHelper dbh;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /** Getting the arguments to the Bundle object */
    Bundle data = getArguments();

    /** Getting integer data of the key current_page from the bundle */
    mCurrentPage = data.getInt("current_page", 0);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    v = inflater.inflate(R.layout.gender_layout, container,false);
    next=(Button)v.findViewById(R.id.next);
    genderGroup=(RadioGroup)v.findViewById(R.id.GenderGroup);

    next.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            updateDatabase();
            ((PreferencesActivity)getActivity()).setCurrentItem(1, true);

        }

    });
    return v;
}

public void updateDatabase()
{
    int selectedId=genderGroup.getCheckedRadioButtonId();
    gender=(RadioButton)v.findViewById(selectedId);
    String selection=gender.getText().toString();
    Log.e("Add Gender To Database","Gender:"+selection);
    try
    {

    //dbh.createDataBase();
    //dbh.openDataBase();
    dbh.updateGender(selection);
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}


}

DataBaseHelper:

package com.example.yipeedo;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

 public class DataBaseHelper extends SQLiteOpenHelper{


private static String DB_PATH = "/data/data/com.example.yipeedo/databases/";

private static String DB_NAME = "YipeedoDB1.txt";

private SQLiteDatabase myDataBase; 

private final Context myContext;
InputStream is;


public DataBaseHelper(Context context) {

    super(context, DB_NAME, null, 1);
    this.myContext = context;
}   

public void write(InputStream is)
{
    try {
        OutputStream out = new FileOutputStream(new File(DB_PATH));
        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = is.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }
        is.close();
        out.flush();
        out.close();
        System.err.println(out + "\n");
    } catch (IOException e) {
        e.printStackTrace();
    }
}


@Override
public void onCreate(SQLiteDatabase db) 
{
    try 
    {
        is=myContext.getAssets().open("YipeedoDB1.txt");
        write(is);
        myDataBase.openOrCreateDatabase(DB_PATH+DB_NAME, null);
    } 
    catch (IOException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
{

}

public void updateGender(String x)
{

    myDataBase.execSQL("Update UserData set gender = " + x + " where name like 'ram';");

}



}

Logcat:

08-18 22:34:46.088: W/System.err(17086): java.lang.NullPointerException
08-18 22:34:46.108: W/System.err(17086):    at     com.example.yipeedo.GenderFragment.updateDatabase(GenderFragment.java:71)
08-18 22:34:46.108: W/System.err(17086):    at com.example.yipeedo.GenderFragment$1.onClick(GenderFragment.java:51)
08-18 22:34:46.108: W/System.err(17086):    at android.view.View.performClick(View.java:4432)
08-18 22:34:46.108: W/System.err(17086):    at android.view.View$PerformClick.run(View.java:18338)
08-18 22:34:46.108: W/System.err(17086):    at android.os.Handler.handleCallback(Handler.java:725)
08-18 22:34:46.108: W/System.err(17086):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-18 22:34:46.108: W/System.err(17086):    at android.os.Looper.loop(Looper.java:137)
08-18 22:34:46.108: W/System.err(17086):    at android.app.ActivityThread.main(ActivityThread.java:5283)
08-18 22:34:46.108: W/System.err(17086):    at java.lang.reflect.Method.invokeNative(Native Method)
08-18 22:34:46.108: W/System.err(17086):    at java.lang.reflect.Method.invoke(Method.java:511)
08-18 22:34:46.108: W/System.err(17086):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
08-18 22:34:46.108: W/System.err(17086):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
08-18 22:34:46.108: W/System.err(17086):    at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 202

Answers (2)

Jose Rodriguez
Jose Rodriguez

Reputation: 10152

The problem is the variable dbh, is null.

on onCreate method add:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /** Getting the arguments to the Bundle object */
    Bundle data = getArguments();

    /** Getting integer data of the key current_page from the bundle */
    mCurrentPage = data.getInt("current_page", 0);

    dbh = new DataBaseHelper(getActivity());
}

And modify:

public void updateGender(String x)
{
    if(myDataBase == null)
        myDataBase = getWritableDatabase();

    myDataBase.execSQL("Update UserData set gender = " + x + " where name like 'ram';");

}

And you need copy database from asset to internal storage before call an onCreate method of DBOpenHelper, please reconsider your class implementation. Here are a good example of implementation What are the best practices for SQLite on Android?.

Upvotes: 1

Carlos
Carlos

Reputation: 6021

You need to instantiate dbh:

dbh = new DataBaseHelper(yourcontext) // Somewhere in the creation of the fragment.

If you need an appropriate context, you can use the parent activity, which is passed to you in the fragment's onCreateView.

Upvotes: 0

Related Questions