how to fix sqliteException in android java

0

Issue

I have trying to insert values into database as well as in content provider but getting null sqlite exception.Before using content provider class my data are added to database without any problem.after trying content provider im getting exception. Also I doesn’t know whether values are inserted for content provider…..
And in SpecificActivity class I need to use drawable which is returned by LoadImageAsyncTask class but Im getting null value there…
Help me to solve those issues..

public class FeedReaderContract {
    public static class FeedEntry implements BaseColumns {
        public static final String TABLE_NAME = "entry";
        public static final String _ID = BaseColumns._ID;
        public static final String COLUMN_NAME_TITLE = "title";
        public static final String COLUMN_NAME_CONTENT = "content";
        public static final String COLUMN_NAME_IMAGE = "image";
        public static final String COLUMN_NAME_CHANNEL = "channel";

    }
}
public class FeedReaderDbHelper extends SQLiteOpenHelper {

    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "FeedReader.db";
    Context context;
    public Context getContext() {
        return context;
    }
    private static final String SQL_CREATE_ENTRIES =
        "CREATE TABLE " + FeedReaderContract.FeedEntry.TABLE_NAME + " (" +
        FeedReaderContract.FeedEntry._ID + "  INTEGER PRIMARY KEY, " +
        FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE + "  TEXT, " +
        FeedReaderContract.FeedEntry.COLUMN_NAME_CONTENT + "  TEXT, " +
        FeedReaderContract.FeedEntry.COLUMN_NAME_CHANNEL + "  TEXT, " +
        FeedReaderContract.FeedEntry.COLUMN_NAME_IMAGE + "  TEXT); ";

    private static final String SQL_DELETE_ENTRIES =
        "DROP TABLE IF EXISTS " + FeedReaderContract.FeedEntry.TABLE_NAME;
    public FeedReaderDbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.context = context;
    }
    public void onCreate(SQLiteDatabase db) {
        NewsFeedProvider.db = db;
        db.execSQL(SQL_CREATE_ENTRIES);
        Log.e("sql db", "created");

    }
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(SQL_DELETE_ENTRIES);
        onCreate(db);
    }
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + FeedReaderContract.FeedEntry.TABLE_NAME);
        onCreate(db);
    }
    public void getData(long id) {
        try {
            Log.e("entered", "getdata");

            SQLiteDatabase db = getReadableDatabase();
            Cursor cursor = db.query(FeedReaderContract.FeedEntry.TABLE_NAME, new String[] {
                FeedReaderContract.FeedEntry._ID, FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE, FeedReaderContract.FeedEntry.COLUMN_NAME_CHANNEL, FeedReaderContract.FeedEntry.COLUMN_NAME_CONTENT, FeedReaderContract.FeedEntry.COLUMN_NAME_IMAGE
            }, FeedReaderContract.FeedEntry._ID + "=?", new String[] {
                String.valueOf(id)
            }, null, null, null, null);
            ArrayList < NewsReport > newsReports = new ArrayList < > ();
            List itemIds = new ArrayList < > ();
            while (cursor.moveToNext()) {

                newsReports.add(new NewsReport(cursor.getString(cursor.getColumnIndex(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE)),
                    cursor.getString(cursor.getColumnIndex(FeedReaderContract.FeedEntry.COLUMN_NAME_CHANNEL)),
                    cursor.getString(cursor.getColumnIndex(FeedReaderContract.FeedEntry.COLUMN_NAME_CONTENT)),
                    cursor.getString(cursor.getColumnIndex(FeedReaderContract.FeedEntry.COLUMN_NAME_IMAGE))));
                long itemId = cursor.getLong(
                    cursor.getColumnIndexOrThrow(FeedReaderContract.FeedEntry._ID));
                itemIds.add(itemId);
                Log.e("id", "" + itemId);
            }
            cursor.close();
            Log.e("newsreportsize", "dbhelper" + newsReports.size());
            if (newsReports.size() != 0) {
                for (int i = 0; i < newsReports.size(); i++) {
                    Log.e("news pic", "" + newsReports.get(i).getPic());
                    Log.e("news title", "" + newsReports.get(i).getTitle());
                    Log.e("news content", "" + newsReports.get(i).getContent());
                    Log.e("news chanel", "" + newsReports.get(i).getNewsChannel());
                }
            }

        } catch (Exception ex) {
            Log.e("exception req data", "" + ex);
        }
    }
}
public class LoadImageAsyncTask extends AsyncTask < String, Void, Drawable > {
    private Drawable drawable;
    String imageUrl;
    private Drawable image;
    @Override
    protected Drawable doInBackground(String...strings) {
        try {
            InputStream is = (InputStream) new URL(imageUrl).getContent();
            Drawable d = Drawable.createFromStream(is, "src name");

            Log.e("drwable", "" + d);


        } catch (Exception e) {
            Log.e("Specific Activity", "Converting drawable" + e);

        }
        return drawable;
    }

