• 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
  azcodemonkey said:
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.

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

First of you are confusing class objects with instances of class objects. Two different things.

Sorry you're just wrong. If you read some more bits of the link you posted. i.e http://java.sun.com/docs/books/jls/third_e...ions.html#41147

  Quote
15.9.3 Choosing the Constructor and its Arguments

Let C be the class type being instantiated. To create an instance of C, i, a constructor of C is chosen at compile-time by the following rules:

* First, the actual arguments to the constructor invocation are determined.

o If C is an anonymous class, and the direct superclass of C, S, is an inner class, then:

+ If the S is a local class and S occurs in a static context, then the arguments in the argument list, if any, are the arguments to the constructor, in the order they appear in the expression.

+ Otherwise, the immediately enclosing instance of i with respect to S is the first argument to the constructor, followed by the arguments in the argument list of the class instance creation expression, if any, in the order they appear in the expression.

Just because you don't write a constructor does not mean that object does not have one and just because you do not call a constructor does not mean one is not called.

Also you seem to be saying that a class object (or any other kind of object) may be initialised and used without being instantiated. How would this work?

  azcodemonkey said:
It is possible to code in Java without using objects.

Please provide a code example of this.

  • 0
  monkey13 said:
... you seem to be saying that a class object (or any other kind of object) may be initialised and used without being instantiated. ... Please provide a code example of this.

public class NoObjects {

	static { 
		// static initialiser - called when class is initialised
		System.out.println("Thes has been initialised");
	}

	public NoObjects() {
		// default constructor - called when an instance is created
		System.out.println("Aer of the class has been instantiated");
	}

	public static void main(String[] args) {
		// static method - called when app is run
		System.out.println("Hellod");
	}

}

  • 0

As I don't have Java here at work I can't check this but I assume you are making this point because the output is:

"The class has been initialised"

"Hello World"

and you're hoping this proves you're argument. However if you see this from the above link.

  Quote
15.9.5.1 Anonymous Constructors

An anonymous class cannot have an explicitly declared constructor. Instead, the compiler must automatically provide an anonymous constructor for the anonymous class. The form of the anonymous constructor of an anonymous class C with direct superclass S is as follows:

The class NoObjects is instantiated anonymously by the compiler because you did not instantiate it. Therefore it can not call the constructor you specified because it is not allowed to. This is not proof that it is not instantiated.

The whole program in this example is an anonymous instantiation of the class NoObjects

  • 0

It's anonymous because you did not instantiate it. It is not a class with no name. It can have a declared constructor on line 8 but this just isn't used in this anonymous instantiation of it.

It's anonymous to you. The JVM will still have all the stuff to do with an instance for that class it just doesn't let you at it.

  • 0

Yes it's not an anonymous class but it is an anonymous incident of that class. That is why I'm using the anonymous class stuff to show what happens with the case we are talking about.

Ok we'll look at it another way. If the class NoObjects is only initialised and not instantiated how does it get any memory? http://java.sun.com/docs/books/jls/third_e.../execution.html

  Quote
12.4.1 When Initialization Occurs

Initialization of a class consists of executing its static initializers and the initializers for static fields declared in the class. Initialization of an interface consists of executing the initializers for fields declared in the interface.

.....

12.5 Creation of New Class Instances

...

Whenever a new class instance is created, memory space is allocated for it with room for all the instance variables declared in the class type and all the instance variables declared in each superclass of the class type, including all the instance variables that may be hidden

....

You can not have any Java code without an object. The very class the code is in is an object its self at runtime.

Edited by monkey13
  • 0
  monkey13 said:
First of you are confusing class objects with instances of class objects. Two different things.

Sorry you're just wrong. If you read some more bits of the link you posted. i.e http://java.sun.com/docs/books/jls/third_e...ions.html#41147

Just because you don't write a constructor does not mean that object does not have one and just because you do not call a constructor does not mean one is not called.

Also you seem to be saying that a class object (or any other kind of object) may be initialised and used without being instantiated. How would this work?

Please provide a code example of this.

We're talking about statics, aren't we? We're talking about how statics are initialized, not objects. Objects are instantiations of classes. Statics are initialized when they are first accessed as the class is then loaded and its statics initialized. Again, class initialization is not equal to class instance construction.

