I confused with extends, i went with theory n solved some examples,
But when i come to my main project still i am doing something wrong and i am not able to find it.
Can any one help me.
ConnectionServiceImpl is called by RPC
When the client call the COnnectionServiceImpl the connectionParameter is set .I checked it by display in the
Map<String, String>
but when second rpc call is done to the ObjectStore by client
its give me error NullPointerException for the same parameter which was set in the super class
public class ConnectionServiceImpl extends RemoteServiceServlet{
// some code including initialization
private ConnectionParameter connectionParameter ;
private Map<String, String> parameter;
public String connection() {
connectionParameter = new ConnectionParameter("abc","xyz",repositoryName);
setConnectionParameter();
setSession();
return getConnected();
}
protected Map<String, String> getConnectionParameter() {
Iterator iterator = parameter.keySet().iterator();
while(iterator.hasNext()){
String key = iterator.next().toString();
String value = parameter.get(key);
System.out.println("key " + key + " value "+value);
}
return parameter;
}
private void setConnectionParameter(){
this.parameter=connectionParameter.getParameter();
}
}
This above class is working fine.
now,
public class ObjectStore extends ConnectionServiceImpl {
// some code
public ObjectStore() {
// TODO Auto-generated constructor stub
this.parameter = getConnectionParameter(); **<--- NullPointer Error**
}
}
Can anybody explain this or tell the mistake
public class ConnectionParameter {
private String repositoryID;
private static String username;
private static String password;
private static String AtomPubUrl;
private static String bindingType;
private Map<String, String> parameter = new HashMap<String, String>();
public ConnectionParameter(String username, String password,
String repositoryId) {
// TODO Auto-generated constructor stub
AtomPubUrl = "http://192.168.1.32:9083/CaseManager/resources/Service";
bindingType = BindingType.ATOMPUB.value();
this.username = username;
this.password = password;
this.repositoryID=repositoryId;
setConnectionParameter();
}
Map<String, String> getParameter() {
return parameter;
}
private void setConnectionParameter() {
parameter.put(SessionParameter.USER,username);
parameter.put(SessionParameter.PASSWORD, password);
parameter.put(SessionParameter.ATOMPUB_URL,AtomPubUrl);
parameter.put(SessionParameter.BINDING_TYPE, bindingType);
parameter.put(SessionParameter.REPOSITORY_ID, repositoryID);
}
}
Stacktrace
java.lang.NullPointerException
at com.server.ConnectionServiceImpl.getConnectionParameter(ConnectionServiceImpl.java:107)
at com.server.ObjectStore.<init>(ObjectStore.java:27)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at java.lang.Class.newInstance0(Class.java:355)
at java.lang.Class.newInstance(Class.java:308)
at org.mortbay.jetty.servlet.Holder.newInstance(Holder.java:153)
at org.mortbay.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:428)
You seem to have a problem with initialization order and ownerhsip of member fields.
Have you initialized
ConnectionServiceImpl.parameterin its constructor? You don’t show any constructor there – do you have any? If not, then right after constructing the object,parameteris uninitialized. Which means that callinggetConnectionParameterbeforeconnectiongives you a null pointer. (Although it should not result in an NPE – is the code you show us really producing an NPE as you claim?).From the above follows that your call to
getConnectionParameterfrom the constructor ofObjectStoreis an error, unless you have initializedConnectionServiceImpl.parameterbefore (in the parent class constructor).Apparently you either have a field called
parameterinObjectStore(which duplicatesConnectionServiceImpl.parameter, thus is highly discouraged), orConnectionServiceImpl.parameter– whose declaration is not shown in your code – is notprivate, thus accessible from the subclass, and you are trying to initialize it from the subclass constructor (which is wrong in general – the base class should take care of initializing its fields properly). And if this is the case, you are actually attempting “circular initialization”: to initialize the field in the subclass with its own value taken via the superclass getter, which is foolish.