Click here to Skip to main content
15,946,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 sombody take a look on it its been three days i cant seem to solve it Thanks
here are my both classes:
Java
package com.example.sanya.ar_expeditor;

import android.app.Activity;
import android.content.Context;
import android.content.CursorLoader;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import java.io.File;
import java.io.IOException;

/**
 * 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;
    }}
package com.example.sanya.ar_expeditor;


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


/**
 * Created by Sanya on 11/19/2015.
 */
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;
    }
}
}
Posted
Updated 23-Nov-15 20:37pm
v3
Comments
Richard MacCutchan 24-Nov-15 3:46am    
Check the Android logs for other error messages to see which reference is causing the problem. Alsao, in future, please indicate the line where the error occurs.

1 solution

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

I cannot see any initialisation for obj in the DatabaseHelper class.
 
Share this answer
 
v2
Comments
Mohibur Rashid 24-Nov-15 4:02am    
Second variable of DatabaseHelper class. But initialisation is missing
Richard MacCutchan 24-Nov-15 4:11am    
Thanks, but it has never been initialised.
Sānyà Sani 24-Nov-15 8:14am    
yes i tried the the second variable initialization too bt this does not help it gives me ERROR on the line--> boolean result= helper.Insert();present in User_overlay_save class.. along with the same above error when i click it it shows me this line.. i cant pin point the exact error logical or sum run time exception..
Richard MacCutchan 24-Nov-15 8:18am    
Same error, you have not initialised your variable. Maybe take some time to do a better desk check of your code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900