help with sort using Calendar class getting null pointer exception

Reply

Join Date: Sep 2004
Posts: 7
Reputation: joebanks is an unknown quantity at this point 
Solved Threads: 0
joebanks joebanks is offline Offline
Newbie Poster

help with sort using Calendar class getting null pointer exception

 
0
  #1
Oct 3rd, 2004
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;
}
}
}
}
}
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 1,749
Reputation: nanosani is an unknown quantity at this point 
Solved Threads: 55
Team Colleague
nanosani's Avatar
nanosani nanosani is offline Offline
Unauthenticated Liar

Re: help with sort using Calendar class getting null pointer exception

 
0
  #2
Oct 4th, 2004
Could you please post the full code .. and please indent the code properly and have it in the [code][*/code] tags
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7
Reputation: joebanks is an unknown quantity at this point 
Solved Threads: 0
joebanks joebanks is offline Offline
Newbie Poster

Re: help with sort using Calendar class getting null pointer exception

 
0
  #3
Oct 4th, 2004
here is the full code...there are 2 class files that i am using....

  1. public class Assn1{
  2.  
  3. private CD[] myCDs;
  4. private Date release;
  5. private static int counter = 0;
  6. private static boolean go = true;
  7.  
  8. public static void main(String[] args)
  9. {
  10. while(go == true)
  11. {
  12. new Assn1();
  13. }
  14.  
  15. }
  16.  
  17. //-------------------------------------------------------------------------------
  18. // Procedure: Assn1
  19. //
  20. // Summary: main driver method for program
  21. //-------------------------------------------------------------------------------
  22. public Assn1()
  23. {
  24. //initialize data members
  25. myCDs = new CD[5]; //provide a dimension for the array
  26.  
  27. for(int i = 0; i < myCDs.length; i++)
  28. {
  29. //main menu for navigating program
  30. Object[] possibleValues = { "Make New Entry",
  31. "View Entries",
  32. "Clear Entires",
  33. "Oldest Entries First",
  34. "Newest Entries First",
  35. "Exit"};
  36.  
  37. Object selectedValue = JOptionPane.showInputDialog(null,
  38. "Please select one of the following:",
  39. "Input",
  40. JOptionPane.INFORMATION_MESSAGE,
  41. null,
  42. possibleValues,
  43. possibleValues[0]);
  44.  
  45. //user selected make a new entry
  46. if(selectedValue.equals(possibleValues[0]))
  47. {
  48. makeNew();
  49.  
  50. }
  51.  
  52. //view all entries in the CD array
  53. if(selectedValue.equals(possibleValues[1]))
  54. {
  55. viewAll();
  56. }
  57.  
  58. //clear all entries in the CD array
  59. if(selectedValue.equals(possibleValues[2]))
  60. {
  61. clearAll();
  62. }
  63.  
  64. //sort by oldest first and
  65. //show the entries in that order
  66. if(selectedValue.equals(possibleValues[3]))
  67. {
  68. JOptionPane.showMessageDialog(null,"Entries have been sorted by Oldest to Newest");
  69. sortByOldest();
  70. }
  71.  
  72. //sort by newest first and
  73. //show entriest in that order
  74. if(selectedValue.equals(possibleValues[4]))
  75. {
  76. JOptionPane.showMessageDialog(null,"Entries have been sorted by Newest to Oldest");
  77. sortByNewest();
  78. }
  79.  
  80. //user selected exit the program....
  81. //make sure they really want to exit
  82. if(selectedValue.equals(possibleValues[5]))
  83. {
  84. exitProgram();
  85. }
  86. }
  87.  
  88. }
  89.  
  90. //-------------------------------------------------------------------------------
  91. // Procedure: sortByNewest
  92. //
  93. // Summary: sorts array values newest to oldest
  94. //-------------------------------------------------------------------------------
  95. public void sortByNewest()
  96. {
  97. if(counter > 1)
  98. {
  99. CD extra = new CD();
  100.  
  101. for(int outer = 0; outer < counter - 1; outer++)
  102. {
  103. for(int inner = 0; inner < counter - 1; inner++)
  104. {
  105. if( myCDs[inner].getreleaseDate().before(myCDs[inner + 1].getreleaseDate()))
  106. {
  107. //this part i got help from robert
  108. extra = myCDs[inner];
  109. myCDs[inner] = myCDs[inner + 1];
  110. myCDs[inner + 1] = extra;
  111. }
  112. }
  113. }
  114. }
  115. }
  116.  
  117. //-------------------------------------------------------------------------------
  118. // Procedure: sortByOldest
  119. //
  120. // Summary: sorts array values oldest to newest
  121. //-------------------------------------------------------------------------------
  122. public void sortByOldest()
  123. {
  124. if(counter > 1)
  125. {
  126. CD extra = new CD();
  127.  
  128. for(int outer = 0; outer < counter - 1; outer++)
  129. {
  130. for(int inner = 0; inner < counter - 1; inner++)
  131. {
  132.  
  133. if( myCDs[inner].getreleaseDate().after(myCDs[inner + 1].getreleaseDate()))
  134. {
  135. //this part i got help from robert
  136. extra = myCDs[inner] ;
  137. myCDs[inner] = myCDs[inner + 1];
  138. myCDs[inner + 1] = extra;
  139. }
  140. }
  141. }
  142. }
  143. }
  144.  
  145. //-------------------------------------------------------------------------------
  146. // Procedure: makeNew
  147. //
  148. // Summary: creates a new entry in CD array
  149. //-------------------------------------------------------------------------------
  150. public void makeNew()
  151. {
  152. if(counter <=4)
  153. {
  154. try
  155. {
  156. //prompt user for entries
  157. String title = JOptionPane.showInputDialog("Enter name of cd");
  158. String genre = JOptionPane.showInputDialog("Enter type of music");
  159. String label = JOptionPane.showInputDialog("Enter label the artist is on");
  160. String artist = JOptionPane.showInputDialog("Enter artist name");
  161.  
  162. String day = JOptionPane.showInputDialog("Enter day of release");
  163. int d = Integer.parseInt(day);
  164. String month = JOptionPane.showInputDialog("Enter month of release");
  165. int m = Integer.parseInt(month);
  166.  
  167. String year = JOptionPane.showInputDialog("Enter year of release");
  168. int y = Integer.parseInt(year);
  169.  
  170. myCDs[counter] = new CD(artist, title, d, m, y, genre, label );
  171.  
  172. if(myCDs[counter] != null){counter++;}
  173. }
  174. catch(Exception Ex)
  175. {
  176. JOptionPane.showMessageDialog(null, "Invalid input entered");
  177. }
  178. }
  179. else JOptionPane.showMessageDialog(null, "The CD array is full, you cannot add more CD's");
  180. }
  181. //-------------------------------------------------------------------------------
  182. // Procedure: viewAll
  183. //
  184. // Summary: displays all current entries in array
  185. //-------------------------------------------------------------------------------
  186. public void viewAll()
  187. {
  188. String output = "";
  189. for(int count = 0; count < myCDs.length; count++)
  190. {
  191. if(myCDs[count] != null)
  192. output += myCDs[count].Display() + "\n";
  193. }
  194. JOptionPane.showMessageDialog(null, output, "All cds in list", JOptionPane.INFORMATION_MESSAGE);
  195. }
  196.  
  197. //-------------------------------------------------------------------------------
  198. // Procedure: clearAll
  199. //
  200. // Summary: clears all entries in the array
  201. //-------------------------------------------------------------------------------
  202. public void clearAll()
  203. {
  204. Object[] option = { "Yes", "No" };
  205.  
  206. Object decision = JOptionPane.showInputDialog(null,
  207. "Clear All Entries in CD Collection",
  208. "Input",
  209. JOptionPane.INFORMATION_MESSAGE,
  210. null,
  211. option,
  212. option[0]);
  213.  
  214. if(decision.equals(option[0]))
  215. {
  216. for(int j = 0; j < myCDs.length; j++)
  217. {
  218. if(myCDs[j] != null)
  219. {
  220. myCDs[j] = null;
  221. }
  222. }
  223. }
  224. }
  225.  
  226. //-------------------------------------------------------------------------------
  227. // Procedure: exitProgram
  228. //
  229. // Summary: provides means for user to exit program
  230. //-------------------------------------------------------------------------------
  231. public void exitProgram()
  232. {
  233. Object[] option = { "Yes",
  234. "No"
  235. };
  236.  
  237. Object decision = JOptionPane.showInputDialog(null,
  238. "Do you really want to exit?",
  239. "Input",
  240. JOptionPane.INFORMATION_MESSAGE,
  241. null,
  242. option,
  243. option[0]);
  244.  
  245. if(decision.equals(option[0]))
  246. {
  247. go = false;
  248. System.exit( 0 );
  249. }
  250. }
  251.  
  252. }//end class Assn1
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7
Reputation: joebanks is an unknown quantity at this point 
Solved Threads: 0
joebanks joebanks is offline Offline
Newbie Poster

