0

So I think I was questioning the wrong way in previous threads. I better put it like this. I'm trying to parse the XML data:

<playlist>
<title>![CDATA[New Playlist]]</title>
<items>
<item>
<title>HAMD(LA ILAHA ILLALLAH)</title>
<description>Recited By :Alhaaj Muhammad Owais Raza Qadri -- Written By: Mufti-e-Azam Hind Molana Mustafa Raza Khan Noori</description>
<image>http://xxx.xxx.xxx.xxx/~kanz/video/Images/9.jpg</image>
<startFromThis>true</startFromThis>
<duration>510</duration>
<source>http://xxx.xxx.xxx.xxx/~kanz/video/flv/9.flv</source>
<sourceAlt>http://xxx.xxx.xxx.xxx/~kanz/video/mp4/9.mp4</sourceAlt>
<sourceType>direct</sourceType>
</item>
</items>
</playlist>

In my Activity, what I did was this but the logcat gives null pointer exception. Here's my Activity and below it the logcat error.

static final String URL = "http://xxx.xxx.xxx.xxx/~kanz/video/XML/9.xml";

// XML node keyse.
static final String KEY_SONG = "item"; // parent node
static final String KEY_TITLE = "title";
static final String KEY_ARTIST = "description";
static final String KEY_THUMB_URL = "image";
static final String KEY_DURATION = "duration";

NodeList nl = doc.getElementsByTagName(KEY_SONG);

// looping through all song nodes <song>
for (int i = 0; i < nl.getLength(); i++) {
    // creating new HashMap
    HashMap<String, String> map = new HashMap <String, String>();
    Element e = (Element) nl.item(i);
    // adding each child node to HashMap key => value
    map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
    map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
    map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
    map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));

    // adding HashList to ArrayList
    songsList.add(map);
}

Logcat gives Unexpected Token error and returns null which causes null pointer exception.

03-28 14:31:34.059: E/Error:(895): Unexpected token (position:TEXT @1:4 in java.io.StringReader@40d220e0) 
03-28 14:31:34.080: I/System.out(895): null
03-28 14:42:59.560: D/AndroidRuntime(978): Shutting down VM
03-28 14:42:59.560: W/dalvikvm(978): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
03-28 14:42:59.639: E/AndroidRuntime(978): FATAL EXCEPTION: main
03-28 14:42:59.639: E/AndroidRuntime(978): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.razatubevidurl/com.example.razatubevidurl.CustomizedListView}: java.lang.NullPointerException
03-28 14:42:59.639: E/AndroidRuntime(978):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-28 14:42:59.639: E/AndroidRuntime(978):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-28 14:42:59.639: E/AndroidRuntime(978):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-28 14:42:59.639: E/AndroidRuntime(978):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-28 14:42:59.639: E/AndroidRuntime(978):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-28 14:42:59.639: E/AndroidRuntime(978):  at android.os.Looper.loop(Looper.java:137)
03-28 14:42:59.639: E/AndroidRuntime(978):  at android.app.ActivityThread.main(ActivityThread.java:5041)
03-28 14:42:59.639: E/AndroidRuntime(978):  at java.lang.reflect.Method.invokeNative(Native Method)
03-28 14:42:59.639: E/AndroidRuntime(978):  at java.lang.reflect.Method.invoke(Method.java:511)
03-28 14:42:59.639: E/AndroidRuntime(978):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-28 14:42:59.639: E/AndroidRuntime(978):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-28 14:42:59.639: E/AndroidRuntime(978):  at dalvik.system.NativeStart.main(Native Method)
03-28 14:42:59.639: E/AndroidRuntime(978): Caused by: java.lang.NullPointerException

Please tell me what I'm doing wrong. I thought my previous question was vague and in this one, I tried my best to make the question clear and understandable.

2
  • where you are fetching xml content? Commented Mar 28, 2013 at 15:14
  • @Sajmon From the URL initialized to the variable URL. Commented Mar 28, 2013 at 19:07

1 Answer 1

1

I guess it's something about encoding. Maybe your parser doesn't like the exclamation mark (!). Can you make sure that you use the XML file with the correct encoding? (UTF-8)

Otherwise, you have this problem: KXmlParser throws "Unexpected token" exception at the start of RSS pasing.

I googled around and it looks like the easiest fix is here. You can try adding this method. (From the tutorial you use, put this in the ImageLoader class.)

import java.io.PushbackInputStream;
private InputStream checkForUtf8BOMAndDiscardIfAny(InputStream inputStream) throws IOException {
PushbackInputStream pushbackInputStream = new PushbackInputStream(new BufferedInputStream(inputStream), 3);
byte[] bom = new byte[3];
if (pushbackInputStream.read(bom) != -1) {
    if (!(bom[0] == (byte) 0xEF && bom[1] == (byte) 0xBB && bom[2] == (byte) 0xBF)) {
        pushbackInputStream.unread(bom);
    }
}
return pushbackInputStream; }

Then you can do

InputStream is=conn.getInputStream();
is = checkForUtf8BOMAndDiscardIfAny(is);

I don't have a chance to test the method but it should do it. Also, please see Byte order mark screws up file reading in Java.

Sign up to request clarification or add additional context in comments.

7 Comments

does the link you sent in the answer parse XML from server? Because mine is stored on the Server.
Yes, it's sent from a server. You can try creating a new local xml file with the same content and parse it.
@EnterLevelDev I think the problem you guessed right. But the solution you sent uses PullParser while I'm using XMLParser. Can you help me solve it with XMLParser?
@AbdusSamiKhan I'd love to but I don't have your full code that I can play with. Any of these helps you stackoverflow.com/questions/1835430/…
check this link, it's my previous question and it has all the code. stackoverflow.com/questions/15656570/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.