Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6780417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:30:12+00:00 2026-05-26T16:30:12+00:00

I am getting response from server side in the form of json array.I need

  • 0

I am getting response from server side in the form of json array.I need to first parse them and then insert these values to database.I have parsed the data. Now I need to insert that data into database. I am using the following class for creating an dinserting data in database.:

Following is my helper class:

public class WineDatabaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "applicationdata";
    private static String DB_PATH = "/data/data/com.emxWinwcountry./databases/";
    private static final int DATABASE_VERSION = 1;

    public static final String DATABASE_CREATE ="CREATE TABLE resources(KEY_TYPE text  , KEY_ENCODING text , KEY_WIDTH text, KEY_HEIGHT text, KEY_DATA text ,KeyIId text)";
    public static final String DATABASE_NAVIGATION_CREATE = "create table navigation (KEY text,TYPE1 text ,LINK_NAME text,DISPLAY_NAME text ,FORWARD_ICON text ,HIGHLIGHTED_ICON_WITH_TEXT text,BANNER_IMAGE text,CHILDREN text,ICON_WITHOUT_TEXT text, HIGHLIGHTED_ICON_WITHOUT_TEXT text,ICON_WITH_TEXT text)";



    public WineDatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }



    // Method is called during creation of the database



    @Override
    public void onCreate(SQLiteDatabase database) {
        try{
            database.execSQL(DATABASE_CREATE);
        database.execSQL(DATABASE_NAVIGATION_CREATE);

    }
    catch (Exception e) {
        System.out.println("in on creste db"+e);
    }}

    // Method is called during an upgrade of the database, e.g. if you increase
    // the database version
    @Override
    public void onUpgrade(SQLiteDatabase database, int oldVersion,
            int newVersion) {
        Log.w(WineDatabaseHelper.class.getName(),
                "Upgrading database from version " + oldVersion + " to "
                        + newVersion + ", which will destroy all old data");
        database.execSQL("DROP TABLE IF EXISTS todo");
        onCreate(database);
    }
}

