Hi guys.

I have an observer/observable structure set up in my code that changes a bunch of TextField values with updated data, no big problem here. However, in my update(Observable obs, Object arg) method, I am trying to call another method to insert the new values into my database.

I have tried the method calling code elsewhere and it writes to the database fine, so I'm wondering if there is some kind of restriction on calling other methods in the "update" method because it is a special observer/observable method?

Here is what I mean:

public void update(Observable obs, Object args){

        Location loc = (Location)obs;
        weatherOverview.setText(loc.getWeatherOverview());
        String temp = Double.toString(loc.getTemp());
        temperatureField.setText(temp);
        windDirection.setText(loc.getWindDirection());
        String spd = Double.toString(loc.getWindSpeed());
        windspeedField.setText(spd);
        pressureField.setText(loc.getPressure());
        String date = getDateAndTime();
        String location = loc.toString();
        locateServer();
        try{server.writeWeatherHistory("a", "b", "c", "d", "e", "f", "g");}
        catch(Exception e){}
        
        
    }

The red lines are the section that isn't working. the locateServer() method is necessary for calling the "writeWeatherHistory" method via RMI. This code works when it is outside of the update method but it would be very good if it would work whenever update is called, so that the new data can be inserted at the right time.

The string variables in the writeWeatherHistory method are for test purposes only, in reality I would put in all the String values assigned to the above text fields.

But yes, any replies greatly appreciated

Recommended Answers

All 2 Replies

If it is working outside of the update() method, but not inside of the update() method, (and you're sure the update method is actually being called) then it is due to a side effect. So some other condition which is either true before the update() method gets called, or which is true as a result of update() being called, is causing your code to crash. But no, there is no restriction on calling methods from within other methods in the sense you're saying.

Did you try putting something in the 'catch' portion of the code? Such as printing the stack trace . . ? And are you sure the update() method actually gets called and gets that far (i.e., makes it down to the try/catch)?

> But yes, any replies greatly appreciated

Print out the stack-trace of the exception (if thrown) instead of gobbling it up for more details.

IMO the design seems kinda wrong; you really shouldn't be persisting the data in an Observer since theoretically an Observable can have multiple observers. The right place to persist updates would be at the source of the data change i.e. in your Observable.

HTH

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.