2015-01-01 61 views
-1

我找不到錯誤java.lang.NullPointerException在'e = servidor.listarTrinos();'listaTrinosSistema爲空。跳過ServicioDatosInterfaceTrino異常java.lang.NullPointerException與列表

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+"/"); 
     } 
    } 
} 

package 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
private static ServicioDatosImpl servidor; 

聲明一個靜態變量

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

這個聲明並初始化具有相同的名稱作爲靜態變量的局部變量。靜態變量保持爲空。它應該是

servidor = new ServicioDatosImpl() 
1

如果您在e = servidor.listarTrinos()上有NPE,則servidor爲空。

這是因爲你有一個靜態屬性servidor與您建立在main方法的變量servidor。

相關問題