Click here to Skip to main content
15,888,733 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The method below is basic insight of what I am planning to output: the account part works but when I try to print along with the transactions I get the nullpointer exception.

The reason is I am using my own queue implementation, underneath the method is my iterator so far but I have no clue what I am missing for this to not work?

         public void Display_Account() {
         System.out.println("Student ID: " + Student_ID);
         System.out.println("Student Name: " + Student_Name);
         System.out.println("Student Email: " + Student_Email);
         System.out.println("Creation Date (DD/MM/YYYY): " + Creation_Date);
         System.out.println("Credit: " + Credit_Balance);
         System.out.println("*----------------------*");

         // Code indicates not applicable to expression type and requires iterable 
         to be implemented
         for (Student_Transaction Tr : Transaction) { 
             Tr.Display_Transaction();
         }
      }

Therefore, I will need to implement the Iterator in my queue class which is the data structure I am using to add or remove objects, despite I'm missing few errors
but I dont know where. 

      // Queue uses class List.
      import java.util.Iterator;

      public class Queue <t> implements Iterable <t>
      {
       private List<t> queueList;
       private int capacity; 

       // no-argument constructor
       public Queue() 
      { 
         queueList = new List<>("queue"); 
         capacity = 1;
      } // end Queue no-argument constructor

      @Override
      public Iterator<t> iterator() {
      return null;
      }

      class QueueIt implements Iterator<t> { 

        // Where the iterator problem is in particular!
   public class QueueIt implements Iterator<t> { 

        @Override
        public boolean hasNext() {
            return false;
        }

        @Override
        public T next() {
            return null;
        }
   }

    
   // add object to queue
   public void enqueue(T object)
   { 
      queueList.insertAtBack(object); 
      capacity++;
   } // end method enqueue

   // remove object from queue
   public T dequeue() throws EmptyListException
   { 
      return queueList.removeFromFront(); 
   } // end method dequeue

   public void Decrement() 
   { 
       capacity--;
   }
   public int Size() 
   { 
       return capacity; 
   }
   
   // determine if queue is empty
   public boolean isEmpty()
   {
      return queueList.isEmpty();
   } // end method isEmpty

   // output queue contents
   public void print()
   {
      queueList.print();
   } // end method print
}

What I have tried:

Every example I have found are related to LinkedList. Dont get me wrong Queue is a member of LinkedList and the bigger picture (Collections utility and iterator). But I am not sure what I am missing out that my Code wont Iterate through objects. 

P.S If you need to see my List class just say so and I will edit the Snippet above.
Posted
Updated 5-Jan-19 0:47am
v2
Comments
Richard MacCutchan 5-Jan-19 6:50am    
Your iterator is incomplete, next() returns null and hasNext() returns false. But all this begs the question, why are you not using the classes that Java already provides?
PL8YR 5-Jan-19 6:54am    
Well as you can see I have made two versions of my program: first version uses all the imported packages from Java. This is the version above where I am not using any of the java packages, these are all implemented manually.

Therefore, as stated above I have implemented my own queue, I just need guidance to what I need to put into my next and hasnext methods, if you know of any good examples relating to queues I would be appreciated :)
Richard MacCutchan 5-Jan-19 11:33am    
Look at the documentation for those methods in standard Java. hasNext returns a boolean value that tells you whether there is an element waiting to be taken off the queue. And next returns the next element to the caller.
PL8YR 5-Jan-19 6:55am    
Don't get me wrong I could just use the import java.util.queue/linkedList/Collections/Iterator but as I said I'm using my own implementations

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900