我想保留一个静态字符串数组来保存在调用服务器时从客户端传递的变量,然后能够使用getter从客户端访问它们.
由于某种原因,我只能得到非常基本的类型(例如int而不是Integer),其他一切都会抛出空指针异常.
这是一段代码片段.(使用GWT)
@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements AddElection
{
//this seems to be throwing a NullPointerException:
static String[] currentElections;
static int index;
public String electionServer(String input) {
// save currently running elections
currentElections[index] = input;
index = index + 1;
// TODO: getcurrentElections
Run Code Online (Sandbox Code Playgroud)
所以.我的问题是,如果我想暂时在服务器端存储字符串数组并能够访问它,我将如何在谷歌网络工具包中这样做?谢谢!
你没有初始化你的静态数组.
至少你必须做这样的事情:
static String[] currentElections = new String[ 100 ];
Run Code Online (Sandbox Code Playgroud)
但似乎您的数组可能会随着时间而增长,因此最好使用集合类:
static List<String > currentElections = new ArrayList<String >();
public String electionServer(String input) {
// save currently running elections
currentElections.add( input );
}
Run Code Online (Sandbox Code Playgroud)
但是如果可以从多个客户端同时调用此方法,请小心.然后你必须像这样同步访问:
static List<String > currentElections =
Collections.synchronizedList( new ArrayList<String >() );
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
737 次 |
| 最近记录: |