我无法从远程方法'new DatabaseSelection2()。siti.getDatabasesName()'获取值来填充'DatabaseSelection2'类中的数组databasesNames。我将创建异常的问题行加粗。我无法解决问题有人帮助我发布SchoolInterface
,SchoolInterfaceImpl
,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)
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;
}
}
答案 0 :(得分:2)
private static Object[] getDatabaseTable() {
Object[] databasesNames = null;
int i = 0;
try {
for(Object o : new DatabaseSelection2().siti.getDatabasesName())
databasesNames[i] = o;
}
这里databasesNames为null,你正在对null进行操作,这就是你得到空指针异常的原因