    @Override
    protected void onPostExecute(Drawable drawableImage) {
        super.onPostExecute(drawableImage);
        setImage(drawableImage);

    }
    public void setImage(Drawable drawable) {
        new SpecificNewsReportActivity().drawable = drawable;
        Log.e("set image", "" + drawable);
        this.drawable = drawable;
    }
    public Drawable getImage() {
        Log.e("get image", "" + drawable);

        return drawable;
    }
}
public class NewsFeedProvider extends ContentProvider {
    static final String PROVIDER_NAME = "com.example.newsreport";
    static final String URL = "content://" + PROVIDER_NAME + "/newsfeed";
    static final Uri CONTENT_URL = Uri.parse(URL);
    static final int uriCode = 1;
    static String newsTitle;
    static String newsContent;
    static String newsImage;
    static String newsChannel;
    static final UriMatcher uriMatcher;
    private FeedReaderDbHelper dbHelper;
    private static HashMap < String, String > values;
    public static SQLiteDatabase db;
    static {
        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        uriMatcher.addURI(PROVIDER_NAME, "newsfeed", uriCode);
    }
    @Override
    public boolean onCreate() {

        dbHelper = new FeedReaderDbHelper(getContext());
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
        queryBuilder.setTables(FeedReaderContract.FeedEntry.TABLE_NAME);
        switch (uriMatcher.match(uri)) {
            case uriCode:
                queryBuilder.setProjectionMap(values);
                break;
            default:
                throw new IllegalArgumentException("Unknown URI" + uri);
        }
        //Cursor cursor=queryBuilder.query(dbHelper.getReadableDatabase(),projection,selection,selectionArgs,null,null,sortOrder);
        Cursor cursor = dbHelper.getReadableDatabase().query(FeedReaderContract.FeedEntry.TABLE_NAME, projection, FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE + "= ?", selectionArgs, null, null, sortOrder);
        cursor.setNotificationUri(getContext().getContentResolver(), uri);
        return cursor;
    }

    @Override
    public String getType(Uri uri) {
        switch (uriMatcher.match(uri)) {
            case uriCode:
                return "vnd.android.cursor.dir/newsfeed";

            default:
                throw new IllegalArgumentException("Unsupported URI" + uri);
        }

    }

