Results 1 to 7 of 7
  1. #1
    avoulou is offline Member
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default Array staying null

    Hi,
    I am trying to filter my arrays by creating a new array and copying the array1[i] within the limits in another array I created.
    I'm setting the arrayFiltered to null at the beginning and then I'm trying to populate it. However, the array stays null.
    Do you guys have any clue why is that?

    If I don't set it to null at the beginning, it works, but then my do-while loop does not work.

    Java Code:
          double lowerLimit, upperLimit;
          ArrayContructor[] arrayFiltered = new ArrayContructor[10];
          arrayFiltered = null;
          
          do
          {
          
             System.out.print("\n\nEnter a lower limit for daily hire rate:");
             lowerLimit = input.nextDouble();
             System.out.print("Enter a upper limit for daily hire rate:");
             upperLimit = input.nextDouble();
             
    
                for (int j = 0; j < array1.length; j++)
                {           
                if (array1[j] != null)
                   if (lowerLimit <= array1[j].getDailyRate() & array1[j].getDailyRate() <= upperLimit )              
                   {
                      arrayFiltered[j] = array1[j];
                   }            
                }                       
             if (arrayFiltered == null)
             {
                System.out.println("There are nothing within those limits");
             }
    
          }
          while (arrayFiltered == null);

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Array staying null

    the array stays null.
    Yes, it will have a null value until you assign it a new value.
    Why do you set the variable to null? That loses the array that it used to point to.

    When an array to hold objects is created it is filled will null values.

    my do-while loop does not work
    Change the condition of the while.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    avoulou is offline Member
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default Re: Array staying null

    I'm setting it to null so that my do-while loop works.

    I have two different constructors for the array:
    public ArrayConstructor (String , String, int, double, char, String , String)
    public ArrayConstructor (String, String, int, double)

    If I don't set the variable to null, how can I do the while-loop with its default value?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Array staying null

    When do you want to exit the while loop? Why keep looping?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    avoulou is offline Member
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default Re: Array staying null

    It keeps looping if nothing is found between lowerLimit and upperLimit

    So I want it to loop if there is nothing in arrayFiltered

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Array staying null

    Use a boolean variable: nothingFound initially true
    set it false when something is found
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    avoulou is offline Member
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default Re: Array staying null

    Haaaaaaa that's a good idea :)

    I'll try it,
    Thanks mate!

Similar Threads

  1. Staying motivated
    By Durden in forum New To Java
    Replies: 7
    Last Post: 05-08-2011, 11:29 AM
  2. Null on 2D Array
    By Trooper88 in forum New To Java
    Replies: 2
    Last Post: 03-22-2011, 07:29 PM
  3. Array null values
    By blug in forum New To Java
    Replies: 2
    Last Post: 03-20-2011, 06:48 AM
  4. Replies: 6
    Last Post: 10-18-2010, 10:40 AM
  5. help array set null or delete
    By chkm8 in forum New To Java
    Replies: 1
    Last Post: 01-19-2009, 08:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •