• 0

[Java] How to suppress Fatal Errors in the log


Question

Hi everyone,

I have a Java program that does some XML manipulation. It throws this error in my log:

[Fatal Error] :-1:-1: Premature end of file.

It is not causing any problems and it is beyond my control to fix it. Is there any way I can suppress these errors and clean up my log? Or maybe, change the message to be more friendly?

Thanks for any help. :)

Recommended Posts

  • 0

I believe that I have narrowed it down to here:

public static Document parse(String xml) throws SAXException,IOException {

InputSource source = new InputSource(new StringReader(xml));

DOMParser parser = new DOMParser();

//[Fatal Error] :-1:-1: Premature end of file.

parser.parse(source);

return parser.getDocument();

}

InputSource is from org.xml.sax.InputSource

DOMParser is from org.apache.xerces.parsers.DOMParser

I did not write this code but I currently maintain it and am not sure why the error is being thrown...

  • 0
  Annorax said:
I believe that I have narrowed it down to here:

public static Document parse(String xml) throws SAXException,IOException {

InputSource source = new InputSource(new StringReader(xml));

DOMParser parser = new DOMParser();

//[Fatal Error] :-1:-1: Premature end of file.

parser.parse(source);

return parser.getDocument();

}

InputSource is from org.xml.sax.InputSource

DOMParser is from org.apache.xerces.parsers.DOMParser

I did not write this code but I currently maintain it and am not sure why the error is being thrown...

I haven't done java in a few years but I believe this is where the error is getting thrown. The // bit is a comment of what the error looks like. Try catching the SAXException and IOException then ignoring them or having them return a better message.

Would help more with a bit of code but like I said it's been a long time.

