im trying to sort through an array and i keep getting a null pointer exception when i do... can anyone see where im going wrong....thanks

getreleasedate() gets the gregorian calendar date for each object in array.
new CD() creates an empty array that can hold five objects

public void sortByNewest()
{
   if(counter > 1)
   {
      CD extra = new CD();

      for(int outer = 0; outer < counter - 1; outer++)
      {
     for(int inner = 0; inner < counter - 1; inner++)
     {
       if( myCDs[inner].getreleaseDate().after(myCDs[inner + 1].getreleaseDate()))
         {                  
           //this part i got help from robert 
         extra = myCDs[inner] ;
         myCDs[inner] = myCDs[inner + 1];
         myCDs[inner + 1] = extra;
         }
    }
      }
    }
}

Recommended Answers

All 5 Replies

Could you please post the full code .. and please indent the code properly and have it in code tags

here is the full code...there are 2 class files that i am using....

public class Assn1{
	
   private CD[] myCDs; 
   private Date release;
   private static int counter = 0;
   private static boolean go = true;
	
   public static void main(String[] args)
   {
     while(go == true)
     {
	new Assn1();		 
     }
	
   }
		
   //-------------------------------------------------------------------------------
      //   Procedure:	Assn1
      //
      //   Summary: main driver method for program 
   //-------------------------------------------------------------------------------
    public Assn1()
	{
	  //initialize data members
	  myCDs = new CD[5]; //provide a dimension for the array
		
	  for(int i = 0; i < myCDs.length; i++)
	  {
	    //main menu for navigating program 
	    Object[] possibleValues = { "Make New Entry",
	    	                            "View Entries",
		                            "Clear Entires",
		                            "Oldest Entries First",
		                            "Newest Entries First",
		                            "Exit"};
		
	    Object selectedValue = JOptionPane.showInputDialog(null,
		                                                   "Please select one of the following:",
		                                                   "Input",
		                                                   JOptionPane.INFORMATION_MESSAGE,
		                                                   null,
		                                                   possibleValues,
		                                                   possibleValues[0]);
		                                                   
	     //user selected make a new entry 
	     if(selectedValue.equals(possibleValues[0]))
	     {	
		makeNew();
		
	     }
		       
	     //view all entries in the CD array 
	     if(selectedValue.equals(possibleValues[1]))
	     {	
		viewAll();
	     }
		
	     //clear all entries in the CD array 
	     if(selectedValue.equals(possibleValues[2]))
	     {
		clearAll();
	     }
		
	     //sort by oldest first and 
	     //show the entries in that order 
	     if(selectedValue.equals(possibleValues[3]))
	     {
		JOptionPane.showMessageDialog(null,"Entries have been sorted by Oldest to Newest");
		sortByOldest();
	     }
		
	     //sort by newest first and 
	     //show entriest in that order 
             if(selectedValue.equals(possibleValues[4]))
	     {
	     	JOptionPane.showMessageDialog(null,"Entries have been sorted by Newest to Oldest");
	     	sortByNewest();
	     }
	   	
	     //user selected exit the program....
	     //make sure they really want to exit 
	     if(selectedValue.equals(possibleValues[5]))
	     {
	       	exitProgram();
	     }
	  }
		
	}
	
    //-------------------------------------------------------------------------------
      //   Procedure:	sortByNewest
      //
      //   Summary: sorts array values newest to oldest
   //-------------------------------------------------------------------------------
	public void sortByNewest()
	{
	    if(counter > 1)
	    {
	        CD extra = new CD();
		
	        for(int outer = 0; outer < counter - 1; outer++)
                     {
		for(int inner = 0; inner < counter - 1; inner++)
		{
		    if( myCDs[inner].getreleaseDate().before(myCDs[inner + 1].getreleaseDate()))
		{					
		   //this part i got help from robert 
	               extra = myCDs[inner];
		  myCDs[inner] = myCDs[inner + 1];
		  myCDs[inner + 1] = extra;
		}
	        }
	     }
	  }
	}
	
      //-------------------------------------------------------------------------------
        //   Procedure:	sortByOldest
        //
        //	 Summary: sorts array values oldest to newest
     //-------------------------------------------------------------------------------
      public void sortByOldest()
      {
          if(counter > 1)
          {
	 CD extra = new CD();
		
	for(int outer = 0; outer < counter - 1; outer++)
	{
	   for(int inner = 0; inner < counter - 1; inner++)
	   {
	      	
	       if( myCDs[inner].getreleaseDate().after(myCDs[inner + 1].getreleaseDate()))
	        {					
		//this part i got help from robert 
		extra = myCDs[inner] ;
		myCDs[inner] = myCDs[inner + 1];
		myCDs[inner + 1] = extra;
	        }
	   }
	}
          }
       }
	