and following is my adapter class

       public class WineDatabaseAdapter {

    // Database fields
    public static final String KEY_TYPE = "type";
    public static final String KEY_ID = "_id";
    public static final String KEY_WIDTH = "width";
    public static final String KEY_HEIGHT = "height";
    public static final String KEY_ENCODING = "encoding";
    public static final String KEY_DATA = "data";
    private static final String KeyIId = "iid";
    private static  String DATABASE_TABLE = "resources";
    private static  String DATABASE_TABLE_NAVIGATION = "navigation";
    private Context context;
    private SQLiteDatabase database;
    private WineDatabaseHelper dbHelper;

    public static final String DISPLAY_NAME= "display_name";
    public static final String KEY="key";
    public static final String TYPE1 = "type1";
    public static final String LINK_NAME = "link_name";
    private static final String FORWARD_ICON = "forward_icon";
    private static final String HIGHLIGHTED_ICON_WITH_TEXT = "highlighted_icon_with_text";
    private static final String ICON_WITHOUT_TEXT = "icon_without_text";
    private static final String HIGHLIGHTED_ICON_WITHOUT_TEXT = "highlighted_icon_without_text";
    private static final String Banner_Image = "banner_image";
    private static final String CHILDREN= "children";
    private static final String ICON_WITH_TEXT = "icon_with_text";
      private SQLiteDatabase db;
      private SQLiteStatement insertStmt;


       private static final String INSERT = "insert into " 
         + DATABASE_TABLE + "(KEY_TYPE,KEY_ENCODING,KEY_WIDTH,KEY_HEIGHT,KEY_DATA,KeyIId)"+" values (?,?,?,?,?,?)";


       private static final String INSERT_NAVIGATION = "insert into " 
             + DATABASE_TABLE_NAVIGATION + "(KEY,TYPE1,LINK_NAME,DISPLAY_NAME,FORWARD_ICON,HIGHLIGHTED_ICON_WITH_TEXT,ICON_WITHOUT_TEXT,HIGHLIGHTED_ICON_WITHOUT_TEXT,Banner_Image,CHILDREN,ICON_WITH_TEXT)"+" values (?,?,?,?,?,?,?,?,?,?,?)";


       public WineDatabaseAdapter(Context context) {
         try{
           this.context = context;
       WineDatabaseHelper openHelper = new WineDatabaseHelper(context);
       this.db=openHelper.getWritableDatabase();
      this.insertStmt=this.db.compileStatement(INSERT);
      this.insertStmt=this.db.compileStatement(INSERT_NAVIGATION);
       }
       catch(Exception e)
       {
           System.out.println(e);
       }
       }

       public long insert(String KEY_TYPE ,String KEY_ENCODING,String KEY_WIDTH,String KEY_HEIGHT,String KEY_DATA,String KeyIId){
           this.insertStmt.bindString(1, KEY_TYPE);
           this.insertStmt.bindString(2, KEY_ENCODING);
           this.insertStmt.bindString(3, KEY_WIDTH);
           this.insertStmt.bindString(4, KEY_WIDTH);
           this.insertStmt.bindString(5, KEY_DATA);
           this.insertStmt.bindString(6, KeyIId);
           return this.insertStmt.executeInsert();
       }

       public long insertNavigation(String KEY ,String TYPE1,String LINK_NAME,String DISPLAY_NAME,String FORWARD_ICON,String HIGHLIGHTED_ICON_WITH_TEXT,String ICON_WITHOUT_TEXT,String HIGHLIGHTED_ICON_WITHOUT_TEXT,String Banner_Image,String CHILDREN,String ICON_WITH_TEXT ){
           this.insertStmt.bindString(1, KEY);
           this.insertStmt.bindString(2, TYPE1);
           this.insertStmt.bindString(3, LINK_NAME);
           this.insertStmt.bindString(4, DISPLAY_NAME);
           this.insertStmt.bindString(5, FORWARD_ICON);
           this.insertStmt.bindString(6, HIGHLIGHTED_ICON_WITH_TEXT);
           this.insertStmt.bindString(7, ICON_WITHOUT_TEXT);
           this.insertStmt.bindString(8, HIGHLIGHTED_ICON_WITHOUT_TEXT);
           this.insertStmt.bindString(9, Banner_Image);
           this.insertStmt.bindString(10, CHILDREN);
           this.insertStmt.bindString(11, ICON_WITH_TEXT);

           return this.insertStmt.executeInsert();
       }

       public Cursor fetchAllResources() {
            return database.query(DATABASE_TABLE, new String[] { KEY_TYPE,
                    KEY_ENCODING, KEY_WIDTH, KEY_WIDTH,KEY_DATA, KeyIId}, null, null, null,
                    null, null);
        }

       public List<String> selectTable(){
           List<String> list=new ArrayList<String>();
       Cursor c = db.rawQuery("SELECT * FROM " +
               DATABASE_TABLE , null);
       if(c.moveToFirst()){
           do{
               list.add(c.getString(0));
           }
           while(c.moveToNext());
       }
       if(c !=null && !c.isClosed()){
           c.close();
       }
       return list;
       }
       public void deleteAll(){
           this.db.delete(DATABASE_TABLE, null, null);
       }


       public void deleteAllNavigation(){
           this.db.delete(DATABASE_TABLE_NAVIGATION, null, null);
       }

       public List<String> selectAll(){
           List<String> list=new ArrayList<String>();
           Cursor cursor=this.db.query(DATABASE_TABLE, new String[]{"KEY_TYPE","KEY_ENCODING","KEY_WIDTH","KEY_HEIGHT","KEY_DATA","KeyIId"},
                   null, null, null, null, "KeyIId");

           if(cursor.moveToFirst()){
               do{
                   list.add(cursor.getString(0));
               }
               while(cursor.moveToNext());
           }
           if(cursor !=null && !cursor.isClosed()){
               cursor.close();
           }
           return list;
       }


       public List<String> selectAllNavigation(){
           List<String> list=new ArrayList<String>();
           Cursor cursor=this.db.query(DATABASE_TABLE_NAVIGATION, new String[]{"KEY,TYPE1","LINK_NAME","DISPLAY_NAME","FORWARD_ICON","HIGHLIGHTED_ICON_WITH_TEXT","ICON_WITHOUT_TEXT","HIGHLIGHTED_ICON_WITHOUT_TEXT","Banner_Image","CHILDREN,ICON_WITH_TEXT"},
                   null, null, null, null, "Key");

           if(cursor.moveToFirst()){
               do{
                   list.add(cursor.getString(0));
               }
               while(cursor.moveToNext());
           }
           if(cursor !=null && !cursor.isClosed()){
               cursor.close();
           }
           return list;
       }

    public WineDatabaseAdapter open() throws SQLException {
        dbHelper = new WineDatabaseHelper(context);
        database = dbHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        dbHelper.close();
    }

    public long  createNavigation(String key, String type, String link_name, String display_name,String forward_icon, String highlighted_icon_with_text, String banner_image,String highlighted_icon_without_text,String icon_without_text,String children,String icon_with_text ) { 

        ContentValues initialValues = createNavigationValues(key,type,link_name,display_name, forward_icon,
                highlighted_icon_with_text, banner_image, highlighted_icon_without_text,banner_image,highlighted_icon_without_text,icon_without_text);

        return database.insert(DATABASE_TABLE, null, initialValues);
    }

    public boolean updateNavigation (long rowId,String key,String type, String link_name,String display_name, String forward_icon, String highlighted_icon_with_text, String banner_image,String highlighted_icon_without_text,String icon_without_text,String children,String icon_with_text )
    {   ContentValues updateValues = createNavigationValues(key, type,link_name,display_name, forward_icon,
            highlighted_icon_with_text, banner_image, highlighted_icon_without_text,banner_image,highlighted_icon_without_text,icon_without_text);


        return database.update(DATABASE_TABLE, updateValues, KEY_ID + "="
                + rowId, null) > 0;
    }

    public boolean deleteNavigation(long rowId) {
        return database.delete(DATABASE_TABLE, KEY_ID + "=" + rowId, null) > 0;
    }

    public Cursor fetchAllNavigations() {
        return database.query(DATABASE_TABLE_NAVIGATION, new String[] { KEY_ID,TYPE1,FORWARD_ICON,LINK_NAME,HIGHLIGHTED_ICON_WITH_TEXT,Banner_Image,CHILDREN,ICON_WITHOUT_TEXT,HIGHLIGHTED_ICON_WITHOUT_TEXT,ICON_WITH_TEXT}, null, null, null,
                null, null);
    }

    public Cursor fetchNavigation(long rowId) throws SQLException {
        Cursor mCursor = database.query(true, DATABASE_TABLE_NAVIGATION, new String[] { KEY_ID,TYPE1,FORWARD_ICON,LINK_NAME,HIGHLIGHTED_ICON_WITH_TEXT,Banner_Image,CHILDREN,ICON_WITHOUT_TEXT,HIGHLIGHTED_ICON_WITHOUT_TEXT,ICON_WITH_TEXT},
                KEY_ID + "=" + rowId, null, null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    public long  createTodo(String iid,String type, String encoding, String data) 
    {
        ContentValues initialValues = createContentValues(iid,type, 
                 encoding, data);

        return database.insert(DATABASE_TABLE, null, initialValues);
    }

    public boolean updateTodo(long rowId,String iid, String type, 
             String encoding, String data ) {
        ContentValues updateValues = createContentValues(iid, type,
                 encoding, data);

        return database.update(DATABASE_TABLE, updateValues, KEY_ID + "="
                + rowId, null) > 0;
    }

    public boolean deleteTodo(long rowId) {
        return database.delete(DATABASE_TABLE, KEY_ID + "=" + rowId, null) > 0;
    }

    public Cursor fetchAllTodos() {
        return database.query(DATABASE_TABLE, new String[] { KEY_ID,KeyIId,
                KEY_TYPE, KEY_WIDTH,KEY_HEIGHT, KEY_ENCODING, KEY_DATA }, null, null, null,
                null, null);
    }

    public Cursor fetchTodo(long rowId) throws SQLException {
        Cursor mCursor = database.query(true, DATABASE_TABLE, new String[] {
                KEY_ID,KeyIId, KEY_TYPE, KEY_WIDTH, KEY_HEIGHT, KEY_ENCODING, KEY_DATA },
                KEY_ID + "=" + rowId, null, null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    private ContentValues createContentValues( String iid,String type, 
             String encoding, String data) {
        ContentValues values = new ContentValues();

        values.put(KeyIId, iid);
        values.put(KEY_TYPE, type);
        values.put(KEY_ENCODING, encoding);
        values.put(KEY_DATA, data);
        return values;
    }

    private ContentValues createNavigationValues(String key,String type, String link_name,String display_name, String forward_icon, String highlighted_icon_with_text, String banner_image,String highlighted_icon_without_text,String icon_without_text,String children,String icon_with_text ) {
        ContentValues values = new ContentValues();
        values.put(KEY, key);
        values.put(TYPE1, type);
        values.put(LINK_NAME, link_name);
        values.put(DISPLAY_NAME, display_name);
        values.put(FORWARD_ICON, forward_icon);
        values.put(HIGHLIGHTED_ICON_WITH_TEXT, highlighted_icon_with_text);
        values.put(Banner_Image, banner_image);
        values.put(CHILDREN, children);
        values.put(ICON_WITHOUT_TEXT, icon_without_text);
        values.put(HIGHLIGHTED_ICON_WITHOUT_TEXT, highlighted_icon_without_text);
        values.put(ICON_WITH_TEXT, icon_with_text);
        return values;
    }
}    

and I am inserting these values in a activity which is below like:

     for (int ii = 0; ii < resources.length(); ii++) 
                                            {
                                                type = resources.getJSONObject(ii).getString("type").toString();
                                                encoding = resources.getJSONObject(ii).getString("encoding").toString();
                                                data1 = resources.getJSONObject(ii).getString("data").toString();
                                                iid = resources.getJSONObject(ii).getString("id").toString();
                                                try{
                                                width=resources.getJSONObject(ii).getString("width").toString();
                                                height=resources.getJSONObject(ii).getString("height").toString();
                                                }
                                                catch(Exception e){
                                                    e.printStackTrace();
                                                }
                                                db = new WineDatabaseAdapter(HomePageWithPhoneIsOfflineDialog.this);
    db.insert(type,encoding,width,height, data1,iid);
   db.fetchAllResources()

It gives me null pointer exception on fetchAllResources() method. Instead when i print the values using string builder it is printing the values.

Edit:

It returns a null array i.e. without any values like[]. Are not the values are being inserted in the database? Then how to insert values in database.

Can Anyone tell me why this is happening.
Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:30:12+00:00Added an answer on May 26, 2026 at 4:30 pm
    public Cursor fetchAllResources() {
        return database.query(DATABASE_TABLE, new String[] { KEY_TYPE,
            KEY_ENCODING, KEY_WIDTH, KEY_WIDTH,KEY_DATA, KeyIId}, null, null, null,
            null, null);
    }
    

    Here your are using database which is never initialized => Null Pointer Exception
    .

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting some response from server side which i need to parse but
I am getting JSON response from the server..Inside that JSON string i am having
I have following array return from server side: [json] => Array ( [0] =>
I am getting the following response from server: <?xml version=1.0 encoding=UTF-8?> <tracks type=array> <track>
im getting response in json, but this wont parse the json response. what m
i am getting response from server in utf-8 but not able to read that.
in android when i am getting response from the server it's giving garbage values
I am making a social app in which I am getting response from server
I am doing form submission and validation using jQuery and from server side I
I'm getting a text from server side and I want to show one word

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.