Re: help with sort using Calendar class getting null pointer exception

 
0
  #4
Oct 4th, 2004
This is the class that holds the properties for the previous class

  1. import javax.swing.*;
  2. import java.util.*;
  3.  
  4.  
  5. public class CD
  6. {
  7.  
  8. private String artist;
  9. private String title;
  10. private Calendar releaseDate;
  11. private String label;
  12. private String genre;
  13.  
  14.  
  15. public CD(String a, String t, int d, int m, int y, String l, String g)
  16. {
  17.  
  18. setArtist(a);
  19. setTitle(t);
  20. setreleaseDate(m,d,y) ;
  21. setLabel(l);
  22. setGenre(g);
  23.  
  24. }
  25. public CD(){
  26. }
  27.  
  28. //set methods
  29. public void setTitle(String t)
  30. {
  31. title = t;
  32. }
  33. public void setreleaseDate(int m, int d, int y)
  34. {
  35. Calendar releaseDate = new GregorianCalendar(y,m,d);
  36. this.releaseDate = releaseDate;
  37.  
  38. }
  39.  
  40. public void setArtist(String a)
  41. {
  42. artist = a;
  43.  
  44. }
  45.  
  46. public void setLabel(String l)
  47. {
  48. label = l;
  49.  
  50. }
  51.  
  52. public void setGenre(String g)
  53. {
  54. genre = g;
  55.  
  56. }
  57.  
  58. //get methods
  59. public String getTitle()
  60. {
  61. return title;
  62. }
  63.  
  64. public Calendar getreleaseDate()
  65. {
  66. return releaseDate;
  67. }
  68.  
  69. public String getArtist()
  70. {
  71. return artist;
  72. }
  73.  
  74. public String getLabel()
  75. {
  76. return label;
  77. }
  78.  
  79. public String getGenre()
  80. {
  81. return genre;
  82. }
  83.  
  84. public String Display()
  85. {
  86.  
  87. String output= "";
  88. output += this.getTitle() + " " + this.getArtist() + " " + label +
  89. " " + releaseDate.get(Calendar.MONTH) + "/" + releaseDate.get(Calendar.DATE) +
  90. "/" + releaseDate.get(Calendar.YEAR) + " " + genre;
  91.  
  92. return output;
  93. }
  94.  
  95.  
  96. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 84
Reputation: jerbo is an unknown quantity at this point 
Solved Threads: 1
jerbo jerbo is offline Offline
Junior Poster in Training

Re: help with sort using Calendar class getting null pointer exception

 
0
  #5
Oct 4th, 2004
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)
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7
Reputation: joebanks is an unknown quantity at this point 
Solved Threads: 0
joebanks joebanks is offline Offline
Newbie Poster

Re: help with sort using Calendar class getting null pointer exception

 
0
  #6
Oct 5th, 2004
Thanks for your help...I have solved my problem
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC