Results 1 to 8 of 8
  1. #1
    cfisher14 is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default Null exception from canceling JDialog

    Hello, this is my first post! :D

    I am in CIS 163 - second level java at GVSU. We are on our second project and its main theme is polymorphism... fun stuff. Anywho, my issue isn't from polymorphism but rather from canceling a JDialog box. Since no textfields in the box were filled, the result from hitting cancel is a Null Pointer Exception. I don't want to post my code right away due to it being sorta lengthy.

    Normally, the person would fill out the textfields and hit OK. It would then send the data to another class and it would display the data accordingly. However, if they hit cancel it blows up. I have tried to google and bing search for sample code but with no luck. I searched for a bit on this site to see if anyone else made a thread about it but no luck either. Alas, I am creating a thread to plea for help!

    I have tried using try-catches but it didn't work or I am not using it right.


    Thank you very much!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    try/catches aren't going to fix this and shouldn't be used. Check the line that the NPE occurs on and find out which object is null that you're trying to de-reference, and work back from that. The key is in the code. That's all the help we can give based on the information you've presented.

  3. #3
    cfisher14 is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    The exception comes from the method that tries to use variables given by the text-fields when the user fills them in. When cancel is clicked, the variables aren't filled in so the method can't do anything with them. I can't fill them though, otherwise they would get displayed. The JDialog box has several text-fields. Once the user fills in the fields and clicks ok, the program then grabs the text and displays a summary of the data in a JList and a detailed version in a JTextArea just below the JList.

    The only option I can think of would be to make the variables equal something, but if I do, they get displayed. I just need it to close the dialog box when cancel is clicked and not do anything with the variables.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  5. #5
    cfisher14 is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    It does it no matter what, because the class that has the method to use the variables calls the class to create the variables. So, no matter what it will be trying to get something.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by cfisher14 View Post
    It does it no matter what, because the class that has the method to use the variables calls the class to create the variables. So, no matter what it will be trying to get something.
    ???
    You might want to give us more detail.

    It's a modal dialog, so you'll know exactly when the user is done dealing with it. At that point in time, it should be easy to check if the user has entered valid data or if data is null, and based on this information, decide whether to try to extract the data or not. If this doesn't help and you're still stuck, you'll need to provide more information. Your greatest chances of getting help would occur if you took the time and effort to create an SSCCE (please check out the link).

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    If the user clicks "cancel" then, clearly, the values of the fields (even if they have values) should not be used.

    You said before "Once the user fills in the fields and clicks ok,...". Now you seem to be saying that some other class expects valid values. In that case you have to return some default values.

    Better I think to stick to your original description and restructure things so that the summary and detail are only calculated and displayed if the user clicks "OK". (This is very likely what the user would expect.)

    ------------------

    SSCCE++

    (really just adding my 4.00 to suggest that the behaviour should be seen from the user's point of view)

  8. #8
    cfisher14 is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    Alright, sorry for my poor explanation! It is hard enough for me to get the handle of this language! I will try again.

    This is the actionPerformed of a button to add a savings account. This is in my bankGUI class.
    if (addSaving == e.getSource()) {

    SavingsAccount s = new SavingsAccount();
    DialogSavingAdd sd = new DialogSavingAdd(this, s);

    jArea.clearSelection();
    dListModel.addAcct(s);
    }
    After that is clicked, it calls the DialogSavingAdd class. Which is where the JDialog and all the text-fields are created. After a user click ok, the information is then shipped to the BankEngine class and used by this method.

    public Object getElementAt(int arg0) {

    return "Number: " +listAccts.get(arg0).getNumber()+"; "+
    "Name: " + listAccts.get(arg0).getOwner().getName() +"; "+
    "Bal: " + listAccts.get(arg0).getBalance();
    }
    After the information has been shipped over to the BankEngine from DialogSavingAdd. The information is then stored in listAccts, which is an arraylist. Then the above method returns the string back to the bankGUI to have it displayed. However, if the user clicks cancel the variables are null and creates the NPE at that method.

    Our professor emphasizes separation of model and view. BankGUI handles all the looks and BankEngine handles all the logic. I am sorry for not making an SSCCE with the code but it was hard since a lot of classes are called upon and so many things are happening due to it being one big pile of steaming polymorphism.

Similar Threads

  1. Null Pointer exception
    By diegoyj in forum New To Java
    Replies: 7
    Last Post: 01-29-2010, 05:17 PM
  2. [SOLVED] Null exception
    By leapinlizard in forum New To Java
    Replies: 2
    Last Post: 04-29-2009, 07:40 AM
  3. Null Pointer Exception
    By andre1011 in forum Advanced Java
    Replies: 4
    Last Post: 02-07-2009, 04:30 AM
  4. Null Pointer Exception
    By ScKaSx in forum New To Java
    Replies: 1
    Last Post: 01-24-2009, 12:27 PM
  5. Null Pointer Exception
    By Jacinth in forum New To Java
    Replies: 4
    Last Post: 01-22-2009, 02:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •