Results 1 to 19 of 19
- 07-01-2011, 01:47 AM #1
Member
- Join Date
- Jul 2011
- Posts
- 9
- Rep Power
- 0
New to Java first time writing a multiclass getting an error
Its a simple little gas app, 3 buttons one for regular premium and super, when you click the respective button, its suppose to call up the respective class method and out put the total, my problem is if i try to use the premium button first, its just spits "AWT-EventQueue-0" java.lang.NullPointerException. if i use the regular button first i get the same error but it calculates and then i can use the premium button from then on with the error keep coming up. Im thinking i should be using an IF / else or if/this kind of deal in my code, but i dont know how to do it calling up outside classes. any suggestions. Thanks!Java Code:public void determineFuelGrade() { if(regularJRadioButton.isSelected()) regularFuel = new RegularFuel(gallonsOfGas,regularGas); totalAmount = regularFuel.getResults(); totalSaleJTextField.setText("" + currency.format (totalAmount)); if(premiumJRadioButton.isSelected()) premiumFuel = new PremiumFuel(gallonsOfGas,premiumGas); totalAmount = premiumFuel.getResults(); totalSaleJTextField.setText("" + currency.format (totalAmount)); }
- 07-01-2011, 01:54 AM #2
The error message will tell you on what line the exception occurs. Go to that line and find out which of the variables are null. You can do this by adding print statements before that line to print out all possible variables. Once you know which variable is nukll then track down why it is null. If you don't know when you declare a variable but do not initialise it then it is null.
Java Code:Foo f; // f is null f.doStuff(); // null pointer exception is thrown
- 07-01-2011, 02:26 AM #3
Member
- Join Date
- Jul 2011
- Posts
- 9
- Rep Power
- 0
oh man im one of those idiots, the error message im actually getting is "at java.security.AccessController.doPrivileged(Native Method)" the other one was from an earlier issue that i remedied already.
- 07-01-2011, 02:44 AM #4
Please post the full text of the error message. There can be useful information in it to help.the error message im actually getting is
- 07-01-2011, 02:53 AM #5
Member
- Join Date
- Jul 2011
- Posts
- 9
- Rep Power
- 0
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 613)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
----jGRASP: operation complete.
- 07-01-2011, 02:57 AM #6
How are the error message you just posted and the code you posted earlier related?
There is nothing in your posts to connect them.
It looks like you have left off the beginning line(s) of the error message. The one that says what the error was.
For example, the first line says Exception and the following ones all begin with at:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: adding container's parent to itself
at java.awt.Container.checkAddToSelf(Container.java:4 20)
at java.awt.Container.addImpl(Container.java:1038)
at javax.swing.CellRendererPane.addImpl(CellRendererP ane.java:89)
at java.awt.Container.add(Container.java:365)
- 07-01-2011, 03:05 AM #7
Member
- Join Date
- Jul 2011
- Posts
- 9
- Rep Power
- 0
ok i cleared the the box and you are right, its all the same error message. its big but here it is.
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at AcmeSelfServe.determineFuelGrade(AcmeSelfServe.jav a:238)
at AcmeSelfServe.enterJButtonActionPerformed(AcmeSelf Serve.java:215)
at AcmeSelfServe$1.actionPerformed(AcmeSelfServe.java :154)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.jav a:6288)
at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
at java.awt.Component.processEvent(Component.java:605 3)
at java.awt.Container.processEvent(Container.java:204 1)
at java.awt.Component.dispatchEventImpl(Component.jav a:4651)
at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
at java.awt.Component.dispatchEvent(Component.java:44 81)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4168)
at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478 )
at java.awt.Component.dispatchEvent(Component.java:44 81)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:643)
at java.awt.EventQueue.access$000(EventQueue.java:84)
at java.awt.EventQueue$1.run(EventQueue.java:602)
at java.awt.EventQueue$1.run(EventQueue.java:600)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:616)
at java.awt.EventQueue$2.run(EventQueue.java:614)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 613)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
if i remove all the whole premiumfuel code just delete all of it off the program, it works fine with no errors, im convinced my If followed by an If and both outputting to the same field and drawing on 2 different outside classes to do the math is the problem, shouldnt it be an if/ else kind of deal or an if/this. I really dont know this is seriously my 4th program ever and im really lost with this one.
- 07-01-2011, 03:12 AM #8
Look at line 238 and determine which variable is null. If you can't tell, add a println just before it to print out the values of all the variables.AcmeSelfServe.java:238)
There might be a problem with the code you posted. The code following the if is indented but it is not enclosed in {}. Only the first statement is conditioned by the if the others will always be executed.
-
So which lines in your code are AcmeSelfServe.java:238 (the 238th line of this class) and AcmeSelfServe.java:215 (the 215th line of this class)?
- 07-01-2011, 03:17 AM #10
Member
- Join Date
- Jul 2011
- Posts
- 9
- Rep Power
- 0
determineFuelGrade(); is 215
totalAmount = premiumFuel.getResults(); is 238
-
This suggests that premiumFuel is null, but it appears to have been constructed just prior to being used. Are you sure that line 238 is the one shown? That it is not in fact the radiobutton? You can only correlate line numbers if you don't change your code after you've run it.
- 07-01-2011, 03:19 AM #12
Member
- Join Date
- Jul 2011
- Posts
- 9
- Rep Power
- 0
thats the code for the215 line wich wasnt included in the orginal postJava Code:try { gallonsOfGas = Integer.parseInt(numberOfGallonsJTextField.getText()); determineFuelGrade(); }
- 07-01-2011, 03:21 AM #13
determine which variable is null. If you can't tell, add a println just before it to print out the values of all the variables.
- 07-01-2011, 03:25 AM #14
Member
- Join Date
- Jul 2011
- Posts
- 9
- Rep Power
- 0
my thinking was because this is line 235 totalAmount = regularFuel.getResults();
- 07-01-2011, 03:37 AM #15
Member
- Join Date
- Jul 2011
- Posts
- 9
- Rep Power
- 0
should each if statement have its {}
-
- 07-01-2011, 03:43 AM #17
Also Make it a habit to type the ending } on the next line everytime you type the beginning {.
Then insert your new code between them.
- 07-01-2011, 03:43 AM #18
Member
- Join Date
- Jul 2011
- Posts
- 9
- Rep Power
- 0
and that was the problem 2 sets of {}, cost me almost 4 hours of my life. Thanks dude.
- 07-01-2011, 03:46 AM #19
Similar Threads
-
JAVA Programmers Wanted - Full-Time and Part-Time Positions open
By javatrek2020 in forum Jobs OfferedReplies: 3Last Post: 08-23-2011, 12:46 PM -
writing the files for particular time
By damuammu in forum Advanced JavaReplies: 3Last Post: 03-15-2011, 06:32 PM -
Help. Java run time error
By slimce in forum New To JavaReplies: 7Last Post: 05-15-2010, 04:15 PM -
Java 1.5 compile time error
By ank_k in forum New To JavaReplies: 4Last Post: 11-13-2008, 11:12 AM -
Logic Error: Not Writing To File
By JDCAce in forum Advanced JavaReplies: 6Last Post: 10-21-2008, 02:13 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks