2010-06-19 139 views
1

我有一個接口:Java接口問題

package com.aex; 

import javax.jws.WebParam; 

public interface IFonds { 
    double getKoers(); 
    String getNaam(); 
    void setKoers(@WebParam(name="koers") double koers); } 

和類:

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 

package com.aex; 

import java.io.Serializable; 
import javax.jws.*; 

/** 
* 
* @author Julian 
*/ 
@WebService 
public class Fonds implements IFonds, Serializable { 

    String naam; 
    double koers; 

    public double getKoers() { 
     return koers; 
    } 

    public String getNaam() { 
     return naam; 
    } 

public Fonds() 
{ 
} 

    public Fonds(String naam, double koers) 
    { 
     this.naam = naam; 
     this.koers = koers; 

    } 

    public void setKoers(@WebParam(name="koers")double koers) { 
     this.koers = koers; 
    } 

} 

現在我想接口的集合送過來一個web服務,所以這裏是我的I類派:

package com.aex; 

import java.util.Collection; 
import java.util.*; 
import javax.jws.*; 

/** 
* 
* @author Julian 
*/ 
@WebService 
public class AEX implements IAEX { 

    Collection<IFonds> fondsen; 

    public Collection<IFonds> getFondsen() { 
     return fondsen; 
    } 


    public AEX() 
    { 
     IFonds fonds1 = new Fonds("hema", 3.33); 


     //fondsen.add(fonds1); 
    } 

    public double getKoers(@WebParam(name="fondsnaam")String fondsNaam){ 

     Iterator iterator = fondsen.iterator(); 

     while(iterator.hasNext()) 
     { 
      Fonds tempFonds = (Fonds)iterator.next(); 
      if(tempFonds.getNaam().endsWith(fondsNaam)) 
      { 
       return tempFonds.getKoers(); 
      } 

     } 
     return -1; 
    } 

} 

的問題是,我得到了最後的顯示類(AEX)的構造一個NullPointerException異常。這是因爲我想將對象添加到接口集合中。任何人都有這個解決方案?

回答

5

是:初始化您的收藏變量!

public AEX() 
{ 
    IFonds fonds1 = new Fonds("hema", 3.33); 

    // This is the line you were missing 
    fondsen = new ArrayList<IFonds>(); 
    fondsen.add(fonds1); 
} 

注意,這其實沒有什麼用的接口或網絡服務...引用類型字段默認做的空,除非你明確地初始化它們,不管上下文。

+0

哈哈,我這麼笨。謝謝=) – Julian 2010-06-19 16:38:59