I'm assuming that the problem is with the files that you're trying to open (they're incorrectly terminated) and that they can't be changed.

Edited by monkey13
  • 0
  Annorax said:
I believe that I have narrowed it down to here:

public static Document parse(String xml) throws SAXException,IOException {

InputSource source = new InputSource(new StringReader(xml));

DOMParser parser = new DOMParser();

//[Fatal Error] :-1:-1: Premature end of file.

parser.parse(source);

return parser.getDocument();

}

InputSource is from org.xml.sax.InputSource

DOMParser is from org.apache.xerces.parsers.DOMParser

I did not write this code but I currently maintain it and am not sure why the error is being thrown...

why is the error in your source code?

  • 0

OK a bit of googleing reveals that it is a SAXException so try something like. (will need more work)

public static Document parse(String xml) throws SAXException,IOException {

try{

InputSource source = new InputSource(new StringReader(xml));

DOMParser parser = new DOMParser();

//[Fatal Error] :-1:-1: Premature end of file.

parser.parse(source);

return parser.getDocument();

}

catch (SAXException sxe)

{ // Do what ever you want to do with the exception}

}

}

Syntax and brackets might be a bit off but it's a start for you.

@Ste. The someone has just put the error in as a comment because this bit keeps throwing that error. I think.

  • 0
  JamesCherrill said:
If it's from an Exception, someone has to (1) create it and (2) throw it. That's the code you need to find.

The problem should be in this bit of code. Althought the problem isn't with the code but the fact that the xml files aren't terminated in the manor that the java xml parser likes.

//class constructor sets up for SAXException.

public static Document parse(String xml) throws SAXException ,IOException{

//Create objects and get xml

InputSource source = new InputSource(new StringReader(xml));

DOMParser parser = new DOMParser();

//[Fatal Error] :-1:-1: Premature end of file.

//Parse xml file. SAXException thrown here due to incorrect/missing xml end of file.

parser.parse(source);

return parser.getDocument();

}

Although another way to fix it might be to append the xml that is passed to the string reader with the correct end of file token.

No need for try and catch then.

Also how is the xml that is passed to this class loaded in? It could be that the EOF token is getting chopped off then. That would mean that the files are ok and the code is introducing the error.

  • 0

Maybe if you can find the ACTUAL CODE where the Exception is created and thrown then you could see the exact conditions under which that happens, and maybe that will enable you either to identify a work-around or sub-class the offending code to override that behaviour. The code fragment you keep on posting is NOT the code we need to look at.

  • 0
  JamesCherrill said:
Maybe if you can find the ACTUAL CODE where the Exception is created and thrown then you could see the exact conditions under which that happens, and maybe that will enable you either to identify a work-around or sub-class the offending code to override that behaviour. The code fragment you keep on posting is NOT the code we need to look at.

I'll assume you are talking to me.

1. It's not my code fragment it's Annorax's

2. You obviously don't understand how JAVA works. Quick lesson for you,

This is the class constructor

public static Document parse(String xml) throws SAXException ,IOException{

The bit I've bolded means this class can throw these exceptions.

parser.parse(source);

This line throws the exception. When the xml parser InputSource tries to parse the xml given to it it doesn't like the way the xml ends. So it throws the SAXException.

The throw statement is within the code of the InputSource. Since this is part of the JAVA platform source you wouldn't go in and change that.

End of JAVA lesson.

Edit:

  JamesCherrill said:

Probably irrelevent. This is for the same error if you pass your xml object through the parser twice. Unless the xml string is passed through the parser somewhere else in the code before it gets here that's not the cause.

  • 0

Thank you all for trying to help me.

Yes, the comment with the error is right above where the error is thrown i.e. "parser.parse(source);" throws the error. This is not an object I have access to as it's in xercesImpl.jar.

The try/catch is a good idea and one I thought of. When I implement it as follows:

InputSource source = new InputSource(new StringReader(xml));

DOMParser parser = new DOMParser();

try {

parser.parse(source);

} catch (Exception e) {

System.out.println("Fatalr");

}

return parser.getDocument();

I get something like this in my log:

Fatal error

[Fatal Error] :-1:-1: Premature end of file.

Fatal error

[Fatal Error] :-1:-1: Premature end of file.

Fatal error

[Fatal Error] :-1:-1: Premature end of file.

So the "Premature end of file" error is thrown from within a class that I cannot edit.

  • 0

Try catch (SAXException sxe) instead of catch(Exception e)

Although from the link JamesCherrill posted

http://dschneller.blogspot.com/2008/01/sax...ure-end-of.html

If you parse something twice you will get an error. Is the xml parsed anywhere else in the code? If it is this is a really stupid way of doing it. Parse once then manipulate many.

The way this Daniel Schneller does it on his blog is stupid. He parses the whole thing once then looks at the type then parses the whole thing again. What a waste of time. Parse once, store, then manipulate based on the type.

  • 0

monkey13:

Not sure what you're smoking - are you always so aggressive?

Anyway, in the name of accuracy...

public static Document parse(.. is NOT a "class constructor"

Constructors are of the form .... ClassName(params...) { ...

I didn't suggest changing the Java platform source, I suggested possibly subclassing it.

Have a nice day

J

  • 0
  JamesCherrill said:
monkey13:

Not sure what you're smoking - are you always so aggressive?

Anyway, in the name of accuracy...

public static Document parse(.. is NOT a "class constructor"

Constructors are of the form .... ClassName(params...) { ...

You started mouthing of about

  JamesCherrill said:
Maybe if you can find the ACTUAL CODE .... The code fragment you keep on posting is NOT the code we need to look at.

Considering I take caps to mean shouting your the one who started getting aggressive.

Time for another JAVA lesson.

public static Document parse(String xml) throws SAXException ,IOException{

Is the constructor for this class. If not where is the constructor then? How else is the class instantiated?

Lets take the basic example from the JAVA tutorial

  Quote
A class contains constructors that are invoked to create objects from the class blueprint. Constructor declarations look like method declarations?except that they use the name of the class and have no return type. For example, Bicycle has one constructor:

public Bicycle(int startCadence, int startSpeed, int startGear) {

gear = startGear;

cadence = startCadence;

speed = startSpeed;

}

To create a new Bicycle object called myBike, a constructor is called by the new operator:

Bicycle myBike = new Bicycle(30, 0, 8);

now the line could read public static Bicycle(int startCadence, int startSpeed, int startGear) throws SAXException ,IOException

It will still be the constructor. I call it the class constructor because is is also the definition of the class. Ok that my not be technically correct in some people's eyes but it's how I was taught. It is however definitely a constructor.

End of JAVA lesson.

  • 0

OK, I'm not a Java guy, as James well knows, but a static method cannot be a constructor.

public [b]static[/b] Document parse(String xml) throws SAXException ,IOException{
///...

It's a static method called parse, not a constructor.

Anyhoo, relax everyone. It's just code! :)

  • 0
  azcodemonkey said:
OK, I'm not a Java guy, as James well knows, but a static method cannot be a constructor.

Technically yes it is. You just don't instantiate it in the same way as a non static class. You don't need to because JAVA handles it all behind the scenes. With a non-static method you would have to do Bicycle myBike = new Bicycle(30, 0, 8); With a static method you can't instantiate an object of this class with a lifetime. ie. the object only exists for as long as the static method is running.

Think of it like this. When you use .parse(xml) what it actually does is creates an object (with the constructor) that you can not access performs the parse() function and then deletes the object.

Back on track to helping Annorax now.

Annorax use catch (SAXException sxe) I've remembered that this kind of exception is actually an "Exception e" and a "String message" (SAXException(String message, Exception e)). So in your earlier test just catching "Exception e" and not "String message" gave two errors. One that you created from catching the "Exception e" and the second from the "String message" part of SAXException you didn't catch.

  • 0

Annorax: I don't think there's anything more I can do in this thread except, in the politest possible way, recommend that you get a second opinion on what monkey13 is writing. You can PM me if you'd like any help.

Roger and Out.

J

  • 0

Annorax. I can only suggest in the politest possible way that if you want to get a second opinion you don't pm JamesCherill. If you want a second opinion I'd ask someone who knows about JAVA. He hasn't actually offered any help, apart from, sub class a method from the JAVA package, to get round your error. Which IMHO is bad practice. If some code is so bad that you have to change the JAVA platform to get it to work I would scrap/change that bit of code.

@JamesCherill If you have any ideas or help why not post them here? They may help someone else latter. Any discussion we have had about the semantics of JAVA does not change the fact that you have offered zero good practice solutions to the problem. Hell you don't even think the problem can be solved in the bit of code posted (and you couldn't identify that the exception was being created and thrown there). No you're probably right. It'd be better to rewrite the JAVA package. :no:

Edited by monkey13
  • 0

Hi!

This output is printed by DefaultErrorHandler from inside of xerces staff.

To suppress this you should provide your own handler implementation.

Just implement XMLErrorHandler interface and provide implementation as a parameter to your parser:

	parser.setProperty(
		"http://apache.org/xml/properties/internal/error-handler", 
		 new MyXMLErrorHandler())

It should help.

  • 0

Old, I know, but I had to comment ....

  monkey13 said:
Technically yes it is. You just don't instantiate it in the same way as a non static class. You don't need to because JAVA handles it all behind the scenes.

Have you got reference for your above statement?

  Quote
With a non-static method you would have to do Bicycle myBike = new Bicycle(30, 0, 8); With a static method you can't instantiate an object of this class with a lifetime.

actually an object can have a static method as well as variables which can be called upon. A static method can instigate it's self, take the singleton pattern.

  Quote
ie. the object only exists for as long as the static method is running.

again, this is not what I understand, so I'm interested in some references.

  Quote
Think of it like this. When you use .parse(xml) what it actually does is creates an object (with the constructor) that you can not access performs the parse() function and then deletes the object.

Again, constructors are called to create objects and I dont think calling a static method will instigate or call the constructor of the class.

Even if a call to a static method instigates an object behind the scenes (waiting on reference), I really doubt the JVM will delete the object as that defeats the purpose of statics.... you would lose your static variable values if you had them....

you can call static methods and access static variables on an object.

  • 0

Hi _kane81. I'll try and clear up your questions but as I said earlier it's been a while since I used Java.

  _kane81 said:
Have you got reference for your above statement?

Not sure if I can find a direct reference (I'll keep looking) but if you think about it then that must be how it works. If you have a static class (or a class, that you have not instantiated, with a static method) it would be possible to code in Java without it being object orientated. If Java didn't do this behind the scenes.

  _kane81 said:
actually an object can have a static method as well as variables which can be called upon. A static method can instigate it's self, take the singleton pattern.

Whoops re-reading I meant class instead if method. What you're saying about methods within a non-static class is right.

  _kane81 said:
again, this is not what I understand, so I'm interested in some references.

See above. I know it's a bit of a cop out but if you have not instantiated a non-static class and use a static method within it Java must create an object of that class to use the static method.

  _kane81 said:
Again, constructors are called to create objects and I dont think calling a static method will instigate or call the constructor of the class.

Even if a call to a static method instigates an object behind the scenes (waiting on reference),

I've been thinking about this one more. If you call a static method of a non-static class you have not instantiated it must construct an object of it somehow. I may retract this statement because I don't know and can't find exactly how it does it. It may not call the constructor there might be a special set of rules for this.

Also I've just found this Static/Class methods. I know it's not a direct answer as to how Java does the creating, handling and destruction of static classes and methods but if you can come up with a way that this line (double avgAtt = MyUtils.mean(attendance)) is executed without the creation of some kind of object I'll be very interested.

  _kane81 said:
I really doubt the JVM will delete the object as that defeats the purpose of statics.... you would lose your static variable values if you had them....

Yes you're right for static classes. It would be daft to delete them. As for static variables I'm not sure how they are handled as they are constant for all objects of a class (or programme depending on how you declare them). With regard to a not instantiated class with a static method I suppose it must either create one object and reuse it if you call the same way again or create then delete. Otherwise there might be a load of objects building up.

  • 0

I think you're confusing "classes" for "objects", and statics belong to the class, not an instance of a class. The class loader initializes a class when a static member is invoked, assigned or accessed. Classes are unloaded when the execution of the program ends, with some exceptions.

http://java.sun.com/docs/books/jls/third_e.../execution.html

My guess is that Class members, a.k.a. statics, are put on the stack and not on the heap, but I have yet to see any documentation that says one way or the other. It could be totally VM dependent.

  Quote
If you have a static class (or a class, that you have not instantiated, with a static method) it would be possible to code in Java without it being object orientated. If Java didn't do this behind the scenes.

It is possible to code in Java without using objects.

  Quote
I've been thinking about this one more. If you call a static method of a non-static class you have not instantiated it must construct an object of it somehow. I may retract this statement because I don't know and can't find exactly how it does it. It may not call the constructor there might be a special set of rules for this.

It doesn't call any constructor. Class initialization is not equal to class construction.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Google Maps expands cycling routes and alerts for low-emission zones in Europe by Aditya Tiwari Google announced some new Google Maps updates for users in Europe. These features are meant to help them get around more sustainably and reduce emissions in cities, according to the search giant. For starters, Google is expanding its AI-powered feature that predicts when walking or taking transit will take roughly the same amount of time as driving. Currently available in 60 cities, the feature recommends walking or transit routes and has helped avoid tens of millions of car trips. It will soon launch in Copenhagen, Stockholm, Warsaw, and other cities. Speaking of car trips, Google displays helpful alerts when a car enters an area designated as a low-emission or low-traffic zone in supported cities, such as London and Berlin. It informs users whether their vehicle is permitted in these zones, and they should select an alternative route if necessary. The feature will expand to more than 1,000 low-emission and low-traffic zones across Europe in the coming months, including Italy, Sweden, Austria, and other countries. For those interested in cycling on the roads, Maps will display helpful details about routes in more places. Riders will see bike lanes on their cycling route, as well as other useful information, such as whether there is heavy car traffic or a steep incline ahead. The feature will expand to 17 new cities, including nine in Europe, which will cover about 125,000 km (77,671 miles) of bike lanes globally. Google has partnered with local governments and public authorities in cities such as Hamburg, Madrid, Barcelona, Milan, Rome, Zurich, Budapest, Vienna, and Brussels to source the data. Additionally, Google announced that fuel-efficient routing, also known as eco-friendly routing, is now available globally. As the name suggests, it finds the most fuel or energy-efficient route for your trip. The feature was introduced in 2021, starting with the US, and later expanded to over 40 countries across Europe in its first major update in 2022. On average, about 500 million trips using fuel-efficient routing are taken each month. Google estimated that the feature collectively resulted in "more than 2.7 million metric tons of GHG [greenhouse gas] emissions avoided in 2024 alone," which is equivalent to taking "more than 630,000 gasoline-powered cars off the road for a year."
    • That's one impressive "baby step". Well done!
    • They're already dead. They've been through many rounds off layoffs due to mismanagement and Destiny 2 is basically over as the interest levels are the lowest theyve ever been.
    • Redesigned Windows 11 Start menu: What users wanted and what Microsoft delivered by Taras Buria Windows 11 is getting a redesigned Start menu. This is a big deal for the soon-to-be-four-year-old operating system and its highly controversial design bits. After years of slow to no progress in the Start menu area, Microsoft is finally delivering a much better variant with many new features and plenty of feedback addressed. How much has been addressed? In 2023, we posted a list of the "Top 10 Start menu features and changes Windows 11 users want," so it is now time to compare that to the new Start menu. Note: The new Start menu is not yet publicly available. Microsoft is testing it in Windows 11 preview builds from the Dev and Beta Channels, and you can check out this article to learn how to enable it so that you can try it yourself. 1. Allow users to turn off the "Recommended" section - Delivered (17K+ upvotes) Round of applause for Microsoft, everyone, as the company actually delivered the most requested Start menu feature. The redesigned variant lets you turn off the Recommended section and hide it altogether so that it does not waste any space. Recommended section, begone!2. More customization options - Nope (5.4K+ upvotes) Although Microsoft now allows turning off the Recommended section and switching between three views for the All apps list, the menu remains quite restricted when it comes to personalization, so if you want true customization, Windhawk and the Start menu styler mod are here for you, allowing some seriously cool Start menu designs, as one on the screenshot below: 3. Allow resizing the Start menu - Sort of delivered (3.8K+ upvotes) You still cannot change the size of the Start menu manually like you could in Windows 10. However, the menu is now more adaptive, which means you will see more content if you have a bigger screen. Still, I would like to have the ability to make the menu bigger, so this one remains standing. 4. Go back to the Windows 10-style Start menu - Nope (3.4K+ upvotes) Microsoft is not going back to the Windows 10 Start menu, so if you are one of the 3,400+ people who upvoted this in Feedback Hub, your best course of action is to install a third-party Start menu or just stick to Windows 10. 5. Use Grid view for the All apps list - Delivered (1.5K+ upvotes) Another popular request was delivered fully. I would even say that Microsoft over delivered it. Instead of just killing the standard list view in favor of grid view, Microsoft let users decide what kind of view they want. You can stick to the classic list, switch over to grid view, or enjoy a categorized view. Again, well done! 6. Display jump lists when right-clicking pinned apps - Needs fixing (1K+ upvotes) Microsoft has already fixed this problem, and you can access jump lists and recent files by right-clicking pinned applications in the current Start menu version. However, turning off the Recommended section also turns off jump lists on the Start menu and taskbar for some reason. A very frustrating design for those who use jump lists and do not want the Recommended section. This needs fixing. Microsoft punishes you with no jump lists if you dare to turn off "Recommended." Why?7. Make the Start menu open on the All apps list by default - Delivered (1K+ upvotes) The new Start menu ticks this box as well. There is no need to click "All apps" when you open the Start menu. It now features a single-view user interface with the list of all apps right below your pins and recommendations. All you need to do is start scrolling. 8. Add a full-screen Start menu - Nope (1K+ upvotes) Even though Microsoft "has got this," nothing indicates that the company plans to reintroduce a full-screen Start menu from the days of Windows 10 and Windows 8. A shame, if you ask me. Interestingly, it appears that Microsoft considered a full-screen Start menu for Windows 11. The company recently showed some of the prototypes it considered implementing, including a scrollable full-screen menu. 9. Bring back live tiles - Nope (1K+ upvotes) Tiles are no longer alive. They are as dead as Windows Phone, and there is no return. 10. Make the Start menu button follow the system accent color - Nope (760+ upvotes) Microsoft "has got this," but the blue Start button is here to stay. Do you like what Microsoft did to Windows 11's Start menu? What features are still missing in your opinion? Share your thoughts in the comments.
  • Recent Achievements

    • Week One Done
      vivetool earned a badge
      Week One Done
    • Reacting Well
      pnajbar earned a badge
      Reacting Well
    • Week One Done
      TBithoney earned a badge
      Week One Done
    • First Post
      xuxlix earned a badge
      First Post
    • First Post
      Tomek Święcicki earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      674
    2. 2
      ATLien_0
      286
    3. 3
      Michael Scrip
      223
    4. 4
      +FloatingFatMan
      195
    5. 5
      Steven P.
      143
  • Tell a friend

    Love Neowin? Tell a friend!