    //------------------------------------------------------------------------------- 
       //   Procedure:	makeNew
       // 
       //	 Summary: creates a new entry in CD array 
     //-------------------------------------------------------------------------------
         public void makeNew()
         {
	if(counter <=4)
	{
	   try
	   {
	      //prompt user for entries 		
	     String title = JOptionPane.showInputDialog("Enter name of cd");
	     String genre = JOptionPane.showInputDialog("Enter type of music");
	     String label = JOptionPane.showInputDialog("Enter label the artist is on");
	     String artist = JOptionPane.showInputDialog("Enter artist name");
	   			
	     String day = JOptionPane.showInputDialog("Enter day of release");
	     int d = Integer.parseInt(day);
	     String month = JOptionPane.showInputDialog("Enter month of release"); 
	     int m = Integer.parseInt(month);
				
	     String year = JOptionPane.showInputDialog("Enter year of release");
	     int y = Integer.parseInt(year);
								
	    myCDs[counter] = new CD(artist, title, d, m, y, genre, label );
	  		
	    if(myCDs[counter] != null){counter++;}
	}
	catch(Exception Ex)
	{
	  JOptionPane.showMessageDialog(null, "Invalid input entered");
	}
        }
	else JOptionPane.showMessageDialog(null, "The CD array is full, you cannot add more CD's");
     }
     //-------------------------------------------------------------------------------
        //   Procedure:	viewAll
        //
        //	 Summary: displays all current entries in array 
     //-------------------------------------------------------------------------------
    public void viewAll()
    {
        String output = "";
        for(int count = 0; count < myCDs.length; count++)
        {
	if(myCDs[count] != null)
             output += myCDs[count].Display() + "\n";	 	
         }
        JOptionPane.showMessageDialog(null, output, "All cds in list", JOptionPane.INFORMATION_MESSAGE);
     }
	
   //-------------------------------------------------------------------------------
      //   Procedure:	clearAll
      //
      //	 Summary: clears all entries in the array  
   //-------------------------------------------------------------------------------
     public void clearAll()
     {
        Object[] option = { "Yes", "No" };
		
        Object decision = JOptionPane.showInputDialog(null,
                                                      "Clear All Entries in CD Collection",
                                                      "Input",
	                                        JOptionPane.INFORMATION_MESSAGE,
	                                         null,
	                                         option,
	                                         option[0]);
	                                             
       if(decision.equals(option[0]))
       {
	for(int j = 0; j < myCDs.length; j++)
	{
	    if(myCDs[j] != null)
	    {
	         myCDs[j] = null;
                 }
	}
        }
   }
	
  //-------------------------------------------------------------------------------
     //   Procedure:	exitProgram
     //
     //	 Summary: provides means for user to exit program 
  //-------------------------------------------------------------------------------
     public void exitProgram()
     {
        Object[] option = { "Yes",
	                      "No"
	                  };
		
        Object decision = JOptionPane.showInputDialog(null,
	                                            "Do you really want to exit?",
	                                             "Input",
	                                        JOptionPane.INFORMATION_MESSAGE,
	                                             null,
	                                             option,
	                                             option[0]);
		                                                   
         if(decision.equals(option[0]))
         {
	go = false;
	System.exit( 0 );
         }
    }
	
}//end class Assn1

This is the class that holds the properties for the previous class

import javax.swing.*;
import java.util.*;


public class CD
{
		
	private String artist;
	private String title;
	private Calendar releaseDate;
	private String label;
	private String genre;
	
	
	public CD(String a, String t, int d, int m, int y, String l, String g)
	{
	
		setArtist(a);
		setTitle(t);
		setreleaseDate(m,d,y) ;
		setLabel(l);
		setGenre(g);
		
	}
	public CD(){
	}
	
	//set methods 
	public void setTitle(String t)
	{
		title = t;
	}
	public void setreleaseDate(int m, int d, int y)
	{
		Calendar releaseDate = new GregorianCalendar(y,m,d);
		this.releaseDate = releaseDate;
		
	}
	
	public void setArtist(String a)
	{
		artist = a;
		
	}   
	
	public void setLabel(String l)
	{
		label = l;
		
	}
	
	public void setGenre(String g)
	{
		genre = g;
		 
	}
	
	//get methods 
	public String getTitle()
	{
	    return title;
	}
	
	public Calendar getreleaseDate()
	{
	    return releaseDate;
	}
	
	public String getArtist()
	{
	    return artist;
	}
	
	public String getLabel()
	{
	    return label;
	}
	
	public String getGenre()
	{
	    return genre;
	}
	
	public  String Display()
	{
		
		String output= "";
		output += this.getTitle() + " " + this.getArtist() + " " + label + 
		" " + releaseDate.get(Calendar.MONTH) + "/" + releaseDate.get(Calendar.DATE) + 
		"/" + releaseDate.get(Calendar.YEAR) + " " + genre;
		
		return output;
	}


}

You are creating the array in your main routine:

public Assn1()
{
  //initialize data members
  myCDs = new CD[5]; //provide a dimension for the array

           ......

By doing this the scope of the array is only visible within the main routine.

I think you need to create this as a class array, or at the very least pass the array when you wish to view or sort the array.

Also I notice in some of your loops, you get the lenght of the array, and check for NULL in the loop. The best way in a loop to prevent this is to test for (Lenght - 1) since arrays are ZERO based, the last index number will always be (length - 1)

Thanks for your help...I have solved my problem

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.