File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Distributed Java and the fly likes Null Pointer Exception running RMI Client Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Distributed Java
Bookmark "Null Pointer Exception running RMI Client" Watch "Null Pointer Exception running RMI Client" New topic
Author

Null Pointer Exception running RMI Client

john d
Greenhorn

Joined: Jul 11, 2002
Posts: 9
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_rmi>java -cp c:\java\projects\seg_api_rmi -Djava.rmi.server.codebase=file:\c:\java\projects\seg_api_rmi\ -Djava.security.policy=file:\c:\java\projects\seg_api_rmi\wideopen.policy DemoSegmentClient 127.0.0.1
Exception in thread "main" java.lang.NullPointerException
at DemoSegmentImpl.getSegment(DemoSegmentImpl.java:113)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:261)
at sun.rmi.transport.Transport$1.run(Transport.java:148)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:144)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:4
60)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport
.java:701)
at java.lang.Thread.run(Thread.java:536)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Stream
RemoteCall.java:247)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:
223)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:133)
at DemoSegmentImpl_Stub.getSegment(Unknown Source)
at DemoSegmentClient.<init>(DemoSegmentClient.java:29)
at DemoSegmentClient.main(DemoSegmentClient.java:47)

Client: DemoSegmentClient.java
import java.rmi.*;
import java.net.MalformedURLException;
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://127.0.0.1:1099/DemoSegment");
// set start time
start = System.currentTimeMillis();
y = ds.getSegment(Integer.toString(hseKey));
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: DemoSegmentServer
import java.io.IOException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.net.MalformedURLException;
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.exportObject( bm );
}
catch (RemoteException remoteException) {
System.err.println("Failure during object export to RMI: " + remoteException);
}
// Register an external name for the service
try {
Naming.rebind("//localhost/DemoSegment", bm);
}
catch (RemoteException remoteException) {
System.err.println("Failure during Name registration: " + remoteException);
}
catch (MalformedURLException malformedException) {
System.err.println("Failure 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: DemoSegmentImpl.java
import java.sql.*;
import oracle.jdbc.driver.*;
import oracle.jdbc.pool.*;
import oracle.jdbc.OracleConnection;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;
public class DemoSegmentImpl implements DemoSegment {
private OracleConnectionPoolDataSource 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 OracleConnectionPoolDataSource();
// Set connection parameters
<connection parameters ...>
// Get Pooled Connection
myPooledConnection = (OraclePooledConnection)myDataSource.getPooledConnection();
// Enable Explicit Caching on Pooled Connection
myPooledConnection.setStatementCacheSize(5);
myPooledConnection.setImplicitCachingEnabled(true);
}
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.setPooledConnection(myPooledConnection);
UnicastRemoteObject.exportObject(segBean);
}
// Other constructor
//
public void DemoSegment(String jdbcDriver,
String dbUserName,
String dbPassword,
int segmentationScheme) throws java.rmi.RemoteException {
try {
// Create Oracle DataSource Instance
myDataSource = new OracleConnectionPoolDataSource();
// Set connection parameters
<connection parameters ...>
// Get Pooled Connection
myPooledConnection = (OraclePooledConnection)myDataSource.getPooledConnection();
// Enable Explicit Caching on Pooled Connection
myPooledConnection.setStatementCacheSize(5);
myPooledConnection.setExplicitCachingEnabled(true);
}
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.setPooledConnection(myPooledConnection);
UnicastRemoteObject.exportObject(segBean);
}

// 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.getSegmentBean(houseKey, segScheme));
}
}
Impl: SegmentBeanImpl.java
import java.io.*;
import java.sql.*;
import javax.sql.*;
import oracle.jdbc.pool.*;
import oracle.jdbc.driver.*;
import oracle.jdbc.OracleConnection;
import java.rmi.RemoteException;
/**
* Segment access bean
*/
public class SegmentBeanImpl implements Serializable, SegmentBean {
private OraclePooledConnection pooledconnection;
private static String segmentSelect = "<SELECT SOMETHING HERE!>";

/**
* Sets the dataSource property value.
*/
public void setPooledConnection(OraclePooledConnection pooledconnection) {
this.pooledconnection = pooledconnection;
}
/**
* Returns a segment for the input house and segment scheme.
* ?? What to return if no corresponding record found??
*/
public int getSegmentBean(String houseKey, int segScheme)
{
// Physical connection
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
int seg = -1;
// Get segment for this house
//
try {
// Get Physical Connection from OraclePooledConnection passed in from DemoSegment
conn = (this.pooledconnection).getConnection();
pstmt = conn.prepareStatement(segmentSelect);
pstmt.setString(1, houseKey);
pstmt.setInt(2, segScheme);
rs = pstmt.executeQuery();
if ( rs.next() ) { // fetch first/only row
seg = rs.getInt(1);
} else {
System.out.println("No Rows for housekey " + houseKey + " ,segScheme " + segScheme);
}
}
catch (SQLException se) {
System.out.println("SegmentBeanImpl.getSegment: SQLExecption occurred: " + se.getMessage());
}
finally {
try {
if (conn != null) {conn.close();}
if (rs != null) {rs.close();}
if (pstmt != null) {pstmt.close();
}
} catch (SQLException se) {} //ignore
}
return seg;
}
}
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Moving to the RMI forum.


"JavaRanch, where the deer and the Certified play" - David O'Meara
 
GeeCON Prague 2014
 
subject: Null Pointer Exception running RMI Client