ku916
asked on
Null pointer exception RMI Client
I'm writing this RMI program(s) and successfully started RMIRegistry and Server, when I try to start the Client (on the same machine as Server) I get this null pointer exception on a remote method call. getSegment is a method within one of the Impl's that are being called remotely from the client.
I've a non-RMI version of the program(s) and it works fine. What could cause this exception? method not being reference correctly?
I've also included the codes for server, client and Impl at the bottom of this message!
thank you in advacned for any tips or ideas?
C:\java\projects\seg_api_r mi>java -cp c:\java\projects\seg_api_r mi -Djava.rmi.server.codebase =file:\c:\ java\proje cts\seg_ap i_rmi\ -Djava.security.policy=fil e:\c:\java \projects\ seg_api_rm i\wideopen .policy DemoSegmentClient 127.0.0.1
Exception in thread "main" java.lang.NullPointerExcep tion
at DemoSegmentImpl.getSegment (DemoSegme ntImpl.jav a:113)
at sun.reflect.NativeMethodAc cessorImpl .invoke0(N ative Method)
at sun.reflect.NativeMethodAc cessorImpl .invoke(Na tiveMethod AccessorIm pl.
java:39)
at sun.reflect.DelegatingMeth odAccessor Impl.invok e(Delegati ngMethodAc ces
sorImpl.java:25)
at java.lang.reflect.Method.i nvoke(Meth od.java:32 4)
at sun.rmi.server.UnicastServ erRef.disp atch(Unica stServerRe f.java:261 )
at sun.rmi.transport.Transpor t$1.run(Tr ansport.ja va:148)
at java.security.AccessContro ller.doPri vileged(Na tive Method)
at sun.rmi.transport.Transpor t.serviceC all(Transp ort.java:1 44)
at sun.rmi.transport.tcp.TCPT ransport.h andleMessa ges(TCPTra nsport.jav a:4
60)
at sun.rmi.transport.tcp.TCPT ransport$C onnectionH andler.run (TCPTransp ort
.java:701)
at java.lang.Thread.run(Threa d.java:536 )
at sun.rmi.transport.StreamRe moteCall.e xceptionRe ceivedFrom Server(Str eam
RemoteCall.java:247)
at sun.rmi.transport.StreamRe moteCall.e xecuteCall (StreamRem oteCall.ja va:
223)
at sun.rmi.server.UnicastRef. invoke(Uni castRef.ja va:133)
at DemoSegmentImpl_Stub.getSe gment(Unkn own Source)
at DemoSegmentClient.<init>(D emoSegment Client.jav a:29)
at DemoSegmentClient.main(Dem oSegmentCl ient.java: 47)
Client:
import java.rmi.*;
import java.net.MalformedURLExcep tion;
import java.util.Locale;
import java.text.NumberFormat;
public class DemoSegmentClient {
private DemoSegment ds;
public DemoSegmentClient() {
int y = 0;
int hseKey = 0;
long start = 0, end = 0;
try {
//bind server object to object in client
ds = (DemoSegment)Naming.lookup ("rmi://12 7.0.0.1:10 99/DemoSeg ment");
// set start time
start = System.currentTimeMillis() ;
y = ds.getSegment(Integer.toSt ring(hseKe y));
System.out.println("Value returned is " + y);
// set the end time
end = System.currentTimeMillis() ;
System.out.println("The elpased time : "+(end - start));
}
catch (MalformedURLException malformedException) {
System.err.println("Bad URL: " + malformedException);
}
catch (NotBoundException notBoundException) {
System.err.println("Not Bound: " + notBoundException);
}
catch (RemoteException remoteException) {
System.err.println("Remote Exception: " + remoteException);
}
}
public static void main (String[] argv) {
new DemoSegmentClient();
}
}
Server:
import java.io.IOException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRem oteObject;
import java.net.MalformedURLExcep tion;
public class DemoSegmentServer {
// public No-argument constructor
public DemoSegmentServer() {
}
public static void main(String args[]) {
new DemoSegmentServer();
DemoSegment bm = null;
try {
// Create a BankManager object
bm = new DemoSegmentImpl();
// Export it to RMI
UnicastRemoteObject.export Object( bm );
}
catch (RemoteException remoteException) {
System.err.println("Failur e during object export to RMI: " + remoteException);
}
// Register an external name for the service
try {
Naming.rebind("//localhost /DemoSegme nt", bm);
}
catch (RemoteException remoteException) {
System.err.println("Failur e during Name registration: " + remoteException);
}
catch (MalformedURLException malformedException) {
System.err.println("Failur e during Name registration: " + malformedException);
}
System.out.println("Server started.");
System.out.println("Enter <CR> to end.");
try {
int i = System.in.read();
}
catch (IOException ioException) {
}
System.exit(0);
}
}
Impl:
import java.sql.*;
import oracle.jdbc.driver.*;
import oracle.jdbc.pool.*;
import oracle.jdbc.OracleConnecti on;
import java.rmi.server.UnicastRem oteObject;
import java.rmi.RemoteException;
public class DemoSegmentImpl implements DemoSegment {
private OracleConnectionPoolDataSo urce myDataSource = null; // Connection Pool Data Source
private OraclePooledConnection myPooledConnection = null; // Pooled Connection
private SegmentBeanImpl segBean = null;
private int segScheme = 1;
// Default constructor
//
public void DemoSegment() throws java.rmi.RemoteException {
// Defualt connection here
//
try {
// Create Oracle DataSource Instance
myDataSource = new OracleConnectionPoolDataSo urce();
// Set connection parameters
<connection parameters ...>
// Get Pooled Connection
myPooledConnection = (OraclePooledConnection)my DataSource .getPooled Connection ();
// Enable Explicit Caching on Pooled Connection
myPooledConnection.setStat ementCache Size(5);
myPooledConnection.setImpl icitCachin gEnabled(t rue);
}
catch(SQLException e) {
System.out.println("Error Logging in to Oracle: " + e.getMessage());
System.exit(1);
//throw new Exception("Error Logging in to Oracle: " + e.getMessage());
}
// Set the default seg scheme
//
segScheme = 1;
// Create the segment bean
//
segBean = new SegmentBeanImpl();
segBean.setPooledConnectio n(myPooled Connection );
UnicastRemoteObject.export Object(seg Bean);
}
// Other constructor
//
public void DemoSegment(String jdbcDriver,
String dbUserName,
String dbPassword,
int segmentationScheme) throws java.rmi.RemoteException {
try {
// Create Oracle DataSource Instance
myDataSource = new OracleConnectionPoolDataSo urce();
// Set connection parameters
<connection parameters ...>
// Get Pooled Connection
myPooledConnection = (OraclePooledConnection)my DataSource .getPooled Connection ();
// Enable Explicit Caching on Pooled Connection
myPooledConnection.setStat ementCache Size(5);
myPooledConnection.setExpl icitCachin gEnabled(t rue);
}
catch(SQLException e) {
System.out.println("Error Logging in to Oracle: " + e.getMessage());
System.exit(1);
//throw new Exception("Error Logging in to Oracle: " + e.getMessage());
}
// Create the segment bean
//
segBean = new SegmentBeanImpl();
segBean.setPooledConnectio n(myPooled Connection );
UnicastRemoteObject.export Object(seg Bean);
}
// Explicit cleanup method
//
public void cleanupDemoSegment() throws java.rmi.RemoteException {
// Release Oracle resouces
//
try {
myPooledConnection.close() ;
}
catch (SQLException e) {} // ignore
}
public int getSegment(String houseKey) throws java.rmi.RemoteException {
return( segBean.getSegment(houseKe y, segScheme));
}
}
I've a non-RMI version of the program(s) and it works fine. What could cause this exception? method not being reference correctly?
I've also included the codes for server, client and Impl at the bottom of this message!
thank you in advacned for any tips or ideas?
C:\java\projects\seg_api_r
Exception in thread "main" java.lang.NullPointerExcep
at DemoSegmentImpl.getSegment
at sun.reflect.NativeMethodAc
at sun.reflect.NativeMethodAc
java:39)
at sun.reflect.DelegatingMeth
sorImpl.java:25)
at java.lang.reflect.Method.i
at sun.rmi.server.UnicastServ
at sun.rmi.transport.Transpor
at java.security.AccessContro
at sun.rmi.transport.Transpor
at sun.rmi.transport.tcp.TCPT
60)
at sun.rmi.transport.tcp.TCPT
.java:701)
at java.lang.Thread.run(Threa
at sun.rmi.transport.StreamRe
RemoteCall.java:247)
at sun.rmi.transport.StreamRe
223)
at sun.rmi.server.UnicastRef.
at DemoSegmentImpl_Stub.getSe
at DemoSegmentClient.<init>(D
at DemoSegmentClient.main(Dem
Client:
import java.rmi.*;
import java.net.MalformedURLExcep
import java.util.Locale;
import java.text.NumberFormat;
public class DemoSegmentClient {
private DemoSegment ds;
public DemoSegmentClient() {
int y = 0;
int hseKey = 0;
long start = 0, end = 0;
try {
//bind server object to object in client
ds = (DemoSegment)Naming.lookup
// set start time
start = System.currentTimeMillis()
y = ds.getSegment(Integer.toSt
System.out.println("Value returned is " + y);
// set the end time
end = System.currentTimeMillis()
System.out.println("The elpased time : "+(end - start));
}
catch (MalformedURLException malformedException) {
System.err.println("Bad URL: " + malformedException);
}
catch (NotBoundException notBoundException) {
System.err.println("Not Bound: " + notBoundException);
}
catch (RemoteException remoteException) {
System.err.println("Remote
}
}
public static void main (String[] argv) {
new DemoSegmentClient();
}
}
Server:
import java.io.IOException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRem
import java.net.MalformedURLExcep
public class DemoSegmentServer {
// public No-argument constructor
public DemoSegmentServer() {
}
public static void main(String args[]) {
new DemoSegmentServer();
DemoSegment bm = null;
try {
// Create a BankManager object
bm = new DemoSegmentImpl();
// Export it to RMI
UnicastRemoteObject.export
}
catch (RemoteException remoteException) {
System.err.println("Failur
}
// Register an external name for the service
try {
Naming.rebind("//localhost
}
catch (RemoteException remoteException) {
System.err.println("Failur
}
catch (MalformedURLException malformedException) {
System.err.println("Failur
}
System.out.println("Server
System.out.println("Enter <CR> to end.");
try {
int i = System.in.read();
}
catch (IOException ioException) {
}
System.exit(0);
}
}
Impl:
import java.sql.*;
import oracle.jdbc.driver.*;
import oracle.jdbc.pool.*;
import oracle.jdbc.OracleConnecti
import java.rmi.server.UnicastRem
import java.rmi.RemoteException;
public class DemoSegmentImpl implements DemoSegment {
private OracleConnectionPoolDataSo
private OraclePooledConnection myPooledConnection = null; // Pooled Connection
private SegmentBeanImpl segBean = null;
private int segScheme = 1;
// Default constructor
//
public void DemoSegment() throws java.rmi.RemoteException {
// Defualt connection here
//
try {
// Create Oracle DataSource Instance
myDataSource = new OracleConnectionPoolDataSo
// Set connection parameters
<connection parameters ...>
// Get Pooled Connection
myPooledConnection = (OraclePooledConnection)my
// Enable Explicit Caching on Pooled Connection
myPooledConnection.setStat
myPooledConnection.setImpl
}
catch(SQLException e) {
System.out.println("Error Logging in to Oracle: " + e.getMessage());
System.exit(1);
//throw new Exception("Error Logging in to Oracle: " + e.getMessage());
}
// Set the default seg scheme
//
segScheme = 1;
// Create the segment bean
//
segBean = new SegmentBeanImpl();
segBean.setPooledConnectio
UnicastRemoteObject.export
}
// Other constructor
//
public void DemoSegment(String jdbcDriver,
String dbUserName,
String dbPassword,
int segmentationScheme) throws java.rmi.RemoteException {
try {
// Create Oracle DataSource Instance
myDataSource = new OracleConnectionPoolDataSo
// Set connection parameters
<connection parameters ...>
// Get Pooled Connection
myPooledConnection = (OraclePooledConnection)my
// Enable Explicit Caching on Pooled Connection
myPooledConnection.setStat
myPooledConnection.setExpl
}
catch(SQLException e) {
System.out.println("Error Logging in to Oracle: " + e.getMessage());
System.exit(1);
//throw new Exception("Error Logging in to Oracle: " + e.getMessage());
}
// Create the segment bean
//
segBean = new SegmentBeanImpl();
segBean.setPooledConnectio
UnicastRemoteObject.export
}
// Explicit cleanup method
//
public void cleanupDemoSegment() throws java.rmi.RemoteException {
// Release Oracle resouces
//
try {
myPooledConnection.close()
}
catch (SQLException e) {} // ignore
}
public int getSegment(String houseKey) throws java.rmi.RemoteException {
return( segBean.getSegment(houseKe
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I will leave a recommendation in the Cleanup topic area that this question is:
- Points for LexZEUS
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
Venabili
EE Cleanup Volunteer