As far as actual instances of a class are concerned, your reference is correct. In the context of discussing how statics work, it is not even relevant, nor is the anonymous class/constructors. Statics have nothing what so ever to do with class construction. The VM allocates memory for the class it is loading, not sure if it's stack(I'd assume so) or heap, and the data/functions remain in memory until program end, or in some special circumstances when the class is unloaded during normal execution. Think of statics as functions or static data in a C-library. You'd link to or load the lib and reference the functions without instantiating anything. The functions or static data just exists in memory until your unload the lib. This is analogous to how statics work in managed languages. C# uses the same semantics as Java does when it comes to statics.

http://msdn.microsoft.com/en-us/library/79b3xss3.aspx

note: Java doesn't have static classes, so there are nothing but non-static classes in that context

  Quote
A non-static class can contain static methods, fields, properties, or events. The static member is callable on a class even when no instance of the class has been created. The static member is always accessed by the class name, not the instance name. Only one copy of a static member exists, regardless of how many instances of the class are created. Static methods and properties cannot access non-static fields and events in their containing type, and they cannot access an instance variable of any object unless it is explicitly passed in a method parameter.

http://msdn.microsoft.com/en-us/library/98f28cdx(VS.71).aspx

http://java.sun.com/docs/books/jls/third_e...es.html#8.3.1.1

  Quote
If a field is declared static, there exists exactly one incarnation of the field, no matter how many instances (possibly zero) of the class may eventually be created. A static field, sometimes called a class variable, is incarnated when the class is initialized (?12.4).

http://java.sun.com/docs/books/jls/third_e...es.html#8.4.3.2

  Quote
A method that is declared static is called a class method.Aclass> method is always invoked without reference to a particular object.> An attempt to reference the current object using the keyword this or the keyword super or to reference the type parameters of any surrounding declaration in the body of a class method results in a compile-time error. It is a compile-time error for a static method to be declared abstract.

A method that is not declared static is called aninstance> method>, and sometimes called a non-static method.An instance method is always invoked with respect to anobject>>, which becomes the current object to which the keywords this and super refer during execution of the method body.

I'm also well aware of default constructors being provided by the compiler should one not be provided explicitly, whether an anonymous class or not. Again, not relevant to statics.

I could write an example of not leveraging objects in Java, but I think you'd argue that I'm using objects because all my statics reside in a class. I can write procedurally in Java using 1 class for entry point and nothing but static methods that are completely stateless and do not leverage encapsulation, inheritance, or polymorphism.

  • 0
  monkey13 said:
Ok we'll look at it another way. If the class NoObjects is only initialised and not instantiated how does it get any memory?

Memory for the code of all the methods is allocated by the class loader when the class is loaded. Memory for the static variables is allocated when the class is initialised. It's only the memory for the instance variables that is allocated when an instance of the class is created. Since static methods are not allowed to reference instance variables they can (and do) execute without any instance of the class being created.

  • 0

Ok azcodemonkey good links and references. I'm afraid I'm going to ignore the C# MS stuff. I don't know anyhting about how C# works so I can't comment on that, but I do doubt it's exactly the same as Java.

Reading from that you seem to be saying that there are operations that can occur outside of an object. This is not so. Yes a static field can exist outside of an object an initialisation time, but how do you do anything with this static field without instantiating an object?

Yes a class method is invoked without a specific object method but that does not mean that an object does not exist and one is not created by the JVM to perform the operation.

You're thinking that because it says don't specify an object one doesn't exist. What I'm saying is you don't need to because Java does it for you and you'd just be confusing it by specifying one.

  Quote
I could write an example of not leveraging objects in Java, but I think you'd argue that I'm using objects because all my statics reside in a class. I can write procedurally in Java using 1 class for entry point and nothing but static methods that are completely stateless and do not leverage encapsulation, inheritance, or polymorphism.

Quite right. The whole thing would be one big class, therefore one big object at run time. Also because a class is of type object you would be doing both polymorphism and inheritance. Although you would fail on the encapsulation. ;)

  • 0
  JamesCherrill said:
Memory for the code of all the methods is allocated by the class loader when the class is loaded. Memory for the static variables is allocated when the class is initialised. It's only the memory for the instance variables that is allocated when an instance of the class is created. Since static methods are not allowed to reference instance variables they can (and do) execute without any instance of the class being created.

