Results 1 to 15 of 15
  1. #1
    jokkycaz is offline Member
    Join Date
    Jul 2011
    Posts
    8
    Rep Power
    0

    Default Quick question about creating and instance and passing it to a constuctor in a GUI

    So I have a question. I need to create and instance of CardManager. Which I do here.
    Java Code:
    public static void main(String[] args)
    	{
    		new GUI2();
    		new GUI();
    		CardManager man = new CardManager();
    		
    	}
    I need to know how to pass the instance to a constructor in my GUI class.

    Java Code:
    CardManager manage;
    	
    	public GUIpanel2(main m, CardManager manage) 
    	{
    		super();
    		
    		this.manage = manage;
    	}

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default

    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    jokkycaz is offline Member
    Join Date
    Jul 2011
    Posts
    8
    Rep Power
    0

    Default

    I'm aware of that but does not answer my question.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default

    Quote Originally Posted by jokkycaz View Post
    I'm aware of that but does not answer my question.
    What about it didn't you understand? Perhaps read the actual start to that tutorial: Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    jokkycaz is offline Member
    Join Date
    Jul 2011
    Posts
    8
    Rep Power
    0

    Default

    I defiantly read that a few hours ago. I know how to pass information to a constructor in the same class. I do not know how to pass an instance to two seperate constructors OUTSIDE of the main program. Am I missing where it talks about that in the link?

    Java Code:
    CardManager man = new CardManager();
    		Panel2 panel = new Panel2(man);
    		GUIpanel2 panel1 = new GUIpanel2(man);
    This doesn't work not sure why.

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

    Default

    This doesn't work
    What happens? "doesn't work" is not a useful description of the problem.
    Does GUIpanel2 have more constructors than the one you posted in post#1.
    That one shows that the constructor takes 2 arguments, not the single one you show in post#5

  7. #7
    jokkycaz is offline Member
    Join Date
    Jul 2011
    Posts
    8
    Rep Power
    0

    Default

    its supposed to have only 1. I was experimenting with different things.
    Java Code:
    CardManager manage;
    	
    	public GUIpanel2(CardManager manage) 
    	{
    		this.manage = manage;
    	}
    Java Code:
    public class main 
    {
    	public static void main(String[] args)
    	{
    		new GUI2();
    		new GUI();
    		CardManager man = new CardManager();
    		
    	}
    }
    In my panel class i am trying to open a credit card. When i do this i get that the credit card i opened is null.

    [CODE]
    public class OpenAccount implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {

    String n = name.getText();
    if(name.getText().trim().length() != 0)
    {
    id2 = manage.openCreditCard(n, 5000.0, 5.0);
    String id1 = Integer.toString(id2);
    id.setText(id1);
    }
    else
    JOptionPane.showMessageDialog(null, "Please enter a name");

    }
    }
    [\CODE]

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

    Default

    When i do this i get that the credit card i opened is null.
    Can you show the error message and the code that generated it?

  9. #9
    jokkycaz is offline Member
    Join Date
    Jul 2011
    Posts
    8
    Rep Power
    0

    Default

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at GUIpanel2$OpenAccount.actionPerformed(GUIpanel2.ja va:51)

    Java Code:
    public class OpenAccount implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			
    			String n = name.getText();
    			if(name.getText().trim().length() != 0)
    			{
    				[B]id2 = manage.openCreditCard(n, 5000.0, 5.0);[/B]
    				String id1 = Integer.toString(id2);
    				id.setText(id1);
    			}
    			else
    				JOptionPane.showMessageDialog(null, "Please enter a name");
    			
    		}
    	}
    The bold is causing it

  10. #10
    jokkycaz is offline Member
    Join Date
    Jul 2011
    Posts
    8
    Rep Power
    0

    Default

    To explain it better. I have a Panel that basically allows a person to put there name in and they can open a creditcard. I have a class called CardManager that does the creating of the creditcards. I have a main program and i have to open the instance of CardManager and then pass it to my gui panel so i can open a credit card with that

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default

    Is the manage variable null?
    Where is it given a value?

  12. #12
    jokkycaz is offline Member
    Join Date
    Jul 2011
    Posts
    8
    Rep Power
    0

    Default

    Yes manage is null. When the gui is ran all the person has to enter is a name. I have the automatically set. aka the interest and balance

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

    Default

    Yes manage is null.
    Then you can't do this:
    manage.openCreditCard(n, 5000.0, 5.0);

  14. #14
    jokkycaz is offline Member
    Join Date
    Jul 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Then you can't do this:
    manage.openCreditCard(n, 5000.0, 5.0);
    ? I already have that exact code.

    How can I get the variable man in my main program into my constructor in GUIpanel2?

    The main program will create an instance of the CardManager and then pass it to the Banker and User gui constructors.

    ^ this is what i cant figure out
    Last edited by jokkycaz; 07-21-2011 at 08:44 PM.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default

    How can I get the variable man in my main program into my constructor in GUIpanel2?
    You have already shown you know how to do this. Please explain your problem.


    Java Code:
        
                   CardManager man = new CardManager();  // Create an instance of the CardManager
    		...
    	       GUIpanel2 panel1 = new GUIpanel2(man);  // pass a reference to it to the constructor
    Perhaps if you wrote a small simple program that compiles to show your problem. The short bits of code you have posted don't show what the problem is.

Similar Threads

  1. Finding and Creating Instance of Every Class in Package
    By Julian Trust in forum New To Java
    Replies: 5
    Last Post: 06-25-2011, 08:34 AM
  2. Help with creating an instance of a class extension
    By Inferno719 in forum New To Java
    Replies: 19
    Last Post: 05-04-2011, 03:53 AM
  3. Replies: 10
    Last Post: 01-06-2010, 08:33 AM
  4. Hello everyone! quick question.
    By irishhokie in forum New To Java
    Replies: 5
    Last Post: 04-03-2009, 04:13 AM
  5. passing an array into an instance
    By lockmac in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 09:35 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
  •