Forums Register Login

XML DOM catch null

+Pie Number of slices to send: Send
Hello friends,

I'll try to read a xml file, but it goes to the catch block and returns null cause there some nodes null and I didn't know whats the best way to read this.

Here is a part of the xml file (I want only a part from the episode node it counts 134):



Here is some Java code:


and here is my output:
Root element : Data
Nest Länge 134

Current Element :Episode
Staff id : 4712380
First Name : Blood Rush: Part One
Last Name : 2
Nick Name : de
Overview :
SeasonNumber : 0
lastupdated : 1391991457
seasonid : 488842
Catch-Block null

thanks for any hints

Cheers Chris
+Pie Number of slices to send: Send
Assuming you wrote the Elements class, this is way to verbose and the method should handle the null case internally.
I would rather see
and have in Element
If you didn't write Element then you could do a similar thing with a static helper method.
+Pie Number of slices to send: Send
I'll get the Element from this:

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
+Pie Number of slices to send: Send
Yes, that's the right Element class to use. But as Cary said, sometimes you're going to get a null value which means something like "no such thing was found" and you're going to need code to test for that possibility.

More than that, you're going to have to decide what to do when you get the null value. So far you've decided that you're going to terminate the program with no further processing, but clearly you're not happy with that decision.
+Pie Number of slices to send: Send
Ok, thanks Paul

so my goal is, to write the data to an MySQL database and the sout is only a demo output to see what happen...

I have try the part from Carey (thank you for the fast response by the way)

but I get the same null

what have I done?


and call the method

If there an other better or easyer way to put the data into a database...
+Pie Number of slices to send: Send
A helper method like this
Called lilke

If getElementsByTagName(), or item() can also return null then you'd have to take that into account in the helper method.
+Pie Number of slices to send: Send
What have you done? Well, not what Carey suggested. But look:



In this expression, I think your code excludes the possibility that eElement is null. But any of these things can be null:

eElement.getElementsByTagName("showid")

eElement.getElementsByTagName("showid").item(0)

eElement.getElementsByTagName("showid").item(0).getTextContent()

You have to test each of them for null before proceeding to find the next one, otherwise you're going to get an NPE. The code to do that is like Carey's but more complicated.
+Pie Number of slices to send: Send
A variation where the tag is not printed at all if it's null.
+Pie Number of slices to send: Send
ahhhh now the coin is falling

Thank you Carey and Paul I'll try it later at home and say you what happen

Cheers
+Pie Number of slices to send: Send
so, I have tryed diffrend versions and this is my output...

Current Element :Episode
id : com.sun.org.apache.xerces.internal.dom.DeepNodeListImpl@c856c98
EpisodeName : com.sun.org.apache.xerces.internal.dom.DeepNodeListImpl@6b7760e9
EpisodeNumber : com.sun.org.apache.xerces.internal.dom.DeepNodeListImpl@82a53b0
Language : com.sun.org.apache.xerces.internal.dom.DeepNodeListImpl@446dbe9e
Overview : com.sun.org.apache.xerces.internal.dom.DeepNodeListImpl@632bfb27
SeasonNumber : com.sun.org.apache.xerces.internal.dom.DeepNodeListImpl@338bbf83
lastupdated : com.sun.org.apache.xerces.internal.dom.DeepNodeListImpl@6966dd91
seasonid : com.sun.org.apache.xerces.internal.dom.DeepNodeListImpl@40d6076d
showid : com.sun.org.apache.xerces.internal.dom.DeepNodeListImpl@7c983e93


Its only for test output...
+Pie Number of slices to send: Send
You forgot item(0).
+Pie Number of slices to send: Send
I take that back...

Why are you calling toString()? Unless that object has overridden the Object#toString() method you get the object's hashCode (i.e. ID), which is what your output shows.

The code you had with item(0).getContent() gave you what you wanted, didn't it? What are you trying to achieve that you didn't have before?
+Pie Number of slices to send: Send
what I want is to store selected (the part from the element) in my database and for me is it equal if its null or filled up with data...

I have make toString() to get any result.

When I try it with the item(0).getTextContent, it goes in the first node and then runs in the catch with null like this

Current Element :Episode
id : 4712380
EpisodeName : Blood Rush: Part One
EpisodeNumber : 2
Language : de
Overview :
SeasonNumber : 0
lastupdated : 1391991457
seasonid : 488842
Catch-Block null

I have try XML Read and it worked but I cant select item for item

maybe I have a node in my brain I don't know

And the way to store the data from the file in the database is equal ...
+Pie Number of slices to send: Send
Ok,  let's see your current code snippet again so I can see where you're at.
1
+Pie Number of slices to send: Send
I'm guessing that you did not implement what Paul and I suggested, and that is, you'll have to check for null at EACH step.

Then you can change this

+Pie Number of slices to send: Send
I highly recommend going to the Javadocs and looking up the DOM methods that you are calling and to see what types they are returning and whether or not they throw exceptions.
+Pie Number of slices to send: Send
at this time I haven't try your last post... but her is the code

+Pie Number of slices to send: Send
I showed you two methods, one that gets text that you'll have to print yourself and one to also do the printing. You are using the one that doesn't print but you've taken away all the code that prints the string.

Edit: Never mind. I see you used a diifferent method name.
+Pie Number of slices to send: Send
Dam it works

I don't ne the printText method.

Thanks anyway yeah

1
+Pie Number of slices to send: Send
Glad it worked for you.
Slime does not pay. Always keep your tiny ad dry.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1195 times.
Similar Threads
How to get a xml id value in java
Unable to figureout why repaint(); is not doing its job
Help XML parsing in Java
reading multple nested variables from XML
Xml data to hashmap for onInfowindowclick Google Maps v2
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 19:21:24.