Yes but when is the memory to perform these operations allocated if no objects exist. Certain things can exist independently of objects but nothing can be done with them without an object. Again just because you don't specificaly create or need to create an object does not mean one is not created.

  • 0
  monkey13 said:
Yes but when is the memory to perform these operations allocated if no objects exist.

Everything needed to execute the static method is already in memory (as explained previously) once the class has been loaded and initialised (apart from variables local to the static method, which are created on the stack as required).

  • 0
  monkey13 said:
Certain things can exist independently of objects but nothing can be done with them without an object. Again just because you don't specificaly create or need to create an object does not mean one is not created.

OK, I think I may have an idea as to where your confusion arises. Yes, you are right, classes are Objects (instances of class Class) (note capitalisation!). However, unlike instances of any other class,

  Quote
Class objects are constructed automatically by the Java Virtual Machine as classes are loaded

(http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html).

So there is an Object, it's an instance of class Class, it has been instantiated, and all that happens before it can execute any of its static methods. This must not be confused with creating instances of the class - this can only be done (not by the class loader!) by executing a "new" statement which, in turn, invokes any default or explicit constructors.

  • 0
  JamesCherrill said:
OK, I think I may have an idea as to where your confusion arises. Yes, you are right, classes are Objects (instances of class Class) (note capitalisation!). However, unlike instances of any other class,

(http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html).

So there is an Object, it's an instance of class Class, it has been instantiated, and all that happens before it can execute any of its static methods. This must not be confused with creating instances of the class - this can only be done (not by the class loader!) by executing a "new" statement which, in turn, invokes any default or explicit constructors.

Well done you have said what I've been saying all along. It is not my confusion but yours as you didn't understand what was happening and how Java works.

I've been saying that classes are instantiated without a user doing it all the way through this thread only now you announce it like you've made some major breakthrough. Although you can still have instances of a class without using new. If you think about it there is a very obvious one.

Also to go from "Objects are not instantiated at all in my code I provided" to "Yes they are but now I'm going to argue about the semantics of this o(O)bject" looks a bit desperate.

  • 0

I see what you did there. You went from arguing that static methods are constructors(the statement that started this whole discussion for me), which they are not, to how the JVM creates objects behind the scenes to allow invocation. Invoking a static does indeed cause the JVM to do something, constructing classes to create a layout of the static's class, but the static invocation in and of itself does not create an instance of its containing type, which is the very definition of a constructor.

You were mixing language and causing confusion which is what I have a problem with. A student that puts on his paper that a static method is a constructor is going to get that statement marked as incorrect, and that is fact. And you also are equating object creation behind the scenes to object oriented development. They are not the same. Using statics is not using object oriented practices. Objects model behavior, statics do not. So I am totally correct that I can write a non-object oriented application in Java by using statics. I would not using objects despite what the framework may do to make it work. A class without non-static members is not a class that is going to be usable as an object. It is solely a container for functions. Sure, I'll be able to call toString on it, and treat it as an Object, but if it has no behavior, it isn't an object oriented application.

class A{

	public static void Foo(){ // ... do something }

}

class Main{

	public static void main(String[] args){
		A.Foo(); // this does not create an instance of A, which is what a constructor does.
		A _a = new A(); // this is an instance created with the default constructor provided at compile time
	}
}

  • 0

I didn't say that static methods are constructors. I said that object could be constructed by calling static methods. Once again (for about the 20th time) if I call a static method of a class that has not been specificity instantiated how does Java do this? Java cannot do anything without creating instances of objects. Just because you don't specify what is being created doesn't mean nothing is being created.

  azcodemonkey said:
And you also are equating object creation behind the scenes to object oriented development. They are not the same. Using statics is not using object oriented practices. Objects model behavior, statics do not. So I am totally correct that I can write a non-object oriented application in Java by using statics. I would not using objects despite what the framework may do to make it work. A class without non-static members is not a class that is going to be usable as an object. It is solely a container for functions. Sure, I'll be able to call toString on it, and treat it as an Object, but if it has no behavior, it isn't an object oriented application.

class A{

	public static void Foo(){ // ... do something }

}

class Main{

