• 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

    • Soon: honda puts a turbo'd k24 in a rocket with a big spoiler and some extra body trim.
    • Half a trillion? Do your homework, they're worth more than 3 freaking trillion dollars. As for the correlation between market cap and features, I think you're mistaken. There are some things called priorities. It obviously wasn't a priority for them to implement this, as it's still not a priority to implement moving taskbar to different locations of the screen. The trillions of dollars they're worth, have nothing to do with this.
    • Ara: History Untold 1.4 update delivers overhauls to AI, map generation, combat, and more by Pulasthi Ariyasinghe The 2024-released 4X strategy experience Ara: History Untold has been steadily receiving a massive amount of features and tweaks during its post-launch support period. Now, Stardock Entertainment and Oxide Games have another fresh update for PC gamers to jump into, and it's targeting some major aspects like the game's combat, AI, the camera perspective, and more. Dubbed the Living Strategy Update, the development team says that its features are a direct result of community feedback. First off, the update brings along the Living Strategy View as the default perspective for gameplay, letting players both get immersed in the environments and also easily manage their cities and armies. Next, the AI of computer opponents has been improved so that they make more meaningful decisions, with better late-game choices being a focus for the developer. Ara: History Untold's map generation has also been overhauled, with much more strategically distinct regions spawning across the playable areas for better strategic thinking. The amount of micromanagement has also been reduced by default, but this can be disabled to gain back full control if required. Players will also find that frame rates will remain high even in late-game scenarios thanks to graphics engine optimizations. Faster turn times, better UI responsiveness, and plenty of balance changes are also a part of the update. "It has been a real pleasure working with our colleagues at Microsoft for this update. The teams at Stardock, Oxide, and Microsoft have so much energy going into these updates. Version 1.4 is just a taste of what we have in store," said Brad Wardell, Chief Executive Officer for Stardock and Oxide. "In an industry where true 1.0 products are rare, especially ones in established genres, this has been a wonderful opportunity to try out new ideas and concepts that we in the strategy community have been thinking about for decades." Ara: History Untold update 1.4, Living Strategy, is now available to PC players across Steam and Xbox stores, including PC Game Pass subscribers. Alongside the update, the Untold Scenarios DLC pack has also arrived. This premium add-on touts fresh historical scenarios like Bronze Age Collapse, the Clash of Hemispheres, and Europe at the Crossroads for players to dive into. A separate selection of "what-if" scenarios are also included here, letting players explore alternate histories and see what they can accomplish. The Ara: History Untold: Untold Scenarios DLC pack is also available now on PC across Steam and Xbox stores with a $9.99 price tag. Disclaimer: Neowin's relationship to Stardock
    • Copilot in Excel just got a major upgrade by Usama Jawad It's no secret that Microsoft is trying to get as many people to use Copilot as possible. This is being done through various means, including integration with OneNote, Defender, Windows, and more. Microsoft Excel is the most popular spreadsheet software out there, so it makes sense that the Redmond tech firm would integrate and build upon the capabilities of Copilot in this tool too. Now, the company has implemented a major upgrade in this integration. In a blog post, Microsoft has made Copilot smarter in terms of context awareness in Excel. This makes the AI assistant more useful when asking questions about your data in nautral language queries. Previously, Copilot would only make inferences based on the cell that you have selected, and it typically worked quite well in this scenario. However, Microsoft has realized that workbooks and sheets have become more complex, which means that Copilot needs to be smarter too. This means that you no longer need to select relevant data when asking queries about it, you can simply ask questions such as "Show me insights about the data I was just analyzing" and "Sort the table in the top-right", and Copilot should just work. Microsoft says that this major upgrade in Copilot is due to combining cell signals with chat history in order to make inferences. It also supports data ranges, which means that it can serve as the foundation for even broader context in future updates. In order to get customers to trust the inferences that Copilot is making, Microsoft will highlight the data that the AI assistant is using. This enables customers to further refine the dataset that Copilot is using so that they can get the most accurate responses. Smart context awareness is available on the web and the following versions of desktop: Windows: Version 2505 (Build 18623.20058) Mac: Version 16.95 (Build 2506.3090) Meanwhile, the visual highlighting feature is also available on the web and the desktop versions mentioned below: Windows: Version 2505 (Build 18705.20000) Mac: Version 16.96 (Build 2506.4070) Microsoft has requested customers to provide feedback through the thumbs up/down icons present at the bottom of the Copilot response cards.
    • We should probably just focus on how nice it is that finally another company is taking reusable rockets seriously. I can't believe there are so few players and many rockets in service today are still completely expendable. Hopefully Honda can scale up theirs and add some badly needed competition. I want to see planes become irrelevant for global travel and we use rockets instead.
  • Recent Achievements

    • Week One Done
      slackerzz earned a badge
      Week One Done
    • 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
  • Popular Contributors

    1. 1
      +primortal
      687
    2. 2
      ATLien_0
      284
    3. 3
      Michael Scrip
      225
    4. 4
      +FloatingFatMan
      197
    5. 5
      Steven P.
      135
  • Tell a friend

    Love Neowin? Tell a friend!