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();
}
}
}
Your
MyXMLHandlerhas thecontextmember, 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!