Win a copy of Bad Programming Practices 101 (e-book) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

XML DOM catch null  RSS feed

 
Ranch Hand
Posts: 50
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Saloon Keeper
Posts: 4361
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chris Ernst
Ranch Hand
Posts: 50
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
 
Sheriff
Posts: 23594
48
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chris Ernst
Ranch Hand
Posts: 50
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Carey Brown
Saloon Keeper
Posts: 4361
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Sheriff
Posts: 23594
48
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Carey Brown
Saloon Keeper
Posts: 4361
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A variation where the tag is not printed at all if it's null.
 
Chris Ernst
Ranch Hand
Posts: 50
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ahhhh now the coin is falling

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

Cheers
 
Chris Ernst
Ranch Hand
Posts: 50
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Carey Brown
Saloon Keeper
Posts: 4361
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You forgot item(0).
 
Carey Brown
Saloon Keeper
Posts: 4361
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Chris Ernst
Ranch Hand
Posts: 50
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ...
 
Carey Brown
Saloon Keeper
Posts: 4361
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok,  let's see your current code snippet again so I can see where you're at.
 
Carey Brown
Saloon Keeper
Posts: 4361
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Carey Brown
Saloon Keeper
Posts: 4361
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chris Ernst
Ranch Hand
Posts: 50
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
at this time I haven't try your last post... but her is the code

 
Carey Brown
Saloon Keeper
Posts: 4361
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chris Ernst
Ranch Hand
Posts: 50
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dam it works

I don't ne the printText method.

Thanks anyway yeah

 
Carey Brown
Saloon Keeper
Posts: 4361
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad it worked for you.
 
Let's go to the waterfront with this tiny ad:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!