Results 1 to 15 of 15
  1. #1
    PedramCarter is offline Member
    Join Date
    Nov 2014
    Posts
    27
    Rep Power
    0

    Default Passing Variable from Class A to Class B to do calculation but getting error?

    I'm getting this error, I definitely know that is my error trying to pass method/variable because when I commented received part of my code it ran and worked.
    I get this error
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)


    Here are my 2 classes

    Java Code:
    }
    	import java.awt.BorderLayout;
    import java.awt.Checkbox;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JCheckBox;
    import java.util.Scanner;
    import javax.swing.WindowConstants;
    import javax.swing.SwingUtilities;
    	public ItemsPage() {
    		Pounds = "";
    		//super();
    		initGUI();
    		Scanner sc = new Scanner(System.in);
    		CashPay sendTotalPrice = new CashPay();
    		System.out.println("Enter your amount");
    		TotalPrice = sc.nextDouble();
    		sendTotalPrice.printTotalPrice(TotalPrice);
    	}



    Java Code:
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    
    import javax.swing.WindowConstants;
    import javax.swing.SwingUtilities;
    
    public class CashPay extends javax.swing.JFrame {
    
    	private JLabel cashamountlbl;
    	private JButton Calculatebtn;
    	private JLabel CashChangelbl;
    	private JTextField CashAmounttxt;
    	private double ChangeLeft;
    	private double price;
    	private double CashInsert = Double.parseDouble(CashAmounttxt.getText());
    	
    
    	/**
    	* Auto-generated main method to display this JFrame
    	*/
    	public static void main(String[] args) {
    		SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				CashPay inst = new CashPay();
    				inst.setLocationRelativeTo(null);
    				inst.setVisible(true);
    				
    			}
    			
    		});
    		ItemsPage callClass = new ItemsPage();
    		callClass.checkwhatIsSelected();
    	}
    	//Converts input from textfield into double variable.
    	public CashPay() {
    		super();
    		
    		initGUI();
    	//Receives variable from another class "ItemsPage"	
    	}
    	public void printTotalPrice(double RecievedTotalPrice){
    		
    		ChangeLeft = CashInsert - RecievedTotalPrice;
    		System.out.println (ChangeLeft);
    	}

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Passing Variable from Class A to Class B to do calculation but getting error?

    Quote Originally Posted by PedramCarter View Post
    I get this error
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
    That's part of the stack trace, but you left out the most important part: the first line, which identifies the error.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    PedramCarter is offline Member
    Join Date
    Nov 2014
    Posts
    27
    Rep Power
    0

    Default Re: Passing Variable from Class A to Class B to do calculation but getting error?

    My bad, here

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at CashPay.<init>(CashPay.java:32)
    at ItemsPage.<init>(ItemsPage.java:72)
    at ItemsPage$1.run(ItemsPage.java:58)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$400(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Passing Variable from Class A to Class B to do calculation but getting error?

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at CashPay.<init>(CashPay.java:32)
    at ItemsPage.<init>(ItemsPage.java:72)
    at ItemsPage$1.run(ItemsPage.java:58)
    The code for the lines in the error message also are missing.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    PedramCarter is offline Member
    Join Date
    Nov 2014
    Posts
    27
    Rep Power
    0

    Default Re: Passing Variable from Class A to Class B to do calculation but getting error?

    I have no idea what you're talking about bro I suck, where do I copy it from, this error isn't actually displayed in my syntax only happens and shows when I run the program :( sorry can you help me, that's all I could find I'm running eclipse.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Passing Variable from Class A to Class B to do calculation but getting error?

    The posted code for the ItemsPage class has 25 lines. The error message says that the code in ItemsPage on lines 72 and 58 were being executed when the exception happened. Where are those lines of code?

    It looks like the code that is posted does not go with the error message.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    PedramCarter is offline Member
    Join Date
    Nov 2014
    Posts
    27
    Rep Power
    0

    Default Re: Passing Variable from Class A to Class B to do calculation but getting error?

    I didn't post the whole code would you like me it's a little long I don't want to annoy you or anything like that or make people read through tons of pages of code, what should I do?

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,717
    Rep Power
    17

    Default Re: Passing Variable from Class A to Class B to do calculation but getting error?

    At a bare minimum line 72 is needed. Very likely some variable or expression on that line is null when it shouldn't be.

    Edit: reading more closely, what is line 32 of the CashPay class? The real line 32 I mean, not the 32nd line of what you posted.
    Last edited by pbrockway2; 03-24-2015 at 02:12 AM.

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Passing Variable from Class A to Class B to do calculation but getting error?

    On observation which may have nothing to do with your current problem but is important.

    In your CashPay class you employ SwingUtilities.invokeLater(). Then you immediately go and start doing some stuff. If the "stuff" depends on your thread invocation, then things might go awry since your invocation may actually not take effect until after you have created an ItemsPage instance. This asynchronous behavior can not only cause problems but be inconsistent (i.e. sometimes your app will work and other times it won't). You just need to be aware of this.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    PedramCarter is offline Member
    Join Date
    Nov 2014
    Posts
    27
    Rep Power
    0

    Default Re: Passing Variable from Class A to Class B to do calculation but getting error?

    Quote Originally Posted by jim829 View Post
    On observation which may have nothing to do with your current problem but is important.

    In your CashPay class you employ SwingUtilities.invokeLater(). Then you immediately go and start doing some stuff. If the "stuff" depends on your thread invocation, then things might go awry since your invocation may actually not take effect until after you have created an ItemsPage instance. This asynchronous behavior can not only cause problems but be inconsistent (i.e. sometimes your app will work and other times it won't). You just need to be aware of this.

    Regards,
    Jim
    Yes I'm aware of that, the way I've designed it to open login screen, you enter account details you go to items page, you click complete shopping button you go to payment method, then you click cash or card and then etc... I'm open to suggestions to improve this, thank you.

  11. #11
    PedramCarter is offline Member
    Join Date
    Nov 2014
    Posts
    27
    Rep Power
    0

    Default Re: Passing Variable from Class A to Class B to do calculation but getting error?

    Quote Originally Posted by pbrockway2 View Post
    At a bare minimum line 72 is needed. Very likely some variable or expression on that line is null when it shouldn't be.

    Edit: reading more closely, what is line 32 of the CashPay class? The real line 32 I mean, not the 32nd line of what you posted.
    Whats wrong with it mate? how can I fix it?

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Passing Variable from Class A to Class B to do calculation but getting error?

    Quote Originally Posted by PedramCarter View Post
    Yes I'm aware of that, the way I've designed it to open login screen, you enter account details you go to items page, you click complete shopping button you go to payment method, then you click cash or card and then etc... I'm open to suggestions to improve this, thank you.
    No suggestions for improvement. First, not enough background. Second, no sense in trying to improve something which doesn't yet quite work correctly. Need to focus on that first. I just wanted you to be aware of the asynchronous nature of this. I have been bitten by it in the past.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Passing Variable from Class A to Class B to do calculation but getting error?

    First off, you need to point out to us which line is line 32 in the CashPay class, as something on that line is null.
    Then you need (as asked) to show the code around line 72 and line 58 of the ItemsPage class.

    You need to identify those lines to us, as the line numbering here is unlikely to match what you have on your machine.
    Without that we are simply guessing as to what is wrong.

    No need to post the whole code, just the bits of your code from that stack trace, and some context around them (eg the methods they're in).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  14. #14
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,717
    Rep Power
    17

    Default Re: Passing Variable from Class A to Class B to do calculation but getting error?

    Quote Originally Posted by PedramCarter View Post
    Whats wrong with it mate? how can I fix it?
    I don't wish to be mean, but you seem to be asking "how can I fix line 32?" without acually telling us what line 32 is.

    I know that it is *not* line 32 of the posted code. Go to your source code for CashPay.java, count down 32 lines and post what you see there.

  15. #15
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Passing Variable from Class A to Class B to do calculation but getting error?

    Quote Originally Posted by pbrockway2 View Post
    Go to your source code for CashPay.java, count down 32 lines and post what you see there.
    We live in the 21st century, I do hope his editor has a way to show line numbers ;)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Passing in a variable from one class to another!?
    By DayKema in forum New To Java
    Replies: 4
    Last Post: 08-16-2014, 11:37 PM
  2. Replies: 10
    Last Post: 06-26-2014, 09:04 AM
  3. Replies: 2
    Last Post: 01-08-2014, 05:09 PM
  4. Passing instance of derived class as base class
    By goatjugsoup in forum New To Java
    Replies: 7
    Last Post: 08-01-2012, 05:15 AM
  5. Replies: 8
    Last Post: 04-14-2010, 05:49 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
  •