Null Pointer Exception problem

Reply

Join Date: Feb 2008
Posts: 5
Reputation: Chris Ichikawa is an unknown quantity at this point 
Solved Threads: 0
Chris Ichikawa's Avatar
Chris Ichikawa Chris Ichikawa is offline Offline
Newbie Poster

Null Pointer Exception problem

 
0
  #1
Aug 2nd, 2008
Hello, everyone.

I've been working on an application that will be able to accept customers and bills of an electrical service company. While testing this application, I've come across an error message saying Null Pointer Exception.

Okay, here are the details. The application is actually an entire GUI project. It has 3 classes, 3 frames and a data driver file (which handles all events). Now, I've told the program to accept an array of objects of class Subscriber. The program will ask the user to specify an initial number of customers on hand.

However, the problem is doing anything with that array of objects gets me nowhere. For instance, I've made a bunch of codes for moving from record to record and another bunch for saving input in the Subscriber array. With this, I was expecting the program to scroll back and forth through the records and be able to save changes and entries with these functions, but all I get is no response and a Null Pointer Exception message.

Here's the data driver file. The developer tool has shown me exactly where the error is - I'll point it out in full-caps comments. EDIT: They are lines 90 and 125.

  1. /**
  2.  * @(#)frmMain.java
  3.  *
  4.  *
  5.  * @author
  6.  * @version 1.00 2008/8/2
  7.  */
  8.  
  9.  
  10. import java.awt.*;
  11. import java.awt.event.*;
  12. import javax.swing.*;
  13.  
  14. public class frmMain implements ActionListener
  15. {
  16. frmSubscriber SubscriberFrame;
  17. frmBill BillFrame;
  18. frmMenu MenuFrame;
  19.  
  20. Subscriber s[];
  21. int n = 0, order = 0;
  22.  
  23. public frmMain()
  24. {
  25. SubscriberFrame = new frmSubscriber(this);
  26. BillFrame = new frmBill(this);
  27. MenuFrame = new frmMenu(this);
  28.  
  29. MenuFrame.open();
  30. }
  31.  
  32. public void actionPerformed (ActionEvent onClick)
  33. {
  34. if (onClick.getSource() == MenuFrame.cmdSubscribe)
  35. {
  36. if (n == 0)
  37. {
  38. do
  39. {
  40. n = Integer.parseInt(JOptionPane.showInputDialog("The system needs at least 1 account in the database.\nCurrently, this is not the case.\n\nIn order to continue, please specify the number of accounts\nyou would like to assign to the database."));
  41.  
  42. if (n <= 0)
  43. {
  44. JOptionPane.showMessageDialog(null, "Invalid entry! Use a value more than 0.");
  45. }
  46. }
  47. while (n <= 0);
  48.  
  49. Subscriber s[] = new Subscriber[n];
  50.  
  51. for (int i = 0; i < n; i++)
  52. {
  53. s[i] = new Subscriber();
  54.  
  55. s[i].setName("");
  56. s[i].setAge(0);
  57. s[i].setAddress("");
  58. s[i].setGender(false);
  59. s[i].setAcct(0);
  60. s[i].setBalance(0);
  61. s[i].setServicestatus(false);
  62. }
  63.  
  64. // view the first record
  65. order = 0;
  66.  
  67. // disable button "Previous" because this is already the first record
  68. SubscriberFrame.cmdPrev.setEnabled(false);
  69.  
  70. SubscriberFrame.txtX.setText("" + (order + 1));
  71. SubscriberFrame.txtY.setText("" + n);
  72. }
  73.  
  74. SubscriberFrame.open();
  75. MenuFrame.close();
  76. }
  77. else if (onClick.getSource() == SubscriberFrame.cmdSave)
  78. {
  79. String name, address;
  80. int acct, age;
  81. double balance;
  82.  
  83. name = SubscriberFrame.txtName.getText();
  84. address = SubscriberFrame.txtAge.getText();
  85. age = Integer.parseInt(SubscriberFrame.txtAge.getText());
  86. acct = Integer.parseInt(SubscriberFrame.txtAcct.getText());
  87. balance = Double.parseDouble(SubscriberFrame.txtAcct.getText());
  88.  
  89. // NULL POINTER EXCEPTION FOUND HERE
  90. s[order].setName(name);
  91. s[order].setAddress(address);
  92. s[order].setAge(age);
  93. s[order].setAcct(acct);
  94. s[order].setBalance(balance);
  95. }
  96. /*else if (onClick.getSource() == SubscriberFrame.cmdNew)
  97. {
  98. int placeholder;
  99.  
  100. placeholder = n;
  101.  
  102. n++;
  103.  
  104. for (int i = placeholder; i < n; i++)
  105. {
  106. s[i] = new Subscriber();
  107. }
  108.  
  109. SubscriberFrame.txtY.setEnabled(true);
  110. SubscriberFrame.txtY.setText("" + n);
  111. SubscriberFrame.txtY.setEnabled(false);
  112. }*/
  113. else if (onClick.getSource() == SubscriberFrame.cmdNext && order < n)
  114. {
  115. // increment order so that the program will go to the next account
  116. ++order;
  117.  
  118. String name;
  119. String address;
  120. int age;
  121. int acct;
  122. double balance;
  123.  
  124. // NULL POINTER EXCEPTION FOUND HERE
  125. name = s[order].getName();
  126. address = s[order].getAddress();
  127. age = s[order].getAge();
  128. acct = s[order].getAcct();
  129. balance = s[order].getBalance();
  130.  
  131. // display the next account's contents
  132. SubscriberFrame.txtName.setText(name);
  133. SubscriberFrame.txtAddress.setText(address);
  134. SubscriberFrame.txtAge.setText("" + age);
  135. SubscriberFrame.txtAcct.setText("" + acct);
  136. SubscriberFrame.txtBalance.setText("" + balance);
  137.  
  138. // show the record number
  139. SubscriberFrame.txtX.setText("" + (order + 1));
  140. SubscriberFrame.cmdPrev.setEnabled(true);
  141.  
  142. // disable button "Next" when at the last record
  143. if (order == n - 1)
  144. {
  145. SubscriberFrame.cmdNext.setEnabled(false);
  146. }
  147. }
  148. else if (onClick.getSource() == SubscriberFrame.cmdPrev && order >= 0)
  149. {
  150. // decrement order so that the program will go the next account
  151. --order;
  152.  
  153. String name;
  154. String address;
  155. int age;
  156. int acct;
  157. double balance;
  158.  
  159. name = s[order].getName();
  160. address = s[order].getAddress();
  161. age = s[order].getAge();
  162. acct = s[order].getAcct();
  163. balance = s[order].getBalance();
  164.  
  165. // display the previous account's contents
  166. SubscriberFrame.txtName.setText(name);
  167. SubscriberFrame.txtAddress.setText(address);
  168. SubscriberFrame.txtAge.setText("" + age);
  169. SubscriberFrame.txtAcct.setText("" + acct);
  170. SubscriberFrame.txtBalance.setText("" + balance);
  171.  
  172. // show the record number
  173. SubscriberFrame.txtX.setText("" + (order + 1));
  174. SubscriberFrame.cmdNext.setEnabled(true);
  175.  
  176. // disable button "Previous" when at the first record
  177. if (order == 0)
  178. {
  179. SubscriberFrame.cmdPrev.setEnabled(false);
  180. }
  181. }
  182. else if (onClick.getSource() == MenuFrame.cmdExit || onClick.getSource() == SubscriberFrame.cmdExit)
  183. {
  184. System.exit(0);
  185. }
  186. else if (onClick.getSource() == SubscriberFrame.cmdReturn)
  187. {
  188. MenuFrame.open();
  189. SubscriberFrame.close();
  190. }
  191.  
  192. }
  193.  
  194. public static void main(String args[])
  195. {
  196. frmMain mf = new frmMain();
  197. }
  198. }

