Results 1 to 19 of 19
  1. #1
    SquintyB is offline Member
    Join Date
    Jul 2011
    Posts
    9
    Rep Power
    0

    Default New to Java first time writing a multiclass getting an error

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

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    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

  3. #3
    SquintyB is offline Member
    Join Date
    Jul 2011
    Posts
    9
    Rep Power
    0

    Default

    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.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    the error message im actually getting is
    Please post the full text of the error message. There can be useful information in it to help.

  5. #5
    SquintyB is offline Member
    Join Date
    Jul 2011
    Posts
    9
    Rep Power
    0

    Default

    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.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    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)

  7. #7
    SquintyB is offline Member
    Join Date
    Jul 2011
    Posts
    9
    Rep Power
    0

    Default

    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.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    AcmeSelfServe.java:238)
    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.

    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.

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

    Default

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

  10. #10
    SquintyB is offline Member
    Join Date
    Jul 2011
    Posts
    9
    Rep Power
    0

    Default

    determineFuelGrade(); is 215

    totalAmount = premiumFuel.getResults(); is 238

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

    Default

    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.

  12. #12
    SquintyB is offline Member
    Join Date
    Jul 2011
    Posts
    9
    Rep Power
    0

    Default

    Java Code:
     try  
             {  
                gallonsOfGas = Integer.parseInt(numberOfGallonsJTextField.getText());  
                determineFuelGrade();  
             }
    thats the code for the215 line wich wasnt included in the orginal post

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    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.

  14. #14
    SquintyB is offline Member
    Join Date
    Jul 2011
    Posts
    9
    Rep Power
    0

    Default

    my thinking was because this is line 235 totalAmount = regularFuel.getResults();

  15. #15
    SquintyB is offline Member
    Join Date
    Jul 2011
    Posts
    9
    Rep Power
    0

    Default

    should each if statement have its {}

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

    Default

    Quote Originally Posted by SquintyB View Post
    should each if statement have its {}
    Yes, and in fact every block should have its own pair of curly braces. This will save your tale time and time again.

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    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.

  18. #18
    SquintyB is offline Member
    Join Date
    Jul 2011
    Posts
    9
    Rep Power
    0

    Default

    and that was the problem 2 sets of {}, cost me almost 4 hours of my life. Thanks dude.

  19. #19
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    And you're just getting started. Just think how many more hours will be wasted by some stupid mistake.
    Most of us have done it. Hopefully only once.

Similar Threads

  1. Replies: 3
    Last Post: 08-23-2011, 12:46 PM
  2. writing the files for particular time
    By damuammu in forum Advanced Java
    Replies: 3
    Last Post: 03-15-2011, 06:32 PM
  3. Help. Java run time error
    By slimce in forum New To Java
    Replies: 7
    Last Post: 05-15-2010, 04:15 PM
  4. Java 1.5 compile time error
    By ank_k in forum New To Java
    Replies: 4
    Last Post: 11-13-2008, 11:12 AM
  5. Logic Error: Not Writing To File
    By JDCAce in forum Advanced Java
    Replies: 6
    Last Post: 10-21-2008, 02:13 AM

Posting Permissions

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