2 Replies - 269 Views - Last Post: 08 February 2012 - 01:39 AM Rate Topic: -----

#1 aj_lavaca  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 55
  • Joined: 18-February 11

Sorting string array, nullpointerexception error

Posted 07 February 2012 - 12:53 AM

I keep getting a NullPointerException error on line 98 of my program, in the sortArray method.
What I am trying to do is to sort my array so I can eventually output it to a print file, but I can't get it to sort. I can't find the error in my code, but I've been trying different methods to sort a string for about 2 hours. I can't use a predefined method for sorting (such as .sort from the array class), but I can't figure out why I keep getting this error. I know it's because I'm pointing somewhere I shouldn't, but I can't figure out why it's pointing there. I would be greatful for any help.
/*
	Author:   
	Date:      
	Assignment: 
*/
import java.io.*;
import java.lang.String;

public class Ajm01soccer
{
  public static void main (String args[]) throws IOException
  {
    FileReader      indisk    = new FileReader("girls40.txt");
    BufferedReader  diskin    = new BufferedReader(indisk);
    FileWriter      outdisk   = new FileWriter("Ajm01soccer.txt");
    PrintWriter     diskprint = new PrintWriter(outdisk);
    
    String  girl1   = "";
    String  girl2   = "";
    String  girl3   = "";
    String  girl4   = "";
    int     radnum  = 0;
    int     slot    = 0;
    String  date    = SoccerDateUtility.getTimeStamp();
    String  team1[] = new String [20];
    String  team2[] = new String [20];
    String  team3[] = new String [20];
    String  team4[] = new String [20];
    
    
    diskprint.println("00000000011111111112222222222333333333344444444");
		diskprint.println("12345678901234567890123456789012345678901234567");
    diskprint.println("===============================================");
    diskprint.println("Proj 1 Anthony Michael    " + date);
    diskprint.println("===============================================");
    diskprint.println("       Lake Wobegon Girl's Soccer League       ");
    diskprint.println("");
    diskprint.println("No.   Team 1     Team 2     Team 3     Team 4  ");
    diskprint.println("--- ---------- ---------- ---------- ----------");
    
    do
    {
      girl1 = diskin.readLine();
      girl2 = diskin.readLine();
      girl3 = diskin.readLine();
      girl4 = diskin.readLine();
      radnum  = 1 + (int)(Math.random() * 4);
      
      if(radnum == 1)
      {
        team1[slot] = girl1;
        team2[slot] = girl2;
        team3[slot] = girl3;
        team4[slot] = girl4;
      }
      
      if(radnum == 2)
      {
        team1[slot] = girl4;
        team2[slot] = girl1;
        team3[slot] = girl2;
        team4[slot] = girl3;
      }
      
       if(radnum == 3)
      {
        team1[slot] = girl3;
        team2[slot] = girl4;
        team3[slot] = girl1;
        team4[slot] = girl2;
      }
      
       if(radnum == 4)
      {
        team1[slot] = girl2;
        team2[slot] = girl3;
        team3[slot] = girl4;
        team4[slot] = girl1;
      }
        slot++;
     }while (diskin.ready() != false);
    
    
    
    team1 = sortArray(team1);
    
    outdisk.close();
    
  }
  
  public static String[] sortArray (String[] oldArray)
  {
    for (int i = 1; i < oldArray.length; i++)
    {
      int s = i-1;
      for (int j = i; j < oldArray.length; j++)
      {
        if(oldArray[j].compareTo(oldArray[s]) < 0)
        {
          s = j;
        }
      }
      
      String temp = oldArray[i-1];
      oldArray[i-1] = oldArray[s];
      oldArray[s] = temp;
      
    }
    return oldArray;
  }
  
  
}


This post has been edited by aj_lavaca: 07 February 2012 - 12:53 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Sorting string array, nullpointerexception error

#2 ianian112  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 119
  • View blog
  • Posts: 378
  • Joined: 28-November 09

Re: Sorting string array, nullpointerexception error

Posted 07 February 2012 - 01:26 AM

Your array is size 20, but you only have 4 elements in it. This will cause your sort to try to call compareTo on a null element. You can add
 if(oldArray[j] != null && oldArray[s] != null && oldArray[j].compareTo(oldArray[s]) < 0)
{
}



To make sure they are not null before calling the method. Other options are to add the != null part in the for loop. such as
for (int j = i; j < oldArray.length && oldArray[j] != null ; j++)


This post has been edited by ianian112: 07 February 2012 - 01:27 AM

Was This Post Helpful? 1
  • +
  • -

#3 aj_lavaca  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 55
  • Joined: 18-February 11

Re: Sorting string array, nullpointerexception error

Posted 08 February 2012 - 01:39 AM

I see what your saying. I incorperated it into my program and it worked perfectly. I've been having trouble with the .compareTo() and you've cleared it up for me. Thanks for your help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1