I've got an interface:
package com.aex;
import javax.jws.WebParam;
public interface IFonds {
double getKoers();
String getNaam();
void setKoers(@WebParam(name="koers") double koers); }
And the class:
/*
* 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;
}
}
Now I want to send over an collection of the interface with a webservice, so here is my class I send:
package com.aex;
import java.util.Collection;
import java.util.*;
import javax.jws.*;
/**
*
* @author Julian
*/
@WebService
public class AEX implements IAEX {
Collection&开发者_运维问答lt;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;
}
}
The problem is that I get an nullpointerexception in the constructor of the last shown class (AEX). This is because I want to add the object into the interface collection. Anyone got solution for this?
Yes: initialize your collection variable!
public AEX()
{
IFonds fonds1 = new Fonds("hema", 3.33);
// This is the line you were missing
fondsen = new ArrayList<IFonds>();
fondsen.add(fonds1);
}
Note that this actually has nothing to do with interfaces or web services... reference type fields default to null unless you explicitly initialize them, whatever the context.
精彩评论