Add to Google Reader or Homepage

Javax.naming.NameAlreadyBoundException

                                If you glance at the title you could have an idea about this exception. It says that Name is Already bounded. 

What does it mean? 


When we are dealing with RMI, we know that the server needs to register the remote object in the RMI registry with a name. The client will use this name to get the remote object.I  have already explained this in my post Java RMI tutorial.Here we have to know one thing that each remote object should be registered with a unique name.

So it is obvious that when two different remote objects are registered with same name then this exception will be thrown for example, if we do like this, then this exception will be thrown.

.....................
.............................
.............................
 Context namingContext = new InitialContext();
namingContext.bind("rmi:central",store);//binding the remote object store with name central
namingContext.bind("rmi:central",shop);
//binding the remote object shop with name central

What are the other reasons?

 In the above shown exmaple, i have registered two different remote objects with same name and so i got this exception thrown, "but the other common reason is trying to  re-bind the object with the same name". 

 When this kind of situation occurs?


I have already told in my post  Executing java RMI that to execute an RMI we need to start the rmi registry first and then execute the server and then client. If you set the codebase property correctly it will work fine, but when you try to re-execute the  server you may have encountered this exception even though codebase is set correctly. why? because "you have actually tried to re-bind the object that is already bounded with the same name".

Here you should understand that "as long as RMI registry is running the entries of the registry persists until the RMI registry is stopped". so when re-executing the server ensure that  the  RMI registry is re-started again.




java.util.ConcurrentModificationException

                           

                In this post we will see about the Concurrent Modification Exception which is usually thrown when a collection is structurally modified by more than one  iterators .Let us see in detail about this exception.

In java API it is given that,"If a method detects that an object is concurrently modified and when such a modification is not permissible, then this exception will be thrown".

 What does it mean?

 

This could be clearly explained in the context of collections, we all know that the collections are used to store the objects and there are iterators available,which contains methods that  enables us to traverse through the collections and  to add and remove elements at the desired locations.

This feature brings us a problem that "when two iterators are simultaneously used to modify the collection then this exception will be thrown" and there are two other situations at which this exception will be thrown, let's see what are they?

-->

Situation 1

 "Do not instantiate an iterator until the collection is filled with the objects", Let's see what happens if we instanitate an iterator before the collection is filled.


Program

import java.util.*;
import java.lang.*;
class Concurrenttest
{
public static void main(String args[])
{
LinkedList a=new LinkedList();
ListIterator li=a.listIterator();//line no 8
a.add("wow");
a.add("world");
a.add("ganesh");//line no 11
System.out.println(a);
System.out.println(li.next());// line no 13
}
}
 


Output

[wow, world, ganesh]
Exception in thread "main" java.util.ConcurrentModificationException
        at java.util.LinkedList$ListItr.checkForComodification(Unknown Source)
        at java.util.LinkedList$ListItr.next(Unknown Source)
        at Concurrenttest.main(Concurrenttest.java:13)

 Here in the above program you can see that i have just created a collection and stored in it  some string objects and i tried to traverse through the list but it throws Concurrent Modification Exception. But if you instantiate the iterator (line no 8)after the collection is filled(line no 11) then this exception would not be thrown.

Situation 2

 

 ''Do not  use the iterator and the collection object itself to modify the collection  simultaneously", Otherwise this exception would be thrown,

program

import java.util.*;
import java.lang.*;
class Concurrenttest
{
public static void main(String args[])
{
LinkedList a=new LinkedList();
a.add("wow");
ListIterator li=a.listIterator();
System.out.println(li.next());
a.add("world");//adding the object(modifying)
li.remove();// removing the object(modifying)
System.out.println(a);
}
}
 

Output

wow
Exception in thread "main" java.util.ConcurrentModificationException
        at java.util.LinkedList$ListItr.checkForComodification(Unknown Source)
        at java.util.LinkedList$ListItr.remove(Unknown Source)
        at Concurrenttest.main(Concurrenttest.java:12)
 
 In the above program you can see that i have used both the collection object and iterator to simultaneously modify the collection. In order to avoid this kind of problem, use any one,either iterator or collection object to modify the collection.

Use of Two Iterators simultaneously


This is the first reason which i have mentioned before,i.e, "using two iterators simultaneously to modify the collection".


Program

import java.util.*;
import java.lang.*;
class Concurrenttest
{
public static void main(String args[])
{
LinkedList a=new LinkedList();
a.add("wow");
a.add("world");
a.add("ganesh");
System.out.println(a);
ListIterator li=a.listIterator();
ListIterator li1=a.listIterator();
System.out.println(li.next());
li.remove();//one iterator removing the first object
li1.next();//Another iterator trying to read the first object that was removed
}
}

Output

[wow, world, ganesh]
wow
Exception in thread "main" java.util.ConcurrentModificationException
        at java.util.LinkedList$ListItr.checkForComodification(Unknown Source)
        at java.util.LinkedList$ListItr.next(Unknown Source)
        at Concurrenttest.main(Concurrenttest.java:16)

If you read the comment line of the above program you can understand what i have done.Thus in this case use only one iterator to modify and traverse the object  or use two iterators only to traverse the objects.


 
 
java errors and exceptions © 2010 | Designed by Chica Blogger | Back to top