0

i have two classes which i am trying to pass my values in other class for insertion in db, through getters and setters i made but i get the the exception and my application crashes as soon as i click the Save button ..please somebody take a look on it its been three days i cant seem to solve it Thanks here are my both classes:

/**
 * Created by Sanya on 11/22/2015.
 */
                   public class User_overlay_save extends Activity implements View.OnClickListener
  {
    private static int RESULT_LOAD_IMAGE = 1;
    private static int RESULT_LOAD_VIDEO = 2;
    private int position = 0;
    private SQLiteDatabase db; DatabaseHelper helper;
    File myapp;
    private MediaController mediaControls;

    Button save_button;
    ImageButton vid_button,img_button;
    EditText  name_overlay;
   TextView real_Imgpath, real_Vidpath;

protected void onCreate(Bundle savedInstanceState){
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_overlay_save);

    save_button=(Button)findViewById(R.id.save_Ouser);

    img_button=(ImageButton)findViewById(R.id.img_button);
    vid_button=(ImageButton)findViewById(R.id.vid_button);

    real_Imgpath =(TextView)findViewById(R.id.real_Imgpath);
    real_Vidpath =(TextView)findViewById(R.id.real_Vidpath);

    name_overlay=(EditText)findViewById(R.id.name_overlay);

    save_button.setOnClickListener(this);
    img_button.setOnClickListener(this);
    vid_button.setOnClickListener(this);

    name_overlay.setOnClickListener(this);

        }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.save_Ouser:
            {
               boolean result= helper.Insert();

                if(result==true)
                {
                    Toast.makeText(getApplicationContext(), "saved..in DB..!!!",
                            Toast.LENGTH_LONG).show();
                }
else
                {
                    Toast.makeText(getApplicationContext(), "ERROR--the path could not b saved..!!",
                            Toast.LENGTH_LONG).show();
                }
            }


            break;
            case R.id.img_button:

                Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

               startActivityForResult(i, RESULT_LOAD_IMAGE);
                break;
            case R.id.vid_button:
                Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent, RESULT_LOAD_VIDEO);

                break;

        }
    }
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            String realPath;
//FOR IMAGE PATH SAVING
        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
            String selectedImagePath;
            Uri selectedImageUri = data.getData();

//MEDIA GALLERY

     selectedImagePath =       ImageFilePath.getPath(getApplicationContext(), selectedImageUri);
        Log.i("Image-->File Path-->", " " + selectedImagePath);
        setImgTextViews(Build.VERSION.SDK_INT, selectedImagePath);
        SET_saveImg(selectedImagePath);
    }
          else if(Build.VERSION.SDK_INT < 19)
            {
            realPath = this.getRealPathFromURI_FromAPI11_18(this, data.getData());
                setImgTextViews(Build.VERSION.SDK_INT, realPath);
            }
//----------->>>>>>>>>>>---------->>>>FOR VIDEO PATH SAVING------>>>>>>------------>>>>>>>>>>>-------->>>>>>>>>---------->>>>

        if (requestCode == RESULT_LOAD_VIDEO && resultCode == RESULT_OK && null != data)
        {
//FOR VIDEO PATH SAVING
            String selectedVidPath;
            Uri selectedVidUri = data.getData();

//VIDEO____PLAY___LIST
            selectedVidPath = ImageFilePath.getPath(getApplicationContext(), selectedVidUri);
            Log.i("VIDEO-->File Path-->", "" + selectedVidPath);
            setVIDTextViews(Build.VERSION.SDK_INT, selectedVidPath);
            SET_saveVid(selectedVidPath);
        }
    }

    public String getRealPathFromURI_FromAPI11_18(Context context, Uri contentUri)
    {
        String[] proj = { MediaStore.Images.Media.DATA };
        String result = null;

        CursorLoader cursorLoader = new CursorLoader(context, contentUri, proj, null, null, null);
        Cursor cursor = cursorLoader.loadInBackground();

        if(cursor != null){
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            result = cursor.getString(column_index);
        }
        return result;
    }


    protected void setImgTextViews(int sdk,String realPath){


    this.real_Imgpath.setText
           ("CURRENT VERSION--SDK-->"+sdk+"Real Path: "+realPath);

}
         protected void setVIDTextViews(int sdk,String realPath)
    {
               this.real_Vidpath.setText
            ("CURRENT VERSION--SDK-->"+sdk+"Real Path: "+realPath);
    }

