Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!

Punit Singh

Ranch Hand
+ Follow
since Oct 16, 2008
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Punit Singh


I have written code to create a word document using Apache XWPF. I need to add a header and footer to this word doc.
Following is the code:


When I run the above piece of code, I am getting the following runtime exception:

Caught an: java.lang.NullPointerException
Message: null
Stacktrace follows:.....
at org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy.<init>(
at org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy.<init>(
at HeaderFooterExample.createDocument(
at HeaderFooterExample.main(


I am not sure what is wrong with the above code.
Any help on how to create a word document with header/footer using HWPF/XWPF will be appreciated.


7 years ago

Harikrishna Gorrepati wrote:If we synchronize on any method in a class, we will get lock on whole object know ?? Why should we synchronize 2 methods ?

Run above example and do some research on output as read() method is not synchronized, two threads can enter this method simultaneously.
Now modify read method with this method and run again.

you can see in first example you do not have lock on read() method, so no lock on your SharedObject, threads are entering in read method without taking any permission, while for entering update method then are taking permission from SharedObject. And SharedObject is giving permission when a thread is exiting from the update method. you can verify this from the println() in the output.
two things to remember here:
1. case argument takes only constant arguments.
2. switch argument cannot be downcast.

Ankit Garg wrote:
(Beaten by just 2 seconds )

Well you need more practice to win
Sorry, I did not understand the question at first time.
You cannot use new with interface.

rohan yadav wrote:So ankit i didnt undestand how to use command like javac -d ../classes com/
I using eclipse previously, so i havnt learned these concepts.

javac : means compile please

-d : means your destination for .class file is

../classes : one up than classes folder (destination)

com/ : and your source file is here in current directory, there is com folder inside that is source file.
I myself used to assume that I am very good in classpath, never felt any problem in classpath compilation and running, but if you see one yr old post, there is a very long debate on classpath concept, and it came out a great learning for me, as I was going in the wrong direction but I was discussing it in public. If I had done that in private, sure I would have missed some part of classpath.

Soumil Shah wrote:

Punit Singh wrote:Even Soumil Shah is also not clear, see his answer, he is creating an object on the buffer pool. While objects are always created on the heap.

Oops, I guess i had typed it wrong; instead of String literal pool, i written string buffer.

If you write String literal pool, then also it will be wrong to create an object in String literal pool.
And by the way, if you people go in wrong direction in classpath concept, we can catch your ears here. How will we know what you are discussing on your gmail. How will we pull your legs?

Ankur kothari wrote:hey but can we access the objref referecne outside the if loop? doesnt being able to acess the variable and scope be the same thing?

Ya accessing and scope is the same thing, we cannot access objref outside the if block, but GC is a completely different thing.
Here objref is just marked as not accessible outside the block, it scope status is changed and set to not accessible, but its reference with new Object() in the heap is still alive but not usable by us, so just a memory waste or leak.

or do you mean variables are stored only in the stacks with the methods they are in and stay there until the method gets over...or returns

you are correct here.

Ankur kothari wrote:this invisible objects topic confused me....the topic above it said that when the method returns it goes out of scope and object can be gc

in invisible objects topic it said that the object will be weakly held even after the block finishes...what is weak and string doing here?

can someone please clear my doubt

Actually the topic is saying about method + reference variable placement on the stack.

Actually methods are loaded in the stack, and whatever objects are created in that method those objects' references are also loaded in the same stack with method.

Suppose you have main() method

Then what will happen here.
1. when control enters to main() method. man() is loaded in the stack.
2. Then from new Object() statement, a new Object is created in the heap.
3. objRef reference variable is created on the same stack where main() is loaded.
4. Reference of that new Object() on the heap is stored in objRef reference variable.

Now when you say this:

5. objRef=null; explicitly breaking the link between objRef on the stack and new Object() on the heap.
GC can see that new Object() on the heap is broken. So it can gc it after line 1.

But when this is done

here after line1 is executed just scope of objRef local reference variable is over, but we are not breaking the link between objRef and new Object() explicitly. And objRef in not destroyed until main() method returns and stack of main() is destroyed. So till main() is living on the stack, objRef is also living on the stack, but after line1 we cannot use objRef as it is not visible after that, it is called invisible after that.

But it is still residing on the stack and is invisible to us and keeping a reference of new Object() on the heap. Garbage collector can't collect new Object() from the heap as its reference is on the stack.

Reason is the purpose why Interface has been designed.
Interface can just say what to do not how to do.
So it can only declare any method (what to do), cannot give body(how to do) to them.
There is no declaration for static methods, as static methods are class methods, not instance methods.