开发者

JavaME MIDlet Will Not Clear Variables?

开发者 https://www.devze.com 2023-02-03 12:50 出处:网络
I have written a JavaME program that accesses a web service to retrieve values, the web service is running on a tomcat server.

I have written a JavaME program that accesses a web service to retrieve values, the web service is running on a tomcat server.

I am facing a very strange problem, whenever i run the program, the program downloads new values like I anticipated but instead of adding them to an unitialized array it seems to store the previous arrays values and just add them. Maybe the code would exp开发者_StackOverflow中文版lain it better.

I first create a string array to store the retrieved values and a small int value to use when looping through them, i also create storage for the services and stubs:

public class StockQuery extends MIDlet implements CommandListener
{
    String[] newResults = null;
    int i = 0;
    String stockSym = null;
    getDataService service = null;
    .....

The midlet then calls a form when the startApp() method runs and this lets to the input a value into a textbox and then press the send command to send the value to a server, this server will send back an array of values (18 to be precise). To implement this I then call the commandAction() method.

public void commandAction(Command c, Displayable d)
{
    else if(c == cmdSend)
    {
        stockSym = getSym.getString();
        service = new getDataService_Stub();
        try
        {
            newResults = service.getStock(stockSym);
            for(i=0; i<newResults.length; i++)
            {
                System.out.println("Data " + i + "    -    " + newResults[i]);
            }
        }
        catch (RemoteException ex)
        {
            ex.printStackTrace();
        }

Now the program should download and place 18 values into the string array and the loop through them. It does do this but it will not flush the previous values from the array for reasons i cant quite figure out.

For example, the first time I executed the loop, the program did loop and print the 18 values, however the second time, it printed the initial 18 values from the previous run and then the extra 18 values it had just downloaded. I have closed my IDE, restarted my PC and deleted Netbeans cache folder and for some reason it will always store the values in the string array and the loop value even though I declared it to 0 whenever the program starts.

I am now on my 15th run of the program and the program output is:

 Data 267    -    34092040
 Data 268    -    22.73 - 31.58
 Data 269    -    NasdaqNM
 Data 270    -    1.95

Can anyone explain why the variables are not being cleared?


The problem seems to be in getDataService_Stub.getStock method. It might be using local device persistency to store previous server data. Please take a look at it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号