user2689576
user2689576

Reputation: 41

Java strange assignment issue

I have got a problem and can't get my head around it.

I assigned integer a to member dataAction in method setDataAction(int a) of class Action.

After assignment the values are still different! a == -3, dataAction == 0. No error or complaint was evoked neither by Eclipse nor by Java.

Environment: Win7-64 with Eclipse Juno and JDK-7u25-win-64.

To prevent any comment in this direction, the assignment was done on an instance of the class. I have tried it on several machines and even set up a pristine one with definitely no virus, only OS & Environment!

a better picture of the debugger screen shot is available as well at: http://setosix.eu/Java_Assignment_Problem.jpg / .gif / .png

    // Class to hold return values of the applet to determine further action
    class Action implements Constants {
      int dataAction;
      int appletAction;
      int dataResult;

      Action() {
        dataAction = MOVE_NOT;
        appletAction = ACT_CONTINUE;
        dataResult = LV_OK;
      }

      public void setDataAction(int a) {
        dataAction = a;
      }
    }

    // here the 'Action' instance is created
    public class TableResource extends Database {
      private Connection dbLink;
      private Statement stmt;
      protected ResultSet rs;
      // hold the return values of applet 
      protected Action nextAction;

      protected TableResource() {
        dbLink = getInstance().getConnection();
        rs = null;
        stmt = null;
        nextAction = new Action();
      }
      ...

     // data class for 'AppletMandanten' 
     public class DataMandanten extends TableResource implements Data {
     ...

    // constants are defined here
    public interface Constants  {
      // constants defining moves in record-/browse-mode 
      public static final int MOVE_NOT = 0;
      public static final int MOVE_FIRST = -1;
      public static final int MOVE_LAST = -2;
      public static final int MOVE_NEXT = -3;
      public static final int MOVE_PREVIOUS = -4;
      public static final int MOVE_NEXTPAGE = -5;
      public static final int MOVE_PREVPAGE = -6;
      ...


    // interface ‘Data’ extends interface ‘Constants’
    public interface Data extends Constants {
    ... 


    // in this class the instance ‘data’ is creates
    public class ModulMandanten extends EventHandler implements Data {
    ...

      // main control of applet
      public int control() {
        int leave = LV_OK;
        DataMandanten data;

        // connect to database
        db = Database.getInstance();

        if( db.dbConnect() < 0 ) {
          Intelligence.out( 1,  "ERROR in ("+"RWG"+"): Failed to connect to Server!");
          leave = LV_ERROR;
        }   

        // here ‘data’ instance is created 
        data = new DataMandanten();
        ...


    public abstract class Applet extends ModulMandanten {
    ...

    // here the invocation takes place
    public class AppletMandanten extends Applet {
    ... 

      // handle events in class ‘AppleMandanten’ (derives from ‘ActionPerformed()’
      private void eventHandler( ActionEvent event ) {
      ...
        // invocation is called in method 'eventHandler', class 'AppletMandanten'
        switch (eventName){     
          case ("btnNext"): {
            // 'next' button pressed
            data.nextAction.setDataAction( MOVE_NEXT );
            // alternatively: doesn't work neither 
            data.nextAction.setDataAction = MOVE_NEXT;
            // 'data.nextAction.setDataAction' printed to console ( != MOVE_NEXT ) 
            System.out.println(Integer.toString(data.nextAction.dataAction));
            break;
           // !!! after this 'dataAction' isn't touched again !!!
         }
         ...

Java assignment issue in Eclipse debug mode http://setosix.eu/Java_Assignment_Problem.jpg

Upvotes: 4

Views: 231

Answers (1)

jboi
jboi

Reputation: 11912

Back to basics. Java does assignments right - one cannot expect a bug in this part of Java. When you write x=a and a is -3, than x will be -3 immediately afterwards. Several things I see in your code that can go wrong:

Multiple Threads

Change your code using AtomicInteger and change the caller, so that setting and getting while expecting the same value, is one atomic call to a method of AtomicInteger, not separate method calls. synchronized would not work in the case of the screenshot above.

Track back the problem

Log a and dataAction before the assignement. And if you want to be sure, directly after the assignment again, but be aware, that another thread could have been jumped in middle and did also a setDataAction(). I excpect, that you will find either some strange assigments of 0 or no call with -3.

Now that you see the wrong assignment, add a little code to your setDataAction().

// add in setDataAction()
if(a == the-wrong-value) try {
    throw new Exception("wrong assignment: " + a);
} catch(Exception e) {
    e.printStackTrace();
}

So, you find out, where the wrong assignment comes from and be able to track down what really happens.

Upvotes: 1

Related Questions