2017-06-01 5 views
-2

Ich bin nicht in der Lage, die Werte von der remote-Methode 'new DatabaseSelection2(). Siti.getDatabasesName()', um die Array-DatenbankenNames in 'DatabaseSelection2' -Klasse zu füllen. Ich mache die Fehlerzeile fett, die die Ausnahme erzeugt. Ich bin nicht in der Lage, es zu lösen. Jemand hilft mir. Ich poste SchoolInterface, SchoolInterfaceImpl, SchoolServer, und seine DatabaseSelection2. Ich habe jedes Mittel der Ressource versucht, aber finde keine AntwortKann die Werte von der Remote-Methode nicht abrufen

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) 

Schnittstelle 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; 

} 

Klasse 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) { 

     } 
    } 
} 

Klasse 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

Stack-Trace bitte? –

+0

Exception in thread "main" java.lang.NullPointerException \t bei schoolclient.DatabaseSelection2.getDatabaseTable (DatabaseSelection2.java:32) \t bei schoolclient.DatabaseSelection2.main (DatabaseSelection2.java:23) – faisal

+0

, und die Linie ist DatabaseSel ection2 .java: 32 –

Antwort

2
private static Object[] getDatabaseTable() { 

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

hier databasesNames ist null und Sie tun Operation auf Null, deshalb erhalten Sie null Zeiger Ausnahme

+0

Ich rufe diese Remote-Methode getDatabasesName() aus Diese Zeile für (Object o: new DatabaseSelection2(). siti.getDatabasesName()) Warum das nicht funktioniert – faisal

+0

Wie Anand Sinha sagt, haben Sie das Array "databasesNames" nicht erstellt. Sie müssen etwas tun wie 'databasesNames = new Object [10];' –

+0

databaseNames ist ein Array, das nicht initialisiert ist und dessen Wert null ist. Bitte initialisiere das Array –

Verwandte Themen