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

  • SEARCH
  • Home
  • 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 6930615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:28:12+00:00 2026-05-27T11:28:12+00:00

I am trying to insert data of Books by parsing XML file. It allows

  • 0

I am trying to insert data of Books by parsing XML file. It allows me to parse the file but throws me error java.lang.NullPointerException on getWritableDatabase.
It throws me error in MyXMLHandler.java file at line:

  xmDB = new XMLDatabaseManager(context);
  xmDB.insertFeed(currentValue);

I have my code below which I use to develop it. I have been working on this for a long time today but cannot figure out the error. It will be really helpful if you can help me here.

package org.database.databasemanager;

import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.util.Log;

public class XMLDatabaseManager {

// the activity or appliation that is creating an object
Context context;

private SQLiteDatabase db;

private final String DATABASE_NAME = "Main.db";
private final int DATABASE_VERSION = 1;

// Table name
public final String TABLE = "events";

// Columns
public static final String TIME = "time";
public final String TITLE = "title";

private final String TAG = "MyActivity";

public XMLDatabaseManager(Context context){
    this.context = context;

    EventDataSQLHelper helper = new EventDataSQLHelper(context);
    this.db = helper.getWritableDatabase();

}



public class EventDataSQLHelper extends SQLiteOpenHelper {
    public EventDataSQLHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "create table " + TABLE + "( " + BaseColumns._ID
                + " integer primary key autoincrement, " 
                + TITLE + " text not null);";
        Log.d("EventsData", "onCreate: " + sql);
        db.execSQL(sql);

        Log.v(TAG, "secondnameText");
    }

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

    }

}


public boolean insertFeed( String title) {
      ContentValues values = new ContentValues();
      values.put("title", title);
      return (this.db.insert(TABLE, null, values) > 0);
}


}

The following code used for Parsing XML file and inserting at the end of the element.

package org.database.databasemanager;
import org.database.databasemanager.XMLDatabaseManager;
import org.database.databasemanager.XMLDatabaseManager.EventDataSQLHelper;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.Cursor;
 import android.util.Log;

 public class MyXMLHandler extends DefaultHandler {

Boolean currentElement = false;
String currentValue = null;
Context context;
private SQLiteDatabase db;
private XMLDatabaseManager xmDB;
public static SitesList sitesList = null;
private final String TAG = "KEY";

public static SitesList getSitesList() {
    return sitesList;
}

public static void setSitesList(SitesList sitesList) {
    MyXMLHandler.sitesList = sitesList;
}

/** Called when tag starts ( ex:- <name>AndroidPeople</name>
 * -- <name> )*/
@Override
public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {

    currentElement = true;

    if (localName.equals("Books"))
    {
        sitesList = new SitesList();

    } 
    }

    public void endElement(String uri, String localName, String qName)
throws SAXException {

     currentElement = false;

    /** set value */
    if (localName.equalsIgnoreCase("title")){

        // Get the value here 
        Log.d(TAG, currentValue);

                        ERROR THROWN HERE
                       --------------------------

         xmDB = new XMLDatabaseManager(context);
         xmDB.insertFeed(currentValue);

    }


}

/** Called to get tag characters ( ex:- <name>AndroidPeople</name>
 * -- to get AndroidPeople Character ) */
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {

    if (currentElement) {
        currentValue = new String(ch, start, length);
        currentElement = false;
    }

}
}

Activity class :

package org.database.databasemanager;

 import java.net.URL;

 import javax.xml.parsers.SAXParser;
 import javax.xml.parsers.SAXParserFactory;

 import org.xml.sax.InputSource;
 import org.xml.sax.XMLReader;

 import android.app.Activity;
 import android.os.Bundle;
 import android.util.Log;
  import android.widget.LinearLayout;
 import android.widget.TextView;

public class DatabaseManagerActivity extends Activity {


XMLDatabaseManager db;
private final String TAG = "SIZE";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

    try{

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        db = new XMLDatabaseManager(this);
        SitesList sitesList = null;
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(1);

        try {

            /** Handling XML */
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();

            /** Create handler to handle XML Tags ( extends DefaultHandler ) */
            MyXMLHandler myXMLHandler = new MyXMLHandler();
            xr.setContentHandler(myXMLHandler);
            //xr.parse(new InputSource(sourceUrl.openStream()));
            xr.parse(new InputSource(getAssets().open("Books1.xml")));

        } catch (Exception e) {
            System.out.println("XML Pasing Excpetion = " + e);
            e.printStackTrace();

        }
        /** Get result from MyXMLHandler SitlesList Object */
        sitesList = MyXMLHandler.sitesList;
        /** Set the layout view to display */
        setContentView(layout);

    }
    catch (Exception e)
    {
        Log.e("ERROR", e.toString());
        e.printStackTrace();
    }

}
}
  • 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-27T11:28:13+00:00Added an answer on May 27, 2026 at 11:28 am

    Your MyXMLHandler has the context member, but I don’t see anywhere it gets updated. Make sure you supply a valid value for that before trying to create the database.

    Also, you should only create the database once and keep a handle to it rather than creating a new handle at the end of every element. Inserts are already slow, no need to make them slower!

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

Sidebar

Related Questions

I was trying to insert new data into an existing XML file, but it's
While trying to insert data to my SQl db i get the following error
I am trying to insert data in table, but if it already exists in
I'm trying to insert data into the SQLite database using get EditText, but I
I was trying to insert new data int my database called KEY_DAY but when
Im trying to insert data to my database, and it gives me an error
I am trying to insert data from a csv file into mysql using BigDump.
i'm trying to insert data into xml using php domdocument. however when i hit
I'm trying to insert data about a user on my website but I think
i am trying to insert data from excel sheet into database in java. for

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.