gpt4 book ai didi

Android数据库插入在XML解析时抛出NULLPointerException

转载 作者:行者123 更新时间:2023-11-30 04:26:06 25 4
gpt4 key购买 nike

我正在尝试通过解析 XML 文件来插入图书数据。它允许我解析文件,但在 getWritableDatabase 上抛出错误 java.lang.NullPointerException。它在 MyXMLHandler.java 文件中抛出错误:

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

下面是我用来开发它的代码。我今天已经为此工作了很长时间,但无法找出错误。如果你能在这里帮助我,那将非常有帮助。

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);
}


}

以下代码用于解析 XML 文件并在元素末尾插入。

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 类:

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();
}

}
}

最佳答案

您的 MyXMLHandler 具有 context 成员,但我没有看到它有任何更新。确保在尝试创建数据库之前为其提供有效值。

此外,您应该只创建一次数据库并保留它的句柄,而不是在每个元素的末尾创建一个新的句柄。插入已经很慢了,没必要让它们变慢!

关于Android数据库插入在XML解析时抛出NULLPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8482855/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com