//---->>------>>>------->>>>SETTERS--FOR IMAGE-->SET_saveImg(selectedImagePath) For &VIDEO -->>SET_saveVid(selectedVidPath)--->>AND--->>GETTERS FOR IMAGE AND VIDEO--------->>>>>

String realPath; String vidPath;
public String GET_saveImg()//IS CALLED FROM DATABASE_HELPER CLASS TO PROVIDE THE SETTER VALUES
{
    return realPath;
}

protected void SET_saveImg(String realPath)
{
    this.realPath=realPath;

}
      protected String GET_saveVid()
{
    return vidPath;
}
     protected void SET_saveVid(String vidPath)
{
  this.vidPath= vidPath;
}}

---------------------<<<<<<------------>>>>>>>>>>>>--------- DATABASEHELPER_class

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


       public class DatabaseHelper extends SQLiteOpenHelper {
          SQLiteDatabase db;    User_overlay_save obj ;


      // Database Name
      public static String DATABASE_NAME = "MY_database.sql";

// Current version of database
private static final int DATABASE_VERSION = 1;

// Name of table
private static final String TABLE_Images = "images";

// All Keys used in table
 static final String KEY_ID = "id";
 static final String KEY_IMAGE = "image";
 static final String KEY_FILE = "file";


public static String TAG = "tag";

private static final String CREATE_TABLE_Images = "CREATE TABLE "
        + TABLE_Images + "(" + KEY_ID
        + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_IMAGE + "TEXT" +            KEY_FILE + " TEXT );";

      public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
         }
   /*This method is called by system if the database is accessed but not   yet created*/



 @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL(CREATE_TABLE_Images); // create Image table

    }


@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int  newVersion)    {
    db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_Images); 
                            // drop               table if exists

    onCreate(db);
      }

   public boolean Insert()
   {      boolean success = false;

    String imgpath = obj.GET_saveImg();
    String vidPath = obj.GET_saveVid();

    db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(KEY_IMAGE, imgpath);
    values.put(KEY_FILE, vidPath);

    db.insert(TABLE_Images, null, values);

    success=true;
    db.close();

if(success) {
    return true;
}
else
{
    return false;
}
}
}
user3598648
  • 25
  • 1
  • 6

1 Answers1

0

Your DatabaseHelper object helper is null. Because you have just declare it, not define anywhere.

put helper = new DatabaseHelper(any arguments if there it is); inside onCreate before setting onClickListener on save button.

protected void onCreate(Bundle savedInstanceState){
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_overlay_save);

        ...........................
        helper = new DatabaseHelper();
        save_button.setOnClickListener(this);

        ..................

}
Bhargav Thanki
  • 4,924
  • 2
  • 37
  • 43
  • thank u @bhargav for ur response i tried that but now its giving the exception of sqlite E/ActivityThread: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:740) 11-24 22:55:40.905 15416-15416/com.example.sanya.ar_expeditor E/SQLiteLog: (1) table images has no column named image SQLiteDatabase: Error inserting image=null file=nullSQLiteDatabase: android.database.sqlite.SQLiteException: table images has no column named image (code 1): , while compiling: INSERT INTO images(image,file) VALUES (?,?) – user3598648 Nov 24 '15 at 18:00
  • refer http://stackoverflow.com/questions/17441927/android-sqlite-issue-table-has-no-column-named and http://stackoverflow.com/questions/15123966/android-table-has-no-column-named-variable-name-mysql-database-error – Bhargav Thanki Nov 24 '15 at 18:03
  • thanku for ur kind help it worked fro the required problem.. :) – user3598648 Nov 30 '15 at 16:08