Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Passed array value is null

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Passed array value is null

    2nd part of a homework project which we have strict instructions. In the first part, I have another class which builds an Array of Country objects, which I use to create a Stack. Then the Stack values are supposed to be inserted into the priority Que and sorted alphebetically. Either my stack is not passing the objects correctly, or for whatever reason the priority Que is not receiving them correctly. When I print the array in pQue1 and pQue 2 it returns

    null

    thanks for any help!

    I am very new to java and programming, but from past experiences I feel like the problem lies somewhere in creating the arrays, or somehow I have programmed the methods to pass the array to a completely different instance of priorityQueue??

    import java.util.Arrays;
     
    public class Stack {
    private int maxSize;        // size of stack array
    private Countries[] stackArray;  //  recall:  ‘instance variables’
    private int top;            // top of stack
    private PriorityQueue pQue1; 
    private PriorityQueue pQue2; 
     
    public Stack(int s)         
      {                
      maxSize = s;             
      stackArray = new Countries[maxSize];           
      top = -1; 
      pQue1 = new PriorityQueue(maxSize);
      pQue2 = new PriorityQueue(maxSize);
     
      } // end constructor
     
      public void push(Countries j)  
      {
      // put item on top of stack
      stackArray[++top] = j;
      System.out.println(stackArray[top]);     
     
      if (stackArray[top].getRegionNumber() == 1 )
          buildPQue1(stackArray[top]);
          pQue1.insert(stackArray[top]);
     
      if  (stackArray[top].getRegionNumber() == 2 )
          buildPQue2(stackArray[top]);
      } // end push
     
      public void buildPQue1(Countries k)
      {         
          pQue1.insert(k);         
      } // end build PQue1()
     
      public void buildPQue2(Countries i)
      {
         pQue1.insert(i);         
      } // end build P Que 2
      // end push()
     
     
     
     
    public class PriorityQueue extends Queue
    {    // array in sorted order, from max at 0 to min at nitems-1
    private int maxSize;
    private Countries[] queArray;
    private Countries[] item;
    private int nItems;
     
     
      public PriorityQueue(int s)    
      {      // constructor             
      super(s);
      maxSize = s;
      queArray = new Countries[maxSize];
      item = new Countries[maxSize];
      }//end constructor
     
      @Override
      public void insert(Countries item)  {     
      int j;                
      if (nItems==0)                           
      {
          queArray[nItems++] = item; 
         // System.out.println(queArray[nItems]);
      }// end if
      else                                         
      {                     
         for (j=nItems-1; j>=0; j--)    
         {
             if (item.getName().compareTo(queArray[j].getName()) < 0 )                                
               queArray[j+1] = queArray[j];                   
            else                                                      
               break;                                            
         } // end for
         if ( j < maxSize)
         {
         queArray[j+1] = item;                                           
         } // end if
           // System.out.print(queArray[j]);
         nItems++;                                                         
     
    }  // end else (nItems > 0)          
    }  // end insert()
     
    public Countries peekMin()            // peek at minimum item
    { 
    return queArray[nItems-1]; 
    } // end peek min
     
     
    public void displayQue()
    {
    System.out.println(queArray[nItems]);
    } // end display que
     
    }  // end class PriorityQ


  2. #2
    Junior Member
    Join Date
    May 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Null values in my Array, not sure why

    2nd part of a homework project which we have strict instructions. In the first part, I have another class which builds an Array of Country objects, which I use to create a Stack. Then the Stack values are supposed to be inserted into the priority Que and sorted alphebetically. Either my stack is not passing the objects correctly, or for whatever reason the priority Que is not receiving them correctly. When I print the array in pQue1 and pQue 2 it returns

    null

    thanks for any help!

    I am very new to java and programming, but from past experiences I feel like the problem lies somewhere in creating the arrays, or somehow I have programmed the methods to pass the array to a completely different instance of priorityQueue??

    i/*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    /**
     *
     * @author Jason
     */
        import java.util.Arrays;
     
    public class Stack {
       private int maxSize;        // size of stack array
       private Countries[] stackArray;  //  recall:  ‘instance variables’
       private int top;            // top of stack
       private PriorityQueue pQue1; 
       private PriorityQueue pQue2; 
     
       public Stack(int s)         
          {			       
          maxSize = s;             
          stackArray = new Countries[maxSize];           
          top = -1; 
     
     
     
          } // end constructor
     
          public void push(Countries j)  
          {
          // put item on top of stack
          stackArray[++top] = j;
         // System.out.println(stackArray[top]);     
     
          pQue1 = new PriorityQueue(maxSize);
          pQue2 = new PriorityQueue(maxSize);
     
          if (stackArray[top].getRegionNumber() == 1 )
              buildPQue1(stackArray[top]);
     
          if  (stackArray[top].getRegionNumber() == 2 )
              buildPQue2(stackArray[top]);
          } // end push
     
          public void buildPQue1(Countries k)
          {         
              pQue1.insert(k);         
          } // end build PQue1()
     
          public void buildPQue2(Countries i)
          {
             pQue1.insert(i);         
          } // end build P Que 2
          // end push()
     
          public Countries pop() 
          {// take item from top of stack
          return stackArray[top--];  
          }// end pop ()
     
          public Countries peek()  
          {       
          return stackArray[top];				
          }  // end peek()
     
          public boolean isEmpty()  
          {  
          return (top == -1);				
          } // end is Empty()
     
          public boolean isFull()   
          {  // true if stack is full        
          return (top == maxSize-1);		
          }  // end is Full()
     
     
     
    }//end class Stack
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    /**
     *
     * @author Jason
     */
    public class PriorityQueue { // array in sorted order, from max at 0 to min at nitems-1
       private int maxSize;
       private Countries[] queArray;
       private Countries[] item;
       private int nItems;
       private Stack stack;
     
     
       public PriorityQueue(int s)    
       {      // constructor             
          stack = new Stack(s);
          maxSize = s;
          queArray = new Countries[maxSize];
          item = new Countries[maxSize];
       }//end constructor
     
     
       public void insert(Countries item)  {     
          int j;				
          if (nItems==0)                           
          {
              queArray[nItems++] = item; 
             // System.out.println(queArray[nItems]);
          }// end if
          else                                         
          {				        
             for (j=nItems-1; j>=0; j--)    
             {
                 if (item.getName().compareTo(queArray[j].getName()) < 0 )                     	          
                   queArray[j+1] = queArray[j];        	          
                else                                     		          
                   break;                              		         
             } // end for
             if ( j < maxSize)
             {
             queArray[j+1] = item;                                           
             } // end if
               // System.out.print(queArray[j]);
             nItems++;                                                         
     
        }  // end else (nItems > 0)	         
        }  // end insert()
     
    public Countries peekMin()            // peek at minimum item
        { 
        return queArray[nItems-1]; 
        } // end peek min
     
     
    public void displayQue()
        {
     
      for ( int i = 0; i < nItems; ++i ) {
         System.out.println( queArray[i] );
      }
        } // end display que
     
    }  // end class PriorityQ`

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Passed array value is null

    Please do not double post your question. If you have something to add or clarify, add it to your existing thread on the topic.
    Threads merged

Similar Threads

  1. Array with null (same for Arraylist)
    By Alex L in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 4th, 2011, 11:51 PM
  2. Array list returns null
    By Scotty in forum Collections and Generics
    Replies: 3
    Last Post: April 14th, 2011, 06:50 AM
  3. [SOLVED] How to Put Spaces in the Output of Passed Arguments?
    By EmSaint in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 25th, 2010, 03:04 PM
  4. How do you set an object in array to null value?
    By Arius in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 25th, 2010, 02:50 AM
  5. [SOLVED] loop , passed and failed
    By chronoz13 in forum Loops & Control Statements
    Replies: 5
    Last Post: August 25th, 2009, 07:00 AM