    @Override
    public Uri insert(Uri uri, ContentValues contentValues) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        long rowId = db.insert(FeedReaderContract.FeedEntry.TABLE_NAME, null, contentValues);
        if (rowId > 0) {
            Uri _uri = ContentUris.withAppendedId(CONTENT_URL, rowId);
            getContext().getContentResolver().notifyChange(_uri, null);
            Log.e("insert", "feedreader" + contentValues);
            return _uri;
        } else {
            Toast.makeText(getContext(), "Row insert failed", Toast.LENGTH_SHORT).show();
            return null;
        }
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int rowsDeleted = 0;
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        switch (uriMatcher.match(uri)) {
            case uriCode:
                rowsDeleted = db.delete(FeedReaderContract.FeedEntry.TABLE_NAME, selection, selectionArgs);
                break;
            default:
                throw new IllegalArgumentException("Unknown URI" + uri);
        }
        getContext().getContentResolver().notifyChange(uri, null);
        return rowsDeleted;
    }

    @Override
    public int update(Uri uri, ContentValues contentValues, String selection, String[] selectionArgs) {
        int rowsUpdated = 0;
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        switch (uriMatcher.match(uri)) {
            case uriCode:
                rowsUpdated = db.delete(FeedReaderContract.FeedEntry.TABLE_NAME, selection, selectionArgs);
                break;
            default:
                throw new IllegalArgumentException("Unknown URI" + uri);
        }
        getContext().getContentResolver().notifyChange(uri, null);
        return rowsUpdated;
    }
}

public class NewsReportActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks < ArrayList < NewsReport >> {
    ProgressBar progressBar;
    FeedReaderDbHelper dbHelper;
    SQLiteDatabase db;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recycler_view);
        dbHelper = new FeedReaderDbHelper(this);
        db = dbHelper.getWritableDatabase();
        Log.e("application", "" + dbHelper);
        Intent intent = getIntent();
        if (intent.hasExtra("exception")) {
            TextView connectionTextView = (TextView) findViewById(R.id.no_connection_text_view);
            connectionTextView.setText("There is no internet connection!!!");
        } else {
            Log.e("no exception", "entered else");
        }
        getSupportLoaderManager().initLoader(0, null, this);

    }

    @NonNull
    @Override
    public Loader < ArrayList < NewsReport >> onCreateLoader(int id, @Nullable Bundle args) {
        NewsReportLoader newsReportLoader = new NewsReportLoader(this);
        newsReportLoader.forceLoad();
        progressBar = (ProgressBar) findViewById(R.id.progress_bar);
        progressBar.setVisibility(View.VISIBLE);

        return newsReportLoader;
    }

    @Override
    public void onLoadFinished(@NonNull Loader < ArrayList < NewsReport >> loader, ArrayList < NewsReport > data) {
        try {
            Log.e("data", "" + data.size());
            NewsReportAdapter adapter = new NewsReportAdapter(this, data);
            RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
            progressBar.setVisibility(View.INVISIBLE);
            recyclerView.setAdapter(adapter);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
            DividerItemDecoration mDividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
                new LinearLayoutManager(this).getOrientation());
            recyclerView.addItemDecoration(mDividerItemDecoration);

            if (dbHelper != null) {
                Log.e("dbhelper", "notnull");
                db = dbHelper.getWritableDatabase();
                if (db != null) {
                    Log.e("db", "notnull");
                    for (int i = 0; i < data.size(); i++) {
                        ContentValues values = new ContentValues();
                        NewsReport newsReport = data.get(i);
                        values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE, newsReport.getTitle());
                        values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_CONTENT, newsReport.getContent());
                        values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_CHANNEL, newsReport.getNewsChannel());
                        values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_IMAGE, newsReport.getPic());
                        values.put(NewsFeedProvider.newsTitle, newsReport.getTitle());
                        values.put(NewsFeedProvider.newsContent, newsReport.getContent());
                        values.put(NewsFeedProvider.newsChannel, newsReport.getNewsChannel());
                        values.put(NewsFeedProvider.newsImage, newsReport.getPic());
                        try {
                            Uri uri = getApplicationContext().getContentResolver().insert(NewsFeedProvider.CONTENT_URL, values);
                        } catch (Exception ex) {
                            Log.e("exception in resolver" + getContentResolver(), "" + ex);
                        }
                        long newRowId = db.insert(FeedReaderContract.FeedEntry.TABLE_NAME, null, values);
                        Log.e("new row id", "" + newRowId);
                        dbHelper.getData(newRowId);
                    }
                } else
                    Log.e("db", "null");
            } else {
                Log.e("dbhelper", "null");
            }
            // db.execSQL("delete from "+ FeedReaderContract.FeedEntry.TABLE_NAME);

        } catch (Exception ex) {
            Log.e("newsactivity", "" + FeedReaderContract.FeedEntry.TABLE_NAME.length());
            Log.e("onloadfinished", "" + ex);
            Log.e("datahelper", "" + dbHelper.getReadableDatabase().rawQuery("SELECT * FROM " + FeedReaderContract.FeedEntry.TABLE_NAME, null));
        }
    }

    @Override
    public void onLoaderReset(@NonNull Loader < ArrayList < NewsReport >> loader) {

    }
}
public class SpecificNewsReportActivity extends AppCompatActivity {
    private TextView contentTextView, titleTextView;
    public ImageView imageView;

