Results 1 to 8 of 8
- 02-27-2011, 01:06 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 4
- Rep Power
- 0
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!
-
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.
- 02-27-2011, 01:28 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 4
- Rep Power
- 0
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.
-
why not if the user clicks cancel, don't try to extract the variables at all?
- 02-27-2011, 01:48 AM #5
Member
- Join Date
- Feb 2011
- Posts
- 4
- Rep Power
- 0
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).
- 02-27-2011, 02:02 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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)
- 02-27-2011, 02:36 AM #8
Member
- Join Date
- Feb 2011
- Posts
- 4
- Rep Power
- 0
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.
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.if (addSaving == e.getSource()) {
SavingsAccount s = new SavingsAccount();
DialogSavingAdd sd = new DialogSavingAdd(this, s);
jArea.clearSelection();
dListModel.addAcct(s);
}
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.public Object getElementAt(int arg0) {
return "Number: " +listAccts.get(arg0).getNumber()+"; "+
"Name: " + listAccts.get(arg0).getOwner().getName() +"; "+
"Bal: " + listAccts.get(arg0).getBalance();
}
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
-
Null Pointer exception
By diegoyj in forum New To JavaReplies: 7Last Post: 01-29-2010, 04:17 PM -
[SOLVED] Null exception
By leapinlizard in forum New To JavaReplies: 2Last Post: 04-29-2009, 06:40 AM -
Null Pointer Exception
By andre1011 in forum Advanced JavaReplies: 4Last Post: 02-07-2009, 03:30 AM -
Null Pointer Exception
By ScKaSx in forum New To JavaReplies: 1Last Post: 01-24-2009, 11:27 AM -
Null Pointer Exception
By Jacinth in forum New To JavaReplies: 4Last Post: 01-22-2009, 01:47 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks