从表中获取对象

时间:2014-05-28 20:35:17

标签: java

我写了这个函数。我试图在对象表中找到一个对象并返回一个字符串表。

public Server(wine[] d) {
   this.d=d;
}

public String[] getData(String name) {
   String[] data = new String[2];
   int i=0;         

   while(i < d.length && s != d[i].id)
      i++;

   if (i < d.length) {
      data[0] = d[i].name;
      data[1] = d[i].price;
      return data;
   } 
     else System.out.println(s + " Not Found ");

   data[0] = "error";
   data[1] = "error";
   return data;
 }

我收到了这个错误

getData  invocation: system exception was thrown.
org.omg.CORBA.MARSHAL: Cannot marshall null string.

我认为问题出在以下两行:

data[0]= d[i].name;
data[1]= d[i].price;

1 个答案:

答案 0 :(得分:0)

如果我理解你的要求是正确的,你想从一系列葡萄酒对象中得到一种葡萄酒的名称和价格。试试这个。

public String[] getData(String name)
{
    String[] data = new String[2];
    boolean wineFound = false;

    for (int i = 0; i < d.length; i++)
    {
        if( d[i].name.equals(name) )
        {
        data[0] = name;
        data[1] = d[i].price;
        wineFound = true;
        }
    }

    if (wineFound == true)
        return data;
    else
    {
    data[0] = "error";
    data[1] = "error";
    return data;
    }
}