EDIT
Things I've tried that failed:
1. Sending Subscriber into cmdSave and cmdNext instruction block because I assumed Subcriber objects are only recognized in cmdSubscribe's instruction block.

It ended up with a syntax error of Variable s might not have been initialized. Subscriber 's' has already been specified as a class attribute, so, clearly, this is not a solution.

2. Puting
[inline]if (onClick.getSource() == cmdSave)[/inline]
and
[inline]if (onClick.getSource() == cmdNext)"[/inline] instruction blocks under
[inline]if (onClick.getSource() == cmdSubscribe)[/inline] because object array Subscriber is first assigned in that block.

It ended up with no run-time errors, but no effect, either.



I use JCreator Lite to do this program. Please do tell me if you need the other files as well.

Also, this is my first time looking through the forum for real - everything here is quite new to me, so I hope you can pardon my ignorance.

Help is very much appreciated.
Last edited by Chris Ichikawa; Aug 2nd, 2008 at 8:48 am. Reason: Pointing out lines with errors
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Null Pointer Exception problem

 
0
  #2
Aug 2nd, 2008
The only place in which s[] is initialized is within the if (onClick.getSource() == MenuFrame.cmdSubscribe) block, and it eclipses the class level variable with a local declaration. None of the other blocks initialize s[]. That array must be initialized prior to use, whether it be in the action listener blocks or another method, and glancing at those blocks in the listener, I'd say you need to change the declaration on line 49 to an initialization of the class level s[] variable like so
  1. s[] = new Subscriber[n];
The warning that it might not be initialized comes from the fact that s[] will only be assigned a value in a conditional block and the compiler sees that it won't have a value if any of the other conditional blocks execute before it.

That can be resolved by initializing s[] to a particular value when you declare it, such as
  1. Subscriber s[] = null;
  2. // or
  3. Subscriber s[] = new Subscriber[0];
Initializing to a size of 0 is usually a better way to go, since any loop operations on the array will simply not execute under the standard looping idiom of
  1. for (int i=0; i<s.length; i++){}
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Null Pointer Exception problem

 
0
  #3
Aug 2nd, 2008
  1.  
  2. public class frmMain implements ActionListener
  3. {
  4. frmSubscriber SubscriberFrame;
  5. frmBill BillFrame;
  6. frmMenu MenuFrame;
  7.  
  8. Subscriber s[];
  9. int n = 0, order = 0;
  10.  
  11. public frmMain()
  12. {
  13. SubscriberFrame = new frmSubscriber(this);
  14. BillFrame = new frmBill(this);
  15. MenuFrame = new frmMenu(this);
  16.  
  17. MenuFrame.open();
  18. }
  19.  
  20. public void actionPerformed (ActionEvent onClick)
  21. {
  22. if (onClick.getSource() == MenuFrame.cmdSubscribe)
  23. {
  24. if (n == 0)
  25. {
  26. do
  27. {
  28. n = Integer.parseInt(JOptionPane.showInputDialog("The system needs at least 1 account in the database.\nCurrently, this is not the case.\n\nIn order to continue, please specify the number of accounts\nyou would like to assign to the database."));
  29.  
  30. if (n <= 0)
  31. {
  32. JOptionPane.showMessageDialog(null, "Invalid entry! Use a value more than 0.");
  33. }
  34. }
  35. while (n <= 0);
  36.  
  37. // Subscriber s[] = new Subscriber[n]; // localized Subscriber, Ezzaral pointed this out
  38. s = new Subscriber[n]; // now uses globally scoped Subscriber
  39.  
  40. for (int i = 0; i < n; i++)
  41. {
  42. s[i] = new Subscriber();
  43.  
  44. s[i].setName("");
  45. s[i].setAge(0);
  46. s[i].setAddress("");
  47. s[i].setGender(false);
  48. s[i].setAcct(0);
  49. s[i].setBalance(0);
  50. s[i].setServicestatus(false);
  51. }
  52.  
  53. // view the first record
  54. order = 0;
  55.  
  56. // disable button "Previous" because this is already the first record
  57. SubscriberFrame.cmdPrev.setEnabled(false);
  58.  
  59. SubscriberFrame.txtX.setText("" + (order + 1));
  60. SubscriberFrame.txtY.setText("" + n);
  61. }
  62.  
  63. SubscriberFrame.open();
  64. MenuFrame.close();
  65. }
  66. else if (onClick.getSource() == SubscriberFrame.cmdSave)
  67. {
  68. String name, address;
  69. int acct, age;
  70. double balance;
  71.  
  72. name = SubscriberFrame.txtName.getText();
  73. address = SubscriberFrame.txtAge.getText();
  74. age = Integer.parseInt(SubscriberFrame.txtAge.getText());
  75. acct = Integer.parseInt(SubscriberFrame.txtAcct.getText());
  76. balance = Double.parseDouble(SubscriberFrame.txtAcct.getText());
  77.  
  78. // NULL POINTER EXCEPTION FOUND HERE
  79. s[order].setName(name);
  80. s[order].setAddress(address);
  81. s[order].setAge(age);
  82. s[order].setAcct(acct);
  83. s[order].setBalance(balance);
  84. }

I'm not sure of the amount of times you want s to be reinitialized - you'll have to set a flag if you only want this action done once.
Last edited by Alex Edwards; Aug 2nd, 2008 at 2:15 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 5
Reputation: Chris Ichikawa is an unknown quantity at this point 
Solved Threads: 0
Chris Ichikawa's Avatar
Chris Ichikawa Chris Ichikawa is offline Offline
Newbie Poster

Re: Null Pointer Exception problem

 
0
  #4
Aug 2nd, 2008
Originally Posted by Ezzaral View Post
I'd say you need to change the declaration on line 49 to an initialization of the class level s[] variable like so
  1. s[] = new Subscriber[n];
Nuh-uh. JCreator Lite doesn't recognize it as a statement. I tried
  1. s = new Subscriber[n];
that is losing the double brackets and it worked.

Thanks for trying, anyway.

On a side note: hours ago, I've just found a potential to use ArrayList to make the array of Subscribers more dynamic in such a way I can add and remove array elements.

But I've not been taught ArrayList in programming classes yet - I could get all the syntaxes (and worse, logic) wrong, so I have to learn by trial and error. Sadly, time isn't something I have right now.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Null Pointer Exception problem

 
0
  #5
Aug 3rd, 2008
Originally Posted by Chris Ichikawa View Post
Nuh-uh. JCreator Lite doesn't recognize it as a statement. I tried
  1. s = new Subscriber[n];
that is losing the double brackets and it worked.
Yes, copy-paste oversight on my part, but you got the point I suppose. Thanks for sharing that enlightenment.
Originally Posted by Chris Ichikawa View Post
Thanks for trying, anyway.

You bet. I'll not waste any more of your time "trying", since you're short on that.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 5
Reputation: Chris Ichikawa is an unknown quantity at this point 
Solved Threads: 0
Chris Ichikawa's Avatar
Chris Ichikawa Chris Ichikawa is offline Offline
Newbie Poster

Re: Null Pointer Exception problem

 
0
  #6
Aug 3rd, 2008
To: Ezzaral

You're welcome.
Last edited by Chris Ichikawa; Aug 3rd, 2008 at 12:45 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 5
Reputation: Chris Ichikawa is an unknown quantity at this point 
Solved Threads: 0
Chris Ichikawa's Avatar
Chris Ichikawa Chris Ichikawa is offline Offline
Newbie Poster

Re: Null Pointer Exception problem

 
0
  #7
Aug 3rd, 2008
To: Alex Edwards

Yup. I knew I should've lost the "Subscriber" in Subscriber s.

I found my way out.

Almost forgot: thanks for pointing out the error.
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