Results 1 to 6 of 6
  1. #1
    augiechimpo is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default using selection sort on 2 string arrays

    here is my code, for a selection sort on 2 string arrays returning nothing. i have 2 string arrays loaded from a file now i need to sort them. but everytime i run this i get a null pointer exception

    public static void selectionSort(String[] array1, String[] array2)
    {


    int startScan, index, minIndex;
    String minValue1, minValue2;

    for (startScan = 0; startScan < (array1.length-1); startScan++)
    {
    minIndex = startScan;
    minValue1 = array1[startScan];
    minValue2 = array2[startScan];
    for(index = startScan + 1; index < array1.length; index++)
    {
    if (array1[index].compareTo(minValue1) < 0)

    {
    minValue1 = array1[index];
    minValue2 = array2[index];
    minIndex = index;
    }
    }
    array1[minIndex] = array1[startScan];
    array2[minIndex] = array2[startScan];
    array1[startScan] = minValue1;
    array2[startScan] = minValue2;
    }
    }
    can anyone please help me

    here is my code to call from a file and load the array then attempt to sort and print out the result of the sort

    public CommissionReport() throws IOException
    {

    Scanner keyboard = new Scanner(System.in);

    System.out.print("Enter the file name: ");
    String inputtedFileName = keyboard.nextLine();

    File fileName = new File(inputtedFileName);

    if(fileName.exists())
    {
    Scanner inputFile = new Scanner(fileName);

    while(inputFile.hasNext())
    {
    for (int index = 0; index < ARRAY_SIZE; index++)
    {
    salesId[index] = inputFile.nextLine();


    salesName[index] = inputFile.nextLine();

    inputFile.close();





    System.out.println(salesId[index]);
    System.out.println(salesName[index]);

    }

    MyArrays.selectionSort(salesId, salesName);

    }


    }
    else
    {
    System.out.println("File does not exist");
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    everytime i run this i get a null pointer exception

    A couple of things: first, when you post code, use the code tags. You put [code] at the start of your code and [/code] at the end. That way the indentation is preserved and the code remains readable.

    Secondly if you get a runtime exception like a NullPointerException it is a good idea to copy and post the entire error message. It will refer to a line number in your code and you should say which line that is.

  3. #3
    augiechimpo is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default oh sorry new to this

    Java Code:
    public static void selectionSort(String[] array1, String[] array2) 
        {
           
       
          int startScan, index, minIndex; 
          String minValue1, minValue2;
    
          for (startScan = 0; startScan < (array1.length-1); startScan++)
          {
             minIndex = startScan;
             minValue1 = array1[startScan];
             minValue2 = array2[startScan];
             for(index = startScan + 1; index < array1.length; index++)
             {
                [B][U]if (array1[index].compareTo(minValue1) < 0)[/U][/B]
                
                {
                   minValue1 = array1[index];
                   minValue2 = array2[index];
                   minIndex = index;
                }
             }
             array1[minIndex] = array1[startScan];
             array2[minIndex] = array2[startScan];
             array1[startScan] = minValue1;
             array2[startScan] = minValue2;
          }
       }
    here is the other code again

    Java Code:
    public CommissionReport() throws IOException 
        {
            
            Scanner keyboard = new Scanner(System.in);
            
            System.out.print("Enter the file name: ");
            String inputtedFileName = keyboard.nextLine();
           
            File fileName = new File(inputtedFileName);
            
           if(fileName.exists())
           {
             Scanner inputFile = new Scanner(fileName);
            
             while(inputFile.hasNext())
             {
                for (int index  = 0; index < ARRAY_SIZE; index++)
                {
                    salesId[index] = inputFile.nextLine();
                   
                
                    salesName[index] = inputFile.nextLine();
                    
                    inputFile.close();
                    
                    
                    
                    
             
                    System.out.println(salesId[index]);
                    System.out.println(salesName[index]);
                    
                }
                
                MyArrays.selectionSort(salesId, salesName);
                
             }
             
             
           }
           else
           {
             System.out.println("File does not exist");
           }
    here is the runtime error i get and the highlighted area

    java.lang.nullpointerexception : null

    the highlighted code is

    if (array1[index].compareTo(minValue1) < 0)

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    It looks like array1[index] is null. You can verify this by using System.out.println().

    Java Code:
    public static void selectionSort(String[] array1, String[] array2) 
    {
       int startScan, index, minIndex; 
       String minValue1, minValue2;
    
       for (startScan = 0; startScan < (array1.length-1); startScan++)
       {
          minIndex = startScan;
          minValue1 = array1[startScan];
          minValue2 = array2[startScan];
          for(index = startScan + 1; index < array1.length; index++)
          {
             [color=blue]if(array1[index] == null)
             {
                System.out.println("array1 is null at index=" + index);
             }[/color]
             if (array1[index].compareTo(minValue1) < 0)
                
                {
                   minValue1 = array1[index];
                   minValue2 = array2[index];
                   minIndex = index;
                }
             }
             array1[minIndex] = array1[startScan];
             array2[minIndex] = array2[startScan];
             array1[startScan] = minValue1;
             array2[startScan] = minValue2;
          }
       }

    Once you have verified that this is the source of the NullPointerException you have to go back to whereever array1 got its values and see why there is a null at that point. (You should also verify that the value of index is reasonable: ie that you really expect the array to have a nonnull value at that point.)

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    You populate the salesId array with code like this:

    Java Code:
    while(inputFile.hasNext())
    {
       for (int index  = 0; index < ARRAY_SIZE; index++)
       {
          salesId[index] = inputFile.nextLine();

    Can you see that the array may very well end up with nulls in it? If not, print it.

    Java Code:
    if(fileName.exists())
    {
       // etc
    }
    else
    {
       System.out.println("File does not exist");
    }
    [color=blue]System.out.println("The salesId array:");
    for(int i = 0; i < salesId.length;i++)
    {
        System.out.print(salesId[i] + " ");
    }
    System.out.println();[/color]
    // etc

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You close the scanner in the array filling loop. You should probably handle the close in a finally
    Last edited by sunde887; 03-27-2011 at 06:03 AM.

Similar Threads

  1. Selection Sort. please help!
    By cassato in forum New To Java
    Replies: 4
    Last Post: 03-14-2011, 11:26 PM
  2. Is this a Selection Sort?
    By Metastar in forum New To Java
    Replies: 2
    Last Post: 10-22-2010, 06:00 AM
  3. selection sort
    By mayhewj7 in forum New To Java
    Replies: 1
    Last Post: 04-29-2009, 01:40 AM
  4. Replies: 3
    Last Post: 01-26-2009, 01:20 AM
  5. Selection sort in Java
    By Java Tip in forum Algorithms
    Replies: 0
    Last Post: 04-15-2008, 08:41 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
  •