I have created a program that parses an XML file. For simplicity's sake, let's just say that there is only 1 type of child element in the XML file which contains 4 attributes which are doubles (x1, y1, x2, y2) and represent the beginning and ending points of a line. I am using JDOM to parse the XML file. After grabbing each attribute and storing the data in doubles, I create a MyLine object (my own class for storing line data) and try to store the object in a List (java.util.List).
The following code shows this section of my program:
Everything works except of the last line. If I comment it out then I never run into any errors. I even tried using a Vector instead of a List and using addElement instead of add.
I am using MVC to design the program, so the idea behind this is that the Controller calls the Model's parseXML function like this...
... and the parseXML function will return the same string (url) that was passed in as its parameter if no errors were encountered during the execution of parseXML. If an error is encountered then the string "error" is returned and the else statement above is executed.
Now for the reason I am confused. The only way that the url string that is returned could be changed is if a MalformedURLException, a JDOMException, or an IOException are encountered. So I put System.out.println("error") statements in the catch blocks for each of these exceptions. I ran the program again and "error" was not printed in the console. So then I decided to alter the Controller's code a little to figure out what was going wrong. I changed it so that another String was receiving the returned String from parseXML and then comparing that returned String to the original and writing "true" or "false" to System.out stating whether the Strings matched or not. This is the code for that:
Strangely enough, still nothing was written to the console. So after realizing that I had added the Controller code to a try block I placed one more System.out call in the catch block for that statement. The code then looked like this...
... I ran the program again and "null" was being printed in the console from the System.out statement in the catch block. What I don't understand is how this excpetion is being thrown. I know that it occurs when I am trying to add a MyLine object to the list in the parseXML function because if I take this line out then it runs fine. Please help!
I don't think the url is null because in the catch block where the exception is caught I have a call to a function residing in the View class (my GUI) which displays a message dialog box with the error that was encountered. The url is sent as part of the message that is to be displayed and when the message box comes up, it definitely has the exact url that I entered.
I did change the comparison of strings to "c_model.parseXML(url).equals(url)" instead of "c_model.parseXML(url) == url", but that didn't change anything in the outcome.
OK, now my novice-ness catches up with me. How do I generate a stack trace and is it possible to generate it in Eclipse?
posted 15 years ago
Nevermind, I'm moron. I placed "ex.printStackTrace(System.out);" in the catch block where the exception was being caught. This is what I got...
try null java.lang.NullPointerException at edu.vt.cs.cs4984gui.glevine.Project1.Model.parseXML(Model.java:92) at edu.vt.cs.cs4984gui.glevine.Project1.Controller$LoadListener.actionPerformed(Controller.java:59) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)
posted 15 years ago
"try" and "null" were printed because they were from my attempts at solving this problem and I didn't remove them prior to printing the stack trace
posted 15 years ago
Being that it was a NULLPointerException, I thought maybe it had something to do with attempting to store a null value in linesList instead of a MyLine object. I placed the code below in after creating the new MyLine object and before trying to add the object to linesList, but "NOT NULL" was printed to the console, so the object is not null.
Glad to hear you've worked this out. How to spot a NullPointerException is a painful learning process everyone has to go through - so next time it will be a breeze!
Which is best to use Vector or LinkedList
Vector is legacy code, try not to use it. As Horatio points out its generally better to code to the interface, not the implementation. That way you don't have any hassles changing one List implementation for another somewhere doesn the line.
Sounds like you might benefit from a glance at the Collections tutorial when you have the time.
Originally posted by Greg Levine: Thanks, I feel pretty stupid because I should have figured this out, but with every new language comes new growing pains I guess.
Glad you sorted it out. Don't feel stupid - everyone comes across bugs that you just can't see!
I had one the other day where I was initialising a couple of numbers in a test case (int test = 050). They were all three digit numbers - what I had forgot is that leading zero indicates octal, just as '0x' does hex. Pretty basic error, but it took me literally ages to work out!
I knew I would regret that burrito. But this tiny ad has never caused regrets: