2010-06-19 88 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