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

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

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);
    }

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

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)

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

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

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

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

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

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

commented: This makes absolutely no sense at all. -3

>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?

I would just like to add a suggestion for those who stumble upon this the way I did. I had the EXACT same error and even with this thread it took me forever to figure out (2 hours actually). I'm a new programmer (can't really even call me that yet) so forgive any mistakes.

So I had a JButton that I declared at the very top of my code (as instance variable i guess?) and then AGAIN i declared it WITHIN my constructor. Not sure if this is how to word it so I will put an example.

public class Hangman extends JFrame{

    private JButton a;  //created variable 'a' to refer to a future button.

    public Hangman(){
        JButton n = new JButton("n");  //then here I put "JButton" again, and aparently didn't need that? Because when I removed JButton from this line, everything went back to normal.
    }


    //then down here I had another method that was used to set a few properties of n.

I also post this because I don't fully understand it and would like to know why it did this. I assume that it went out of scope or something, because the action handler that was controlling the 'a' button worked great, but the method called later to work on the properties was outside of the method it was created in. The button never left the frame, but aparently no longer existed in memory. Can someone explain this so I understand WHY it happend? I fixed my bug, but would like to know why what I did worked lol. It will help others who stumble upon this because as a newbie, this was actually a tough bug to fix (hope the OP figured his out 2 years ago).

commented: thanks, your probelm was my same exact problem. did you ever find out the reason it didn't work? +0

Line 3 declares a button variable, but does not initialise it, so it's null
line 6 declares a second button variable that happens to have the same name, and initialises it to refer to a newly-created button.
At line 7 the second variable goes out of scope and ceases to exist. That leaves the button created on line 6 with no remaining references, so it's garbage collected.
The rest of the code then executes with the original button variable still null.

Crap, I didn't realize that I put 'a' as the first button and 'n' as the second. They were both supposed to be 'a'. I wish I could edit that now!

It seems like you knew what I meant to say though. Thank you for that explanation! I didn't realize that you could do that. So the only reason it allows me to declare two variables of the same name is because they are in different 'scopes'? Because normally it tells me "duplicate variable" or something like that. Does declaring it within the method OVERRIDE the old variable? Or does it just create a second pointer to the same object (or lack therof in this case).

A field and a local variable that happen to have the same name are otherwise completely unconnected. Only the name is the same. It is much like having a method and a local variable with the same name. As far as the compiler is concerned, it is just a trivial coincidence. The compiler cares no more that a field and variable have the same name than you would care about having the same number of teeth as the number of protons in an atom of germanium.

The only issue is the potential ambiguity about whether you are refering to the field or the variable when you use the name a, and that's not really ambiguous because the compiler always assumes that you mean the variable. If you want to use the field a in the scope of a local variable named a, you normally do it by typing this.a, since that makes the fact that a is a field explicit. Naturally you can use this every time you access a field of this, and leaving this off is just a conveninent abbreviation that unfortunately causes some confusion as in this case. JavaScript and Python are examples of languages that requires this every time, and if you want to do that in Java it would be hard to call you wrong since it allows anyone reading your source code to instantly distinguish fields from local variables.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.