5 Replies Latest reply: Apr 8, 2007 4:27 PM by 807599 RSS

    Two implementations of one interface running from the same main method

    807599
      Hi I am looking for help and advice on how I need to correct my code.

      I have so far made an array based implementation and a linked list based version of a FIFO queue. I have set them up so that they are both implementing the same FIFO interface and tested them on their own in separate projects and they pass all my tests.

      Next I made a new project and a new package and put all the source files and the interface into it and wrote a new main method that creates an array of queues randomly selecting between the array implementation and the linked list one. Now I have an error when I try and add an element to a linked list queue. I have gone back and checked that this does not occur in the separate project with the same code and can't figure out what the problem is. I think that I have some problem with pointers but what I can't say.

      Here is the offending piece of code: PS I am keeping the head of my list empty of data because when I put data in it it messed up my iterator.
      public void add (Object thisObj) {
      LLNode temp;
      
      if (Q_Empty()) {
          
          temp = new LLNode (thisObj);
          head = new LLNode(null); 
          head.setNext(temp);
          temp.setNext(null);
              
          }
      
      
      else { if (!Q_Empty() )
      {
         
          LLNode current = new LLNode(null);
          current = head;
          
          temp = new LLNode(thisObj);
          
          while (current.getNext() != null){
              current = current.getNext();
          }
          
          temp.setNext(null);
          current.setNext(temp);
          }
      }
      
      
      count++; //count is incremented as new element added to queue
      }
        • 1. Re: Two implementations of one interface running from the same main method
          3004
          Now I have
          an error when I try and add an element to a linked
          list queue.
          And what would that error be? And where exactly is it occurring?
          • 2. Re: Two implementations of one interface running from the same main method
            807599
            I have found out what my problem seems to be but still can't understand why it is occurring.

            When I do my random instantiation of the Llist queue and the array queues all the LList ones end up thinking they are the same list. I think I must have a memory leak somewhere but I can't see why. I don't get why this is going on because I do a new LListQueue() each time and assign them to different slots in my array of queues.

            Here is my code. I would really appreciate your help as I am now stuck.
             private LListQueue get_LListQueue(){
                    LListQueue Q = new LListQueue();
                return Q;
            }
            
                private void SetUpQueueArray() {
                     
                queue = new FIFO_INT[maxQueues] ; //new array of queues that conform to FIFO_INT
                FIFO_INT Q;
                
                Random RNums = new Random();
                
                    for (int i = 0; i < maxQueues ; i ++){ //for loop to instantiate a new queue in each slot of the array
                        
                        if (RNums.nextInt(2) == 0) {//randomly choosing which type of queue
                        queue[i]  = new ArrayTest();
                        System.out.println ("element " +i+ " is a array q");//a println so that I can see what is going on
                        }
                        
                    else {
                        this.queue[i] = new LListQueue();
                        System.out.println ("element " +i+ " is a llist q");//a println so that I can see what is going on
                           }
                
               // this.queue[i] = Q ;
              }
            }
            • 3. Re: Two implementations of one interface running from the same main method
              807599
              Sorry it is at the line
              [
               while (current.getNext() != null){
              /code]
               and is a Null Pointer Exception
              
              but since i posted I have figured out that by this stage the program thinks that there is already something in this queue so the Q_Empty() method goes wrong.
              
              If you could please look at my next post it shows the code that is causing this error.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
              • 4. Re: Two implementations of one interface running from the same main method
                807599
                It is because current contains a null value. Thats why you are getting null pointer exception.

                bye for now
                sat
                • 5. Re: Two implementations of one interface running from the same main method
                  807599
                  AARGH I've sorted myself out.

                  My problem was that I had declared the count variable in my LListQueue class as a static int so that there was only one for the whole class to reference. So once I added to a queue the count was getting incremented for all the other empty queues.

                  So I guess my memory leaks and all that stuff was just another wild goose chase.