带列表的异常java.lang.NullPointerException

时间:2015-01-01 19:32:05

标签: java

我无法在“java.lang.NullPointerException'”行中找到错误e = servidor.listarTrinos();listaTrinosSistema是空的。跳过ServicioDatosInterfaceTrino

package Basededatos;

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import Common.Gui;
import Common.ServicioDatosInterface;
import Common.Trino;
import Common.Utils;

public class Basededatos {

    private static ServicioDatosImpl servidor;

    public static void main(String[] args) throws Exception {
        Utils.setCodeBase(ServicioDatosInterface.class);    
        ServicioDatosImpl servidor = new ServicioDatosImpl();
        ServicioDatosInterface remote = (ServicioDatosInterface)UnicastRemoteObject.exportObject(servidor, 8888);

        Registry registry = LocateRegistry.createRegistry(Registry.REGISTRY_PORT);

        registry.rebind("Pepito", remote);

        menu();
    }
    public static void menu() throws RemoteException{
        int opt = 0;
        do {
            opt = Gui.menu("Menu Base de Datos", new String[]{ "listarTrinos" });

            switch (opt) {
                case 0: listarTrinos(); break;
            }
        }
        while (opt != 1);
    }

    public static void listarTrinos() throws RemoteException{
        List<Trino> e;
        e = servidor.listarTrinos();
        System.out.print("Trinos enviados");
        Iterator<Trino> nombreIterator = e.iterator();
        while(nombreIterator.hasNext()){
            String elemento = nombreIterator.next().toString();
            System.out.print(elemento+" / ");
        }
    }
}

包Basededatos;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import Common.ServicioDatosInterface;
import Common.Trino;

    public class ServicioDatosImpl implements ServicioDatosInterface{

        private List<Trino> listaTrinosSistema = new ArrayList<Trino>();

        public List<Trino> listarTrinos() throws RemoteException{
            //if (listaTrinosSistema.isEmpty() == true || listaTrinosSistema.size()==0) return null;
            //else return listaTrinosSistema;
            return listaTrinosSistema;
        }

2 个答案:

答案 0 :(得分:2)

private static ServicioDatosImpl servidor;

这声明了一个静态变量

public static void main(String[] args) throws Exception {
    Utils.setCodeBase(ServicioDatosInterface.class);    
    ServicioDatosImpl servidor = new ServicioDatosImpl();

并声明并初始化一个与静态变量同名的局部变量。静态变量保持为null。它应该是

    servidor = new ServicioDatosImpl()

答案 1 :(得分:1)

如果你在e = servidor.listarTrinos()上有一个NPE,则servidor为空。

这是因为你有一个静态属性servidor,你在main方法中创建一个变量servidor。