| | |
help with sort using Calendar class getting null pointer exception
![]() |
•
•
Join Date: Sep 2004
Posts: 7
Reputation:
Solved Threads: 0
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;
}
}
}
}
}
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;
}
}
}
}
}
Could you please post the full code .. and please indent the code properly and have it in the [code][*/code] tags
•
•
Join Date: Sep 2004
Posts: 7
Reputation:
Solved Threads: 0
here is the full code...there are 2 class files that i am using....
Java Syntax (Toggle Plain Text)
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
•
•
Join Date: Sep 2004
Posts: 7
Reputation:
Solved Threads: 0
This is the class that holds the properties for the previous class
Java Syntax (Toggle Plain Text)
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; } }
•
•
Join Date: Sep 2004
Posts: 84
Reputation:
Solved Threads: 1
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)
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)
![]() |
Similar Threads
- Null Pointer Exception....... (Java)
- Null pointer exception while getting other applet in the same page (Java)
- null pointer exception --- urgent (Java)
- Java Null Pointer Exception (Java)
Other Threads in the Java Forum
- Previous Thread: Problem with readline
- Next Thread: Anybody can help me make a game?
| Thread Tools | Search this Thread |
-xlint add android api applet application applications array arrays automation bank bi binary blackberry bluetooth chat class clear client code compile compiler component database development digit eclipse equation error event formatingtextintooltipjava fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide idea image infinite int integer j2me java javame javaprojects jetbrains jni jpanel jtable julia learningresources linux list main map method methods mobile myregfun netbeans nonstatic notdisplaying openjavafx pearl problem program project qt recursion repositories scanner screen scrollbar server set sms sort sorting spamblocker sql sqlserver state storm string superclass swing system thread threads tree variablebinding windows xor