	public static void main(String[] args){
		A.Foo(); // this does not create an instance of A, which is what a constructor does.
		A _a = new A(); // this is an instance created with the default constructor provided at compile time
	}
}

Sorry you would not be using objects so therefore this is not object orientated? How you write your code changes the underling principals of how Java operates and invalidates it as an object orientated programming language?

Point 1. There is at least one object involved in this code before you even add any lines after class Main{ (see JamesCherrill)

Point 2 Again Just because you don't specify instantiation does not mean it doesn't happen. If A.Foo(); doesn't instantiate class A (or A is not already instantiated) how is this operation performed?

Point 3 (backs up point 2) There is one very major and obvious class, if you think about it, that is regularly instantiated without the use of new. What is it? (I've asked this question before and no-one has yet to answer it)

  • 0
  monkey13 said:
Point 1. There is at least one object involved in this code before you even add any lines after class Main{ (see JamesCherrill)

Please don't try to quote me in support of your bizarre ideas about object creation. The post you referred to was an attempt to understand why you were going wrong and maybe offer you a lifeline to dig yourself out of the increasingly deep hole into which you are digging yourself. No instance of the class is needed or created when a static method in that class is called.

Your fundamental error is in believing that

  Quote
Java cannot do anything without creating instances of objects.

This is simply not true.

  • 0
  JamesCherrill said:
No instance of the class is needed or created when a static method in that class is called.

Your fundamental error is in believing that

  Quote
Java cannot do anything without creating instances of objects.

This is simply not true.

Oh dear I'd assumed you had worked it out. Ok once again then. The question no-one seems to be able to answer. If Java does not create any objects how does it perform operations?

Also. Why does how you are writing the code completely invalidate it as an object orientated language?

  • 0
  monkey13 said:
<insult snipped>. Ok once again then. The question no-one seems to be able to answer. If Java does not create any objects how does it perform operations?

Also. Why does how you are writing the code completely invalidate it as an object orientated language?

1. Why do you think it needs an object to perform operations? It doesn't. That's why there is no answer to the question as asked.

2. Yes, you can write a Java progam with no objects instantiated. and that is not OO practice. The fact that it's possible in Java doesn't invalidate anything.

3. Just as a matter of curiosity - do you work as a software developer and, if so, in what language(s)?

  • 0

1. Yes it does because it is a pure object orientated language. Unlike C++ which is a language with object orientated features. I wonder if this is where the confusion arises in C++ it is possible to write non-OO stuff.

2. See above. The main class you use is an object at runtime.

3. Not exactly. I work in research at the moment which at the moment is mainly Matlab and a bit of C++. However I am the only one with an IT background at my work so that means I end up fielding all kinds of questions.

Edit: Perhaps this will clear it up.

http://java.sun.com/docs/books/tutorial/ja...epts/class.html

Bit basic I know but it makes this rather succinct point.

  Quote
A class is the blueprint from which individual objects are created.

If you have the blueprints to a house can you live in it? No, because you need to build the house before you can do anything with it.

Edited by monkey13
  • 0

I find this difficult to express but you are obviously an intelligent and articulate person. Your misunderstandings about Java are natural, given that you don't actually work with the language. There are a number of people who post regularly in these forums who have huge experience in Java, all of whom are willing to help you if you are willing to listen. I personally have only been a software developer for 30 years, and using Java exclusively since 1996, but I still learn a lot from those contributors who know more than I do. I guess you're too far down the road of rejection to listen to anything I have to say, but maybe you can open your mind to people like azcodemonkey and alexanderkl and kane81. They really do know what they're talking about. Good luck, and goodbye.

James

  • 0

You're right I don't work with the language now. As I said it's been a few years since I worked Java.

For all your years of expertise you couldn't correctly identify or solve the problem with Annorax's (the original poster) code. Whist I was offering solutions you were harping on about needing the "ACTUAL CODE" as you put it and then the solution you offered, sub classing the Java platform to avoid the error rather than dealing with it. Is, IMHO, very bad practice. If that is being

  JamesCherrill said:
too far down the road of rejection to listen to anything I have to say

I'm very happy here. ;)

Take care.

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

    • No registered users viewing this page.
  • Posts

    • Go ###### yourself Apple. What an embarassing piece of ###### company.
    • From code to combat: Meta CTO calls for Silicon Valley involvement in military contracts by Hamid Ganji This week, Meta announced a partnership with the defense technology startup Anduril to build next-gen VR/AR extended reality headsets for the US military. Microsoft was given the contract in 2018, and the headsets were supposed to be built based on Microsoft HoloLens. Earlier this year, Microsoft gave up the entire project to Anduril but kept its role as the cloud services provider. Meta's Chief Technology Officer (CTO) is now calling for more Silicon Valley involvement in military contracts. Speaking at the Bloomberg Tech summit in San Francisco (via: Business Insider), Andrew Bosworth said the recent partnership between Meta, Anduril, and the US military could be a "return to grace" for Silicon Valley. "The Valley was founded on a three-way investment between the military, academics, and private industry. That was the founding of it," Bosworth said. Meta's CTO added that building VR/AR headsets for the US military doesn't turn the company into a defense contractor. He also said it was "way too early" to determine whether military contracts would become a business segment for Meta. "So far, it's like a zero. Let's start with one and go from there. I think there's no reason it couldn't be meaningful in the impact that it has," he added. The US military's desire for AI-powered tools and weapons has turned Big Tech into military contractors, whether willingly or unwillingly. Companies like Microsoft, Google, and Meta have a long history of providing services to the military and law enforcement agencies. The relationship between tech firms and the defense segment has always been controversial. In the most recent case, a group of Microsoft employees protested against the company's partnership with the Israeli military, which led to the layoff of the protesting staff.
    • Or run msinfo32! With Windows 11, Microsoft believes it has reinvented the wheel?
    • The long-awaited Nothing Phone (3) is finally coming next month, launch date confirmed by Aditya Tiwari London-based consumer electronics brand Nothing is due to launch its latest flagship in 2025. The company dropped a new teaser for the Phone (3), revealing when the flagship device will be out on the market. Nothing Phone (3) will be unveiled during a live event on July 1 at 1:00 PM ET / 10:00 AM PT / 6:00 PM BST / 11:30 PM IST. It has already created a live event titled "Come to Play" on its official YouTube channel, for which you can add a reminder by clicking on the "Notify Me" button. Nothing CEO Carl Pei has previously dropped several details about the unreleased smartphone. He took part in a social media AMA earlier this year and said Nothing Phone (3) will arrive in the third quarter of 2025. Pei confirmed that Nothing Phone (3) will make its way to the US this time after a dry spell since 2023. However, he didn't specify whether the device will be sold directly or through the beta channel, which currently includes Phone (3a), Phone (3a) Pro, and CMF Phone 2 Pro. The US has been a rocky terrain for Nothing. Its first smartphone was made available through the beta channel, and the Nothing Phone (2) is the only smartphone from the company that has been widely available in the US. The Glyph interface featured on the back of Nothing smartphones has remained a differentiating factor from the start. However, the smartphone maker recently posted a 9-second video in which the Glyph lights on the back of a Nothing smartphone abruptly turn off. "We killed the Glyph Interface," the company said. It makes sense when you check out the Phone (3) teaser and see dot matrix-style lights being flashed in a pattern. Nothing released another teaser about a week ago, featuring the number 3 lit up as dot matrix LEDs. If you're looking for some trivia, Nothing product manager Raymond Zhu estimated in a Q&A video that the company would need to sell about 250,000 Phone (3) units to turn a profit. Answering another question, he added that their biggest weakness is "no one knows us", and the company is struggling to reach the masses without high marketing budgets. Let's wait to see what Nothing has in store for Phone (3) next month other than the new physical button. Speaking of the future of smartphones, the Nothing CEO believes that our entire software experience will eventually be condensed down to just one app.
    • As far as I can remember, no one has done a 4v4 before. 2v2? yes. 3v3? yes.
  • Recent Achievements

    • Week One Done
      jbatch earned a badge
      Week One Done
    • First Post
      Yianis earned a badge
      First Post
    • Rookie
      GTRoberts went up a rank
      Rookie
    • First Post
      James courage Tabla earned a badge
      First Post
    • Reacting Well
      James courage Tabla earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      403
    2. 2
      +FloatingFatMan
      179
    3. 3
      snowy owl
      174
    4. 4
      ATLien_0
      170
    5. 5
      Xenon
      135
  • Tell a friend

    Love Neowin? Tell a friend!