954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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

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.(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

parkz16
Newbie Poster
6 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

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

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

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?

package Application;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.File;
import java.io.FileInputStream;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.helpers.DefaultHandler;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import Assingment.User;
import Assingment.NormalUser;
import Assingment.AdminUser;
import Assingment.UserList;
import Assingment.BusinessManager;
import Assingment.UserHandler;

/**
 *
 * @author Tristan
 */
public class UserManagement extends javax.swing.JFrame {

    private BusinessManager theBM;
    private UserList users;
    private AdminUser adminUsers;
    private NormalUser normalUsers;
    private DefaultComboBoxModel userModel;
    private File fileName;
    private String[] userNames;

    /** Creates new form UserManagement */
    public UserManagement() {
        createDM();
        createUserCmb();
        initComponents();

        // Create the File object to hold the file name
        fileName = new File("C:\\Users\\Tristan\\Desktop\\edited\\Assingment 2010-01-16 at 16.11.38", "UserData.xml");

        // Create the data in a Branch object, then get the
        // account numbers and place them in the combo box.
        getData();
        userNames = users.getUserNames();
        userModel = new DefaultComboBoxModel(userNames);

        // NetBeans method to initialise GUI components


        // Show the data for the first Users
        cmbUsers.setSelectedIndex(0);
        showUsersDetails();
    }

    private void getData() {
        // Set up the file details and create the parser, then start it.
        // On completion, retrieve the accounts and display them.
        DefaultHandler handler = new UserHandler();

        try {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            parser.parse(fileName, handler);
        } catch (Exception err) {
            System.out.println("Exception: " + err.getMessage());
            err.printStackTrace();
        }

        // On completion of the parsing process, get the branch object
        // created by the account handler
        users = ((UserHandler) handler).getUserlist();
    }

    private void createDM() {
// Creates the model through test data
        generateTD();
    }

    private void createUserCmb() {
        String[] userNames = users.getUserNames();
        userModel = new DefaultComboBoxModel(userNames);
    }

    private void generateTD() {
        // This method creates the data model from hard-coded test data.

        NormalUser nUser;
        AdminUser aUser;
// Create the object that controls the data model

        theBM = new BusinessManager();

        // allows the GUI to access the objects they contain.
        users = theBM.getUsers();


        aUser = new AdminUser("Tristan Parker", "taparker", "tristan@parker.com", 2);
        users.addUser(aUser);

        nUser = new NormalUser("Caroline Williams", "cmwillaims", "caroline@williams.com", 2);
        users.addUser(nUser);
    }

// Event Handlers
    private void showUsersDetails() {
        User selected = users.getUserAt(cmbUsers.getSelectedIndex());
        txtEmailAddress.setText(selected.getEmail());
        txtUsername.setText(selected.getUserID());

        if (selected instanceof AdminUser) {
            radAdmin.setSelected(true);
        } else {
            radNormal.setSelected(true);
        }

    }

    private void addUserDetails() {
        AddUserGUI createUser;
        User user;

        user =new AdminUser();
        user =new NormalUser();
        createUser =new AddUserGUI(this, true, user);
        createUser.setVisible(true);

        if (createUser.getCloseButton().equals("OK")) {
            users.addUser(user);
            userModel.addElement(user.getName());
            cmbUsers.setSelectedIndex(users.size() - 1);
        }

    }

    private void updateUser() {
        int selectedIndex = cmbUsers.getSelectedIndex();
        if (selectedIndex == -1) {
            JOptionPane.showMessageDialog(null,
                    "User not selected!", "Error",
                    JOptionPane.ERROR_MESSAGE);
        } else {
            AddUserGUI augUpdate;
            User selected = users.getUserAt(selectedIndex);

            augUpdate =
                    new AddUserGUI(this, true, selected);
            augUpdate.setVisible(true);

            // On return from the dialog, rebuild model in case
            // the name has changed
            if (augUpdate.getCloseButton().equals("OK")) {
                userModel = new DefaultComboBoxModel(users.getUserNames());
                cmbUsers.setModel(userModel);
                cmbUsers.setSelectedIndex(selectedIndex);
            }

        }
    }

    private void deleteUser() {
        int confirmed = JOptionPane.showConfirmDialog(null,
                "Are you sure you want to delete this user ",
                "Confirm closure!", JOptionPane.YES_NO_OPTION);
        if (confirmed == JOptionPane.YES_OPTION) {
            users.removeUserAt(cmbUsers.getSelectedIndex());
            userModel.removeElementAt(cmbUsers.getSelectedIndex());
        }

    }

    // Save and exit
    private void saveAndExit() {
        FileOutputStream outStream;
        PrintWriter pw;
        User user;

        // Create stream and write the DTD
        try {
            outStream = new FileOutputStream(fileName);
            pw = new PrintWriter(outStream);

            // write out DTD
            pw.println("<?xml version = \"1.0\" standalone = \"yes\"?>");
            pw.println("<!DOCTYPE Users [");
            pw.println("  <!ELEMENT Users (aUser, nUser)+ >");
            pw.println("  <!ELEMENT aUser (name, userID, emailAddress) >");
            pw.println("  <!ELEMENT nUser (name, userID, emailAddress) >");
            pw.println("  <!ELEMENT name (#PCDATA) >");
            pw.println("  <!ELEMENT userID (#PCDATA) >");
            pw.println("  <!ELEMENT emailAddress (#PCDATA) >");
            pw.println("] >");

            // Write out the data
            pw.println("<Users>");

            // Write out the xml representation of each account object.
            // It doesn't matter which type of account it is - because
            // BankAccount has a getXML() method as well as the sub-classes
            // having the same signature methods, it will use whichever is
            // appropriate to the current object.
            for (int ct = 0; ct < users.size(); ct++) {

                user = users.getUserAt(ct);
                pw.println(user.getXML());
            }


            pw.println("</Users>");

            // Close the file
            pw.close();
        } catch (IOException err) {
            err.printStackTrace();
        }

        // Finally, close the program.
        System.exit(0);
    }
parkz16
Newbie Poster
6 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

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

AndreiDMS
Junior Poster in Training
68 posts since Oct 2009
Reputation Points: 10
Solved Threads: 9
 

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.(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)

parkz16
Newbie Poster
6 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

A NullPointerException is
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

AndreiDMS
Junior Poster in Training
68 posts since Oct 2009
Reputation Points: 10
Solved Threads: 9
 

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

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 
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.: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.(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 expectothers 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:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 
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..

musthafa.aj
Posting Whiz
312 posts since Nov 2009
Reputation Points: 19
Solved Threads: 45
 

Just replace simple types like int, long etc. by corresponding class types like Integer, Long etc. It really helps :-)

xy75
Newbie Poster
1 post since Jan 2011
Reputation Points: 7
Solved Threads: 0
 

>Just replace simple types like int, long etc. by corresponding class types like Integer, Long etc. It really helps :-)
Why on Earth did you resurrect this old thread to post such nonsense?

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You