    private Intent intent;
    public Drawable drawable;
    private String imageUrl;



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

        ActionBar actionBar = this.getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        intent = getIntent();
        LoadImageAsyncTask task = new LoadImageAsyncTask();
        AsyncTask < String, Void, Drawable > d = task.execute();

        try {
            drawable = d.get();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        imageUrl = getIntent().getStringExtra("image");
        Log.e("drawable specific", "" + drawable);
        drawable = new LoadImageAsyncTask().getImage();
        contentTextView = (TextView) findViewById(R.id.specific_news_report_content_text_view);
        titleTextView = (TextView) findViewById(R.id.specific_news_report_title_text_view);
        imageView = (ImageView) findViewById(R.id.specific_news_report_image_view);
        contentTextView.setText(intent.getStringExtra("content"));
        titleTextView.setText(intent.getStringExtra("title"));
        try {
            if (drawable != null) {
                imageView.setImageDrawable(drawable);
            } else {
                Log.e("returned drawable", "null");
            }
        } catch (Exception ex) {
            Log.e("enna exception", "" + ex);
        }

    }
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            Intent intent = NavUtils.getParentActivityIntent(this);
            NavUtils.navigateUpTo(this, intent);
        }
        return super.onOptionsItemSelected(item);
    }

}

stacktrace

2019-07-29 13:57:20.172 24098-24098/com.example.newsreport E/SQLiteLog: (1) near "null": syntax error
2019-07-29 13:57:20.177 24098-24098/com.example.newsreport E/SQLiteDatabase: Error inserting channel=News18.com null=https://images.news18.com/ibnlive/uploads/2019/07/yediyurappa-1.jpg image=https://images.news18.com/ibnlive/uploads/2019/07/yediyurappa-1.jpg title=Karnataka Assembly Trust Vote LIVE: Yediyurappa Wins Floor Test, Speaker Ramesh Kumar Resigns - News18 content=Eleven Congress MLAs and three JDS lawmakers faced the axe from the Speaker in addition to the three disqualified earlier, bringing down the majority mark to 104, one less than the current strength of 105 of the BJP, which also enjoys the support of an Indepe… [+3489 chars]
    android.database.sqlite.SQLiteException: near "null": syntax error (code 1 SQLITE_ERROR): , while compiling: INSERT INTO entry(channel,null,image,title,content) VALUES (?,?,?,?,?)
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:903)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:514)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
        at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1562)
        at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1433)
        at com.example.newsreport.NewsFeedProvider.insert(NewsFeedProvider.java:78)
        at android.content.ContentProvider$Transport.insert(ContentProvider.java:265)
        at android.content.ContentResolver.insert(ContentResolver.java:1587)
        at com.example.newsreport.NewsReportActivity.onLoadFinished(NewsReportActivity.java:96)
        at com.example.newsreport.NewsReportActivity.onLoadFinished(NewsReportActivity.java:29)
        at androidx.loader.app.LoaderManagerImpl$LoaderObserver.onChanged(LoaderManagerImpl.java:250)
        at androidx.lifecycle.LiveData.considerNotify(LiveData.java:113)
        at androidx.lifecycle.LiveData.dispatchingValue(LiveData.java:131)
        at androidx.lifecycle.LiveData.setValue(LiveData.java:289)
        at androidx.lifecycle.MutableLiveData.setValue(MutableLiveData.java:33)
        at androidx.loader.app.LoaderManagerImpl$LoaderInfo.setValue(LoaderManagerImpl.java:189)
        at androidx.loader.app.LoaderManagerImpl$LoaderInfo.onLoadComplete(LoaderManagerImpl.java:174)
        at androidx.loader.content.Loader.deliverResult(Loader.java:132)
        at androidx.loader.content.AsyncTaskLoader.dispatchOnLoadComplete(AsyncTaskLoader.java:258)
        at androidx.loader.content.AsyncTaskLoader$LoadTask.onPostExecute(AsyncTaskLoader.java:83)
        at androidx.loader.content.ModernAsyncTask.finish(ModernAsyncTask.java:490)
        at androidx.loader.content.ModernAsyncTask$InternalHandler.handleMessage(ModernAsyncTask.java:507)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2019-07-29 13:57:20.218 24098-24098/com.example.newsreport E/SQLiteLog: (1) near "null": syntax error
2019-07-29 13:57:20.220 24098-24098/com.example.newsreport E/SQLiteDatabase: Error inserting channel=News18.com null=https://images.news18.com/ibnlive/uploads/2019/07/yediyurappa-1.jpg image=https://images.news18.com/ibnlive/uploads/2019/07/yediyurappa-1.jpg title=Karnataka Assembly Trust Vote LIVE: Yediyurappa Wins Floor Test, Speaker Ramesh Kumar Resigns - News18 content=Eleven Congress MLAs and three JDS lawmakers faced the axe from the Speaker in addition to the three disqualified earlier, bringing down the majority mark to 104, one less than the current strength of 105 of the BJP, which also enjoys the support of an Indepe… [+3489 chars]
    android.database.sqlite.SQLiteException: near "null": syntax error (code 1 SQLITE_ERROR): , while compiling: INSERT INTO entry(channel,null,image,title,content) VALUES (?,?,?,?,?)
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:903)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:514)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
        at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1562)
        at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1433)
        at com.example.newsreport.NewsReportActivity.onLoadFinished(NewsReportActivity.java:102)
        at com.example.newsreport.NewsReportActivity.onLoadFinished(NewsReportActivity.java:29)
        at androidx.loader.app.LoaderManagerImpl$LoaderObserver.onChanged(LoaderManagerImpl.java:250)
        at androidx.lifecycle.LiveData.considerNotify(LiveData.java:113)

I need to use drawable which is returned from loadimageasynctask
I need to store all data in database as well as for content provider

Solution

In the table there are 4 columns and the primary key.
When you insert a new row you supply values for the ContentValues object but you do it twice for each column:

values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE, newsReport.getTitle());
values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_CONTENT, newsReport.getContent());
values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_CHANNEL, newsReport.getNewsChannel());
values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_IMAGE, newsReport.getPic());
values.put(NewsFeedProvider.newsTitle, newsReport.getTitle());
values.put(NewsFeedProvider.newsContent, newsReport.getContent());
values.put(NewsFeedProvider.newsChannel, newsReport.getNewsChannel());
values.put(NewsFeedProvider.newsImage, newsReport.getPic()); 

Why?
I suspect that one of:

NewsFeedProvider.newsTitle
NewsFeedProvider.newsContent
NewsFeedProvider.newsChannel
NewsFeedProvider.newsImage

returns null.
So delete these lines:

values.put(NewsFeedProvider.newsTitle, newsReport.getTitle());
values.put(NewsFeedProvider.newsContent, newsReport.getContent());
values.put(NewsFeedProvider.newsChannel, newsReport.getNewsChannel());
values.put(NewsFeedProvider.newsImage, newsReport.getPic());

They are not needed.
The values for the columns are set by the previous 4 lines.

Answered By – forpas

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More