テーブルにデータを挿入しようとしていますが、行 に例外java.lang.NullPointerExceptionが発生しています
ih.bind(coldesc,メッセージ);
. 問題が databaHelper にあるのか、メソッドにあるのかわかりません。
これは、GMAIL からすべてのメールを取得し、非マルチパート メールをテーブルに挿入するための retrieveEmails メソッドですih.bind(coldesc,message);
。
public void retrieveEmails(){
Properties props = new Properties();
props.put("mail.pop3.host" , "pop.gmail.com");
props.put("mail.pop3.user" , username);
// Start SSL connection
props.put("mail.pop3.socketFactory" , 995 );
props.put("mail.pop3.socketFactory.class" , "javax.net.ssl.SSLSocketFactory" );
props.put("mail.pop3.port" , 995);
Session session = Session.getDefaultInstance(props , new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication( username , password);
}
});
try {
Store store = session.getStore("pop3");
store.connect("pop.gmail.com", 995 , username , password);
Folder fldr = store.getFolder("Inbox");
fldr.open(Folder.HOLDS_MESSAGES);
int totalMsg = fldr.getMessageCount();
int unread = fldr.getUnreadMessageCount();
System.out.println("Total MSG: " + totalMsg + " unreaed: " + unread );
Message[] messages = fldr.getMessages();
//Message[] msg = fldr.search(new FlagTerm(new Flags(Flag.SEEN), false));
Message[] msg = messages;
System.out.print("size " + messages .length);
if (messages.length == 0) System.out.println("No messages found.");
DatabaseHelper dbHelper = DatabaseHelper.getHelper(getApplicationContext());
db = dbHelper.getWritableDatabase();
db.beginTransaction();
for (int i = 0; i < messages.length; i++) {
System.out.println("Subject : " + messages[i].getSubject());
if(messages[i].getSubject().contains("Order")){
System.out.println("Message " + (i + 1));
System.out.println("From : " + messages[i].getFrom()[0]);
System.out.println("Subject : " + messages[i].getSubject());
Object content = messages[i].getContent();
Object objRef = msg[i].getContent();
if (!(objRef instanceof Multipart)) {
String message =((String)content).replace("<br />", "").replace("<strong>", "").replace("</strong>", "").replace("<div>", "").replace("<h1>", "");
message = message.replace("</div>", "").replace("</h1>", "");
System.out.print(message);
InsertHelper ih = null;
try {
ih = new InsertHelper(db, DatabaseHelper.table);
final int coldesc = ih.getColumnIndex(DatabaseHelper.column);
ih.bind(coldesc,message);
ih.execute();
db.setTransactionSuccessful();
Log.e("","Inserted");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (ih != null)
ih.close();
if (db != null) {
db.endTransaction();
// db.close();
}
}
//return;
}
else{
Multipart multipart = (Multipart) objRef;
//Multipart multipart = (Multipart) msg[i].getContent();
for (int x = 0; x < multipart.getCount()-1; x++) {
BodyPart bodyPart = multipart.getBodyPart(x);
String disposition = bodyPart.getDisposition();
if (disposition != null && (disposition.equals(BodyPart.ATTACHMENT))) {
System.out.println("Mail have some attachment : ");
DataHandler handler = bodyPart.getDataHandler();
System.out.println("file name : " + handler.getName());
} else {
System.out.println(bodyPart.getContent());
}
}
}
}
}
fldr.close(true);
store.close();
Log.d("Store", "Store was closed");
} catch(Exception exc) {
Log.e("",exc.toString());
}
}
ここで私のデータベースヘルパークラス:
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String table = "tblorder";
public static final String column = "description";
private static final String DATABASE_NAME = "commments.db";
private static final int DATABASE_VERSION = 1;
private static DatabaseHelper instance;
// Database creation sql statement
private static final String DATABASE_CREATE = "create table "
+ table + "("+ column
+ " nvarchar(20) NULL);";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(DatabaseHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + table);
onCreate(db);
}
public static synchronized DatabaseHelper getHelper(Context context) {
if (instance == null)
instance = new DatabaseHelper(context);
return instance;
}
}
スタック トレースは次のとおりです。
03-26 10:20:39.200: W/System.err(17132): java.lang.NullPointerException
03-26 10:20:39.210: W/System.err(17132): at android.database.DatabaseUtils$InsertHelper.bind(DatabaseUtils.java:1166)
03-26 10:20:39.210: W/System.err(17132): at com.example.gmailreceiver.MainActivity.retrieveEmails(MainActivity.java:247)
03-26 10:20:39.220: W/System.err(17132): at com.example.gmailreceiver.MainActivity$2.onClick(MainActivity.java:176)
03-26 10:20:39.225: W/System.err(17132): at android.view.View.performClick(View.java:3660)
03-26 10:20:39.230: W/System.err(17132): at android.view.View$PerformClick.run(View.java:14398)
03-26 10:20:39.235: W/System.err(17132): at android.os.Handler.handleCallback(Handler.java:605)
03-26 10:20:39.240: W/System.err(17132): at android.os.Handler.dispatchMessage(Handler.java:92)
03-26 10:20:39.245: W/System.err(17132): at android.os.Looper.loop(Looper.java:137)
03-26 10:20:39.250: W/System.err(17132): at android.app.ActivityThread.main(ActivityThread.java:4517)
03-26 10:20:39.255: W/System.err(17132): at java.lang.reflect.Method.invokeNative(Native Method)
03-26 10:20:39.255: W/System.err(17132): at java.lang.reflect.Method.invoke(Method.java:511)
03-26 10:20:39.255: W/System.err(17132): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:995)
03-26 10:20:39.260: W/System.err(17132): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
03-26 10:20:39.260: W/System.err(17132): at dalvik.system.NativeStart.main(Native Method)