0

I am trying to store a SpannedString into an Sqlite3 database, unfortunately, there is no direct way to put it in a ContentValues object. From several other posts, I gathered that the solution is to convert the Spannable string to HTML before writing the DB. So I have this code (focus on 'name' and 'code', which are CharSequences with styles):

    values.put(CATEGORY_COL, category.ordinal());
    values.put(NAME_COL, Html.toHtml(new SpannedString(name),Html.TO_HTML_PARAGRAPH_LINES_INDIVIDUAL));
    values.put(CODE_COL, Html.toHtml(new SpannedString(code),Html.TO_HTML_PARAGRAPH_LINES_INDIVIDUAL) );
    values.put(DESCRIPTION_COL, description.toString());

The conversion of 'code' to html fails with an exception:

E  FATAL EXCEPTION: main
Process: com.commodo.aplX, PID: 24921
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
    at android.text.Html.withinParagraph(Html.java:617)
    at android.text.Html.withinBlockquoteIndividual(Html.java:456)
    at android.text.Html.withinBlockquote(Html.java:403)
    at android.text.Html.withinDiv(Html.java:338)
    at android.text.Html.withinHtml(Html.java:288)
    at android.text.Html.toHtml(Html.java:269)

I traced the code with the debugger and found where the exception is being generated. It is in the Html.java file, and the offending code tries to get the typeface family name (String returned by getFamily()) but it is null, and therefore the exception occurs next line, where "monospace".equals(null) .

int s = ((StyleSpan) style[j]).getStyle();

if ((s & Typeface.BOLD) != 0) {
  out.append("<b>");
}
if ((s & Typeface.ITALIC) != 0) {
  out.append("<i>");
}

if (style[j] instanceof TypefaceSpan) {
    String s = ((TypefaceSpan) style[j]).getFamily();
    if ("monospace".equals(s)) {
        out.append("<tt>");
    }
}

if (style[j] instanceof SuperscriptSpan) {
    out.append("<sup>");
}

It seems to me this is a bug. I would like to find a bypass for it, or another, not complicated, way of storing spannables in a database.

mplungjan
  • 169,008
  • 28
  • 173
  • 236
ilomambo
  • 8,290
  • 12
  • 57
  • 106

0 Answers0