无法从远程方法获取值

问题描述:

我无法从远程方法'new DatabaseSelection2()。siti.getDatabasesName()'中获取值以填充'DatabaseSelection2'类中的数组databasesNames。我大胆地创造了例外的问题线。我无法解决它有人帮助我我张贴SchoolInterfaceSchoolInterfaceImpl,SchoolServer,及其DatabaseSelection2。我已经试过资源的每一个意思,但没有找到任何答案无法从远程方法获取值

class DatabaseSelection2: 

    package schoolclient; 


    import java.rmi.Naming; 
    import java.rmi.RemoteException; 
    import java.sql.SQLException; 

    import javax.swing.JOptionPane; 

    import schoolserver.SchoolInterface; 

    public class DatabaseSelection2 { 

     SchoolInterface siti = null; 
     public static void main (String[] args){ 

      try { 
      new DatabaseSelection2().siti = 
        (SchoolInterface) Naming.lookup("SchoolServer"); 
      } 
      catch (Exception e) { 
       e.printStackTrace(); 
      } 
      **for(Object o : getDatabaseTable())**//line 23 
      System.out.println(o); 
     } 

     private static Object[] getDatabaseTable() { 

      Object[] databasesNames = new Object[10]; 
      int i = 0; 
      try { 
       **for(Object o : new DatabaseSelection2().siti.getDatabasesName())** //line 32 
        databasesNames[i++] = o; 
      } 
      catch (SQLException e) { 
       JOptionPane.showMessageDialog(null, "SQLException in read" 
         + "Databases\n" + e, "Error", JOptionPane.ERROR_MESSAGE); 
      } 
      catch (RemoteException e) { 
       JOptionPane.showMessageDialog(null, "RemoteException in read Databases\n" + e, 
         "Error", JOptionPane.ERROR_MESSAGE); 
      } 

      return databasesNames; 
     } 
    } 

Exception in thread "main" java.lang.NullPointerException 
    at schoolclient.DatabaseSelection2.getDatabaseTable(DatabaseSelection2.java:32) 
    at schoolclient.DatabaseSelection2.main(DatabaseSelection2.java:23) 

接口SchoolInterface

package schoolserver; 

import java.rmi.Remote; 
import java.rmi.RemoteException; 
import java.sql.SQLException; 
import java.util.ArrayList; 
import java.util.List; 


public interface SchoolInterface extends Remote { 
    public ArrayList getDatabasesName() throws RemoteException, SQLException; 

} 

类SchoolServer

package schoolserver; 

import java.rmi.Naming; 

public class SchoolServer { 
    public static void main (String[] args) { 
     try { 
      SchoolInterfaceImpl sii = new SchoolInterfaceImpl(); 
      Naming.rebind("SchoolServer", sii); 
     } 
     catch (Exception e) { 

     } 
    } 
} 

类SchoolInterfaceImpl:

package schoolserver; 

import java.rmi.RemoteException; 
import java.rmi.server.UnicastRemoteObject; 
import java.sql.Connection; 
import java.sql.Statement; 
import java.util.ArrayList; 
import java.util.List; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.ResultSetMetaData; 
import java.sql.SQLException; 

public class SchoolInterfaceImpl 
      extends UnicastRemoteObject implements SchoolInterface { 

    protected SchoolInterfaceImpl() throws RemoteException { 
     super(); 
     // TODO Auto-generated constructor stub 
    } 
    public ArrayList getDatabasesName() 
      throws RemoteException, SQLException { 
     ArrayList databasesName = null; 
     Connection connection = null; 
     ResultSet resultSet = null; 
     try { 
     connection = DriverManager.getConnection(
       "jdbc:sqlserver://localhost\\FAISAL:1433;" 
       + "username=fas;password=24071982"); 
     resultSet = connection.getMetaData().getCatalogs(); 
     while(resultSet.next()){ 
      databasesName.add(resultSet.getObject(1)); 
     } 
     } 
     catch (SQLException e) { 
      throw new SQLException(); 
     } 
     finally{ 
      try { 
       if(connection != null) 
        connection.close(); 
      } 
      catch(SQLException e) { 
       throw new SQLException(); 
      } 
      try { 
       if(resultSet != null) 
        resultSet.close(); 
      } 
      catch(SQLException e) { 
       throw new SQLException(); 
      } 
     } 
     return databasesName; 
    } 

} 
+0

堆栈跟踪请? –

+0

异常在线程 “主” 显示java.lang.NullPointerException \t在schoolclient.DatabaseSelection2.getDatabaseTable(DatabaseSelection2.java:32) \t在schoolclient.DatabaseSelection2.main(DatabaseSelection2.java:23) – faisal

+0

且其线DatabaseSel ection2 .java:32 –

private static Object[] getDatabaseTable() { 

     Object[] databasesNames = null; 
     int i = 0; 
     try { 
      for(Object o : new DatabaseSelection2().siti.getDatabasesName()) 
       databasesNames[i] = o; 
     } 

这里databasesNames为空,你在做空操作,这就是为什么你得到空指针异常

+0

我将此远程方法调用getDatabasesName()从(Object o:new DatabaseSelection2()。siti.getDatabasesName())为什么这不起作用 – faisal

+0

正如Anand Sinha所说,你还没有创建'databasesNames'数组。你需要做一些像'databasesNames = new Object [10];' –

+0

databasesNames是一个未初始化的数组,它的值为空。请初始化该阵列 –