• 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

    • Microsoft 365 Word gets SharePoint eSignature, now you can ditch third-party signing tools by Paul Hill Microsoft has just announced that it will be rolling out an extremely convenient feature for Microsoft 365 customers who use Word throughout this year. The Redmond giant said that you’ll now be able to use SharePoint’s native eSignature service directly in Microsoft Word. The new feature allows customers to request electronic signatures without converting the documents to a PDF or leaving the Word interface, significantly speeding up workflows. Microsoft’s integration of eSignatures also allows you to create eSignature templates which will speed up document approvals, eliminate physical signing steps, and help with compliance and security in the Microsoft 365 environment. This change has the potential to significantly improve the quality-of-life for those in work finding themselves adding lots of signatures to documents as they will no longer have to export PDFs from Word and apply the signature outside of Word. It’s also key to point out that this feature is integrated natively and is not an extension. The move is quite clever from Microsoft, if businesses were using third-party tools to sign their documents, they would no longer need to use these as it’s easier to do it in Word. Not only does it reduce reliance on other tools, it also makes Microsoft’s products more competitive against other office suites such as Google Workspace. Streamlined, secure, and compliant The new eSignature feature is tightly integrated into Word. It lets you insert signature fields seamlessly into documents and request other people’s signatures, all while remaining in Word. The eSignature feature can be accessed in Word by going to the Insert ribbon. When you send a signature request to someone from Word, the recipient will get an automatically generated PDF copy of the Word document to sign. The signed PDF will then be kept in the same SharePoint location as the original Word file. To ensure end-to-end security and compliance, the document never leaves the Microsoft 365 trust boundary. For anyone with a repetitive signing process, this integration allows you to turn Word documents into eSignature templates so they can be reused. Another feature that Microsoft has built in is audit trail and notifications. Both the senders and signers will get email notifications throughout the entire signing process. Additionally, you can view the activity history (audit trail) in the signed PDF to check who signed it and when. Finally, Microsoft said that administrators will be able to control how the feature is used in Word throughout the organization. They can decide to enable it for specific users via an Office group policy or limit it to particular SharePoint sites. The company said that SharePoint eSignature also lets admins log activities in the Purview Audit log. A key security measure included by Microsoft, which was mentioned above, was the Microsoft 365 trust boundary. By keeping documents in this boundary, Microsoft ensures that all organizations can use this feature without worry. The inclusion of automatic PDF creation is all a huge benefit to users as it will cut out the step of manual PDF creation. While creating a PDF isn’t complicated, it can be time consuming. The eSignature feature looks like a win-win-win for organizations that rely on digital signatures. Not only does it speed things along and remain secure, but it’s also packed with features like tracking, making it really useful and comprehensive. When and how your organization gets it SharePoint eSignature has started rolling out to Word on the M365 Beta and Current Channels in the United States, Canada, the United Kingdom, Europe, and Australia-Pacific. This phase of the rollout is expected to be completed by early July. People in the rest of the world will also be gaining this time-saving feature but it will not reach everyone right away, though Microsoft promises to reach everybody by the end of the year. To use the feature, it will need to be enabled by administrators. If you’re an admin who needs to enable this, just go to the M365 Admin Center and enable SharePoint eSignature, ensuring the Word checkbox is selected. Once the service is enabled, apply the “Allow the use of SharePoint eSignature for Microsoft Word” policy. The policy can be enabled via Intune, Group Policy manager, or the Cloud Policy service for Microsoft 365 Assuming the admins have given permission to use the feature, users will be able to access SharePoint eSignatures on Word Desktop using the Microsoft 365 Current Channel or Beta Channel. The main caveats include that the rollout is phased, so you might not get it right away, and it requires IT admins to enable the feature - in which case, it may never get enabled at all. Overall, this feature stands to benefit users who sign documents a lot as it can save huge amounts of time cumulatively. It’s also good for Microsoft who increase organizations’ dependence on Word.
    • It's always good to have an option to secure your stuff to another medium. I did that with DVD/CD collection, and run my own media server now. It's more convenient that way and no need for separate players anymore.
    • Google Search AI Mode gets support for data visualization and custom charts by Aditya Tiwari Google announced it is rolling out support for data visualizations and graphs for finance-related queries in Google Search's AI Mode. Introduced last month at the Google I/O 2025 keynote, the feature lets you analyze complex datasets and create custom charts simply using natural language prompts. The updated AI Mode lets you compare and analyze information over a specific period, Google explained. It generates interactive graphs and provides a comprehensive explanation for your questions. AI Mode utilizes Gemini's multimodal capabilities and multi-step reasoning approach to comprehend the question's intent while accessing historical and real-time information relevant to the question. For instance, instead of manually researching individual companies and their stock prices, you can use AI Mode to compare the stock performance of different companies for a specific year. Once the graph is generated, you can choose the desired time period using the mouse cursor and ask follow-up questions based on the data presented. These new data visualizations for finance queries are available to users who have enabled the AI Mode experiment in Labs. AI Mode was introduced earlier this year as an experimental feature in the US. The feature is an upgraded version of AI Overviews, and Google closely worked with AI power users through the initial development process. It uses the “query fan-out” technique to perform multiple related searches across subtopics and different data sources, then combines them to come up with a comprehensive response. Google updated AI Mode last month to use a custom version of the latest Gemini 2.5 model. It added several new features, including Deep Search, live capabilities, agentic capabilities of Project Mariner, a new shopping experience, and the ability to add personal context by linking Google apps. The search giant is planning to turn AI Mode into its bread and butter. It has begun testing ads for the feature, which will appear below and be integrated into AI Mode responses where relevant.
    • Guys, you should find another way to promote your deals... It's the third article in the last months that promote this deal for an upgrade from 10. Considering that upgrade from 10 to 11 is free it's a total non-sense.
    • Store should be a shrine of useful applications, vetted and verified. Easily sorted by publisher. Windows should start with not much installed and have things as options in the store. Not the wild west mess that it is. You could delete 95%+ of the crap on there and no one would notice. They need to add a better UI to the updates, it's awful right now.
  • Recent Achievements

    • Week One Done
      luxoxfurniture earned a badge
      Week One Done
    • First Post
      Uranus_enjoyer earned a badge
      First Post
    • Week One Done
      Uranus_enjoyer earned a badge
      Week One Done
    • Week One Done
      jfam earned a badge
      Week One Done
    • First Post
      survivor303 earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      432
    2. 2
      +FloatingFatMan
      239
    3. 3
      snowy owl
      213
    4. 4
      ATLien_0
      211
    5. 5
      Xenon
      157
  • Tell a friend

    Love Neowin? Tell a friend!