Link to home
Start Free TrialLog in
Avatar of ku916
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_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:
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:
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:
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.getSegment(houseKey, segScheme));
    }

}
ASKER CERTIFIED SOLUTION
Avatar of LexZEUS
LexZEUS

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
No comment has been added lately, so it's time to clean up this TA.
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