简体   繁体   中英

XML Serialization with Simple Framework

I am using a framework called Simple, which is very helpful for serializing and deser. xml content. However I am not able to serialize a xml file to an object. The output object should be filled with information, but I get a Null Pointer Exception.

Here is my java code:

    InputStream is = getResources().openRawResource(R.xml.startingdata);
    Serializer ser = new Persister();
    Data data = ser.read(Data.class, is);

I have a file in res/xml/ called startingdata.xml. I am trying to serilize the data into a "Data" object. However I am getting a NullPointerException related to the data object.

Here is the startingdata.xml:

<data>
<categories>
    <category>Inbox</category>
    <category>Private</category>
    <category>Work</category>
    <category>Business</category>
</categories>

<todos>
    <todo>
        <id>1</id>
        <text>Explore the app!</text>
    </todo>

    <todo>
        <id>2</id>
        <text>Add more todos!</text>
        <date>2013-05-09 12:21:55 CET</date>
    </todo>
</todos>

I think the xml model classes are ok, and there is no problem with them, but I can post them if you need to see them.

Here are the model classes:

Data.java

@Root
public class Data {

 @ElementList
 public List<Category> categories;

 @ElementList
 public List<ToDo> todos;

}

@Element(name="todo")
 public class ToDo {

@Element(required=true)
public String id;

@Element(required=true)
public String text;

@Element(required=false)
public Date date;

 }

@Element(name="category")
public class Category {

    @Element(required=true)
    public String text;

}

I am using a Toast to check if the created data object was created.

Toast toast = Toast.makeText(getApplicationContext(), data.toString(), Toast.LENGTH_LONG);
        toast.show();

Here is the full stack trace:

05-09 14:13:38.407: E/AndroidRuntime(9246): FATAL EXCEPTION: main
05-09 14:13:38.407: E/AndroidRuntime(9246): java.lang.RuntimeException: Unable to start     activity ComponentInfo{com.todo.wanttodo/com.todo.wanttodo.MainActivity}: java.lang.NullPointerException
05-09 14:13:38.407: E/AndroidRuntime(9246):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-09 14:13:38.407: E/AndroidRuntime(9246):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-09 14:13:38.407: E/AndroidRuntime(9246):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-09 14:13:38.407: E/AndroidRuntime(9246):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-09 14:13:38.407: E/AndroidRuntime(9246):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-09 14:13:38.407: E/AndroidRuntime(9246):     at android.os.Looper.loop(Looper.java:137)
05-09 14:13:38.407: E/AndroidRuntime(9246):     at android.app.ActivityThread.main(ActivityThread.java:5041)
05-09 14:13:38.407: E/AndroidRuntime(9246):     at java.lang.reflect.Method.invokeNative(Native Method)
05-09 14:13:38.407: E/AndroidRuntime(9246):     at java.lang.reflect.Method.invoke(Method.java:511)
05-09 14:13:38.407: E/AndroidRuntime(9246):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-09 14:13:38.407: E/AndroidRuntime(9246):     at     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-09 14:13:38.407: E/AndroidRuntime(9246):     at dalvik.system.NativeStart.main(Native     Method)
05-09 14:13:38.407: E/AndroidRuntime(9246): Caused by: java.lang.NullPointerException
05-09 14:13:38.407: E/AndroidRuntime(9246):     at com.todo.wanttodo.MainActivity.onCreate(MainActivity.java:77)
05-09 14:13:38.407: E/AndroidRuntime(9246):     at android.app.Activity.performCreate(Activity.java:5104)
05-09 14:13:38.407: E/AndroidRuntime(9246):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-09 14:13:38.407: E/AndroidRuntime(9246):     at         android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-09 14:13:38.407: E/AndroidRuntime(9246):     ... 11 more

Thank you in advance!

Are you sure about the startingdata.xml file?

<?xml version="1.0" encoding="UTF-8"?>
<data>
    <categories>
        <category>Inbox</category>
        <category>Private</category>
        <category>Work</category>
        <category>Business</category>
    </categories>

    <todos>
        <todo>
            <id>1</id>
            <text>Explore the app!</text>
        </todo>

        <todo>
            <id>2</id>
            <text>Add more todos!</text>
           <date>2013-05-09 12:21:55 CET</date>
       </todo>
    </todos>
</data>

You can try a different way using XmlPullParser: android - training , android - reference

In my project, I set @Default in my DAO classes.

@Root(name="data")
@Default(value = DefaultType.FIELD, required = false)
public class Data {

   @ElementList
   public List<Category> categories;

   @ElementList
   public List<ToDo> todos;

    ....

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM