942,967 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 5687
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 17th, 2010
0

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

Expand Post »
there are no visible error in the syntax but when i run the application i keep getting the following error. i have been trying to fix it for hours and simply cant find the problem. the error that appears when running is;

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.JComboBox.setModel(JComboBox.java:292)
at Application.UserManagement.initComponents(UserManagement.java:234)
at Application.UserManagement.<init>(UserManagement.java:52)
at Application.UserManagement$7.run(UserManagement.java:448)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
BUILD SUCCESSFUL (total time: 1 second)


hopefully somone can help
Reputation Points: 10
Solved Threads: 0
Newbie Poster
parkz16 is offline Offline
6 posts
since Jan 2010
Jan 17th, 2010
0
Re: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
If you read the messages they tell you at which line you get the error and what the error is.
You are using something which is null
Sponsor
Featured Poster
Reputation Points: 1005
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,256 posts
since Dec 2007
Jan 17th, 2010
0
Re: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
yeah i am unable to locate the thing that is null, and the errors are inside the java fixed classes which i have in no way edited.

here is the syntax from my mainGUI. does it show where the NullPointer is?

java Syntax (Toggle Plain Text)
  1. package Application;
  2.  
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.PrintWriter;
  7. import java.io.File;
  8. import java.io.FileInputStream;
  9. import javax.xml.parsers.SAXParser;
  10. import javax.xml.parsers.SAXParserFactory;
  11. import org.xml.sax.helpers.DefaultHandler;
  12. import javax.swing.DefaultComboBoxModel;
  13. import javax.swing.JOptionPane;
  14. import Assingment.User;
  15. import Assingment.NormalUser;
  16. import Assingment.AdminUser;
  17. import Assingment.UserList;
  18. import Assingment.BusinessManager;
  19. import Assingment.UserHandler;
  20.  
  21. /**
  22.  *
  23.  * @author Tristan
  24.  */
  25. public class UserManagement extends javax.swing.JFrame {
  26.  
  27. private BusinessManager theBM;
  28. private UserList users;
  29. private AdminUser adminUsers;
  30. private NormalUser normalUsers;
  31. private DefaultComboBoxModel userModel;
  32. private File fileName;
  33. private String[] userNames;
  34.  
  35. /** Creates new form UserManagement */
  36. public UserManagement() {
  37. createDM();
  38. createUserCmb();
  39. initComponents();
  40.  
  41. // Create the File object to hold the file name
  42. fileName = new File("C:\\Users\\Tristan\\Desktop\\edited\\Assingment 2010-01-16 at 16.11.38", "UserData.xml");
  43.  
  44. // Create the data in a Branch object, then get the
  45. // account numbers and place them in the combo box.
  46. getData();
  47. userNames = users.getUserNames();
  48. userModel = new DefaultComboBoxModel(userNames);
  49.  
  50. // NetBeans method to initialise GUI components
  51.  
  52.  
  53. // Show the data for the first Users
  54. cmbUsers.setSelectedIndex(0);
  55. showUsersDetails();
  56. }
  57.  
  58. private void getData() {
  59. // Set up the file details and create the parser, then start it.
  60. // On completion, retrieve the accounts and display them.
  61. DefaultHandler handler = new UserHandler();
  62.  
  63. try {
  64. SAXParserFactory factory = SAXParserFactory.newInstance();
  65. SAXParser parser = factory.newSAXParser();
  66. parser.parse(fileName, handler);
  67. } catch (Exception err) {
  68. System.out.println("Exception: " + err.getMessage());
  69. err.printStackTrace();
  70. }
  71.  
  72. // On completion of the parsing process, get the branch object
  73. // created by the account handler
  74. users = ((UserHandler) handler).getUserlist();
  75. }
  76.  
  77. private void createDM() {
  78. // Creates the model through test data
  79. generateTD();
  80. }
  81.  
  82. private void createUserCmb() {
  83. String[] userNames = users.getUserNames();
  84. userModel = new DefaultComboBoxModel(userNames);
  85. }
  86.  
  87. private void generateTD() {
  88. // This method creates the data model from hard-coded test data.
  89.  
  90. NormalUser nUser;
  91. AdminUser aUser;
  92. // Create the object that controls the data model
  93.  
  94. theBM = new BusinessManager();
  95.  
  96. // allows the GUI to access the objects they contain.
  97. users = theBM.getUsers();
  98.  
  99.  
  100. aUser = new AdminUser("Tristan Parker", "taparker", "tristan@parker.com", 2);
  101. users.addUser(aUser);
  102.  
  103. nUser = new NormalUser("Caroline Williams", "cmwillaims", "caroline@williams.com", 2);
  104. users.addUser(nUser);
  105. }
  106.  
  107. // Event Handlers
  108. private void showUsersDetails() {
  109. User selected = users.getUserAt(cmbUsers.getSelectedIndex());
  110. txtEmailAddress.setText(selected.getEmail());
  111. txtUsername.setText(selected.getUserID());
  112.  
  113. if (selected instanceof AdminUser) {
  114. radAdmin.setSelected(true);
  115. } else {
  116. radNormal.setSelected(true);
  117. }
  118.  
  119. }
  120.  
  121. private void addUserDetails() {
  122. AddUserGUI createUser;
  123. User user;
  124.  
  125. user =new AdminUser();
  126. user =new NormalUser();
  127. createUser =new AddUserGUI(this, true, user);
  128. createUser.setVisible(true);
  129.  
  130. if (createUser.getCloseButton().equals("OK")) {
  131. users.addUser(user);
  132. userModel.addElement(user.getName());
  133. cmbUsers.setSelectedIndex(users.size() - 1);
  134. }
  135.  
  136. }
  137.  
  138. private void updateUser() {
  139. int selectedIndex = cmbUsers.getSelectedIndex();
  140. if (selectedIndex == -1) {
  141. JOptionPane.showMessageDialog(null,
  142. "User not selected!", "Error",
  143. JOptionPane.ERROR_MESSAGE);
  144. } else {
  145. AddUserGUI augUpdate;
  146. User selected = users.getUserAt(selectedIndex);
  147.  
  148. augUpdate =
  149. new AddUserGUI(this, true, selected);
  150. augUpdate.setVisible(true);
  151.  
  152. // On return from the dialog, rebuild model in case
  153. // the name has changed
  154. if (augUpdate.getCloseButton().equals("OK")) {
  155. userModel = new DefaultComboBoxModel(users.getUserNames());
  156. cmbUsers.setModel(userModel);
  157. cmbUsers.setSelectedIndex(selectedIndex);
  158. }
  159.  
  160. }
  161. }
  162.  
  163. private void deleteUser() {
  164. int confirmed = JOptionPane.showConfirmDialog(null,
  165. "Are you sure you want to delete this user ",
  166. "Confirm closure!", JOptionPane.YES_NO_OPTION);
  167. if (confirmed == JOptionPane.YES_OPTION) {
  168. users.removeUserAt(cmbUsers.getSelectedIndex());
  169. userModel.removeElementAt(cmbUsers.getSelectedIndex());
  170. }
  171.  
  172. }
  173.  
  174. // Save and exit
  175. private void saveAndExit() {
  176. FileOutputStream outStream;
  177. PrintWriter pw;
  178. User user;
  179.  
  180. // Create stream and write the DTD
  181. try {
  182. outStream = new FileOutputStream(fileName);
  183. pw = new PrintWriter(outStream);
  184.  
  185. // write out DTD
  186. pw.println("<?xml version = \"1.0\" standalone = \"yes\"?>");
  187. pw.println("<!DOCTYPE Users [");
  188. pw.println(" <!ELEMENT Users (aUser, nUser)+ >");
  189. pw.println(" <!ELEMENT aUser (name, userID, emailAddress) >");
  190. pw.println(" <!ELEMENT nUser (name, userID, emailAddress) >");
  191. pw.println(" <!ELEMENT name (#PCDATA) >");
  192. pw.println(" <!ELEMENT userID (#PCDATA) >");
  193. pw.println(" <!ELEMENT emailAddress (#PCDATA) >");
  194. pw.println("] >");
  195.  
  196. // Write out the data
  197. pw.println("<Users>");
  198.  
  199. // Write out the xml representation of each account object.
  200. // It doesn't matter which type of account it is - because
  201. // BankAccount has a getXML() method as well as the sub-classes
  202. // having the same signature methods, it will use whichever is
  203. // appropriate to the current object.
  204. for (int ct = 0; ct < users.size(); ct++) {
  205.  
  206. user = users.getUserAt(ct);
  207. pw.println(user.getXML());
  208. }
  209.  
  210.  
  211. pw.println("</Users>");
  212.  
  213. // Close the file
  214. pw.close();
  215. } catch (IOException err) {
  216. err.printStackTrace();
  217. }
  218.  
  219. // Finally, close the program.
  220. System.exit(0);
  221. }
Last edited by peter_budo; Jan 17th, 2010 at 8:50 am. Reason: Keep it Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
parkz16 is offline Offline
6 posts
since Jan 2010
Jan 17th, 2010
0
Re: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
Hi,

It seems that you are setting a null model for ComboBox in your initComponents() method (which is not listed in the code you've shown us).
Check your code at line 292 as shown in the error log.

... and next time please use [code] tags for your code

GL
Reputation Points: 10
Solved Threads: 9
Junior Poster in Training
AndreiDMS is offline Offline
68 posts
since Oct 2009
Jan 17th, 2010
0
Re: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
now i am getting a new set of error codes, i am unable to edit anything within the fixed java classes.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Application.UserManagement.showUsersDetails(UserManagement.java:121)
at Application.UserManagement.cmbUsersClicked(UserManagement.java:449)
at Application.UserManagement.access$000(UserManagement.java:35)
at Application.UserManagement$1.actionPerformed(UserManagement.java:268)
at javax.swing.JComboBox.fireActionEvent(JComboBox.java:1240)
at javax.swing.JComboBox.setSelectedItem(JComboBox.java:567)
at javax.swing.JComboBox.setSelectedIndex(JComboBox.java:603)
at Application.UserManagement.<init>(UserManagement.java:65)
at Application.UserManagement$7.run(UserManagement.java:479)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
parkz16 is offline Offline
6 posts
since Jan 2010
Jan 17th, 2010
0
Re: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
A NullPointerException is
Quote originally posted by java.sun.com ...
Thrown when an application attempts to use null in a case where an object is required. These include:
* Calling the instance method of a null object.
* Accessing or modifying the field of a null object.
* Taking the length of null as if it were an array.
* Accessing or modifying the slots of null as if it were an array.
* Throwing null as if it were a Throwable value.
Check your showUserDetails method for one of this cases.

Hope this helps,
GL
Reputation Points: 10
Solved Threads: 9
Junior Poster in Training
AndreiDMS is offline Offline
68 posts
since Oct 2009
Jan 17th, 2010
0
Re: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
Why are you talking about editing the "fixed Java classes"? The error is in your code. The error messages relate to a single error and show the sequence of calls (starting deep in the Swing event handler), via your faulty code, and maybe back into a Java API method before the error is detected. However, the fault will be in the few lines of your code that are listed, specifically:
at Application.UserManagement.showUsersDetails(UserManagement.java:121)
Somewhere on that line is an uninitialised variable or a method call that has returned null. If you don't know which it is, try printing each and every value on that line to System.out
Featured Poster
Reputation Points: 1887
Solved Threads: 940
Posting Expert
JamesCherrill is offline Offline
5,723 posts
since Apr 2008
Jan 18th, 2010
0
Re: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
Click to Expand / Collapse  Quote originally posted by parkz16 ...
yeah i am unable to locate the thing that is null, and the errors are inside the java fixed classes which i have in no way edited.
No you were too lazy to read the messages, as I told you. That is why you couldn't locate the error.:

Quote ...
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.JComboBox.setModel(JComboBox.java:292)
at Application.UserManagement.initComponents(UserManagement.java:234)
at Application.UserManagement.<init>(UserManagement.java:52)
at Application.UserManagement$7.run(UserManagement.java:448)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
BUILD SUCCESSFUL (total time: 1 second)
Did I have to point that out to you? When you get any error, don't you even bother to read it, or just post the error and expect others to do it for you?

If you still can't fix it, then point out at the code where that line is. Although the problem is that you are using something which is null (you haven't initialized something) as stated by the exception and other posters:
Quote ...
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
Sponsor
Featured Poster
Reputation Points: 1005
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,256 posts
since Dec 2007
Jan 18th, 2010
0
Re: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
Java Syntax (Toggle Plain Text)
  1. fileName = new File("C:\\Users\\Tristan\\Desktop\\edited\\Assingment 2010-01-16 at 16.11.38", "UserData.xml");

might be you were not given correct path for file...

check also ..

some times file will be taken but no contents in it..

so no user from the file means nullpointerexception may come..
Reputation Points: 19
Solved Threads: 45
Posting Whiz
musthafa.aj is offline Offline
312 posts
since Nov 2009
Jan 14th, 2011
-1

replace simple types in your data classes

Just replace simple types like int, long etc. by corresponding class types like Integer, Long etc. It really helps :-)
Reputation Points: 7
Solved Threads: 0
Newbie Poster
xy75 is offline Offline
1 posts
since Jan 2011

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Images are not displayed
Next Thread in Java Forum Timeline: Implementing calculator memory function





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC