-2

I am unable to get the values from the remote method 'new DatabaseSelection2().siti.getDatabasesName()' to fill the array databasesNames in 'DatabaseSelection2' class. I make bold the issue line which create the exception. I am unable to solve it someone help me I am posting SchoolInterface, SchoolInterfaceImpl, SchoolServer, and its DatabaseSelection2. I have tried every mean of resource but do not find any answer

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)

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

}

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

        }
    }
}

Class 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;
    }

}
faisal
  • 19
  • 1
  • 8
  • Stack trace please? – Steve Smith Jun 01 '17 at 15:25
  • Exception in thread "main" java.lang.NullPointerException at schoolclient.DatabaseSelection2.getDatabaseTable(DatabaseSelection2.java:32) at schoolclient.DatabaseSelection2.main(DatabaseSelection2.java:23) – faisal Jun 01 '17 at 15:26
  • And which is line DatabaseSel‌​ection2.java:32 – Steve Smith Jun 01 '17 at 15:30
  • at schoolclient.DatabaseSelection2.main(DatabaseSelection2.java:23) for(Object o : getDatabaseTable()) at schoolclient.DatabaseSelection2.getDatabaseTable(DatabaseSelection2.java:32) for(Object o : new DatabaseSelection2().siti.getDatabasesName()) – faisal Jun 01 '17 at 15:32
  • Also, please copy the exception info into the question itself, not just the comments – EJoshuaS - Stand with Ukraine Jun 01 '17 at 16:41

1 Answers1

2
private static Object[] getDatabaseTable() {

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

here databasesNames is null and you are doing operation on null , that is why you are getting null pointer exception

Anand Sinha
  • 322
  • 3
  • 7
  • I am calling this remote method getDatabasesName() from this line for(Object o : new DatabaseSelection2().siti.getDatabasesName()) why this is not working – faisal Jun 01 '17 at 15:30
  • As Anand Sinha says, you haven't created the `databasesNames` array. You need to do something like `databasesNames = new Object[10];` – Steve Smith Jun 01 '17 at 15:32
  • databasesNames is an Array which is not initialized and its value is null . Please initialize that array – Anand Sinha Jun 01 '17 at 15:32
  • I do it now but it didn't solve the issue – faisal Jun 01 '17 at 15:40
  • what change you did in code and what exception is coming. Please send that exception. – Anand Sinha Jun 01 '17 at 15:43
  • 'Object[] databasesNames = new Object[15]; int i = 0; try { for(Object o : new DatabaseSelection2().siti.getDatabasesName()) databasesNames[i++] = o; }' the same exception 'Exception in thread "main" java.lang.NullPointerException at schoolclient.DatabaseSelection2.getDatabaseTable(DatabaseSelection2.java:32) at schoolclient.DatabaseSelection2.main(DatabaseSelection2.java:23)' – faisal Jun 01 '17 at 15:46
  • It is coming because your databaseName is more than 15 . If you are not sure about size then you can use List. – Anand Sinha Jun 01 '17 at 15:59
  • i have checked from another program i have just 4 databases – faisal Jun 02 '17 at 01:30