Results 1 to 15 of 15
- 07-21-2011, 05:41 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 8
- Rep Power
- 0
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.
I need to know how to pass the instance to a constructor in my GUI class.Java Code:public static void main(String[] args) { new GUI2(); new GUI(); CardManager man = new CardManager(); }
Java Code:CardManager manage; public GUIpanel2(main m, CardManager manage) { super(); this.manage = manage; }
- 07-21-2011, 05:45 PM #2
First hit for googling "java constructor": Passing Information to a Method or a Constructor (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!
- 07-21-2011, 07:21 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 8
- Rep Power
- 0
- 07-21-2011, 07:24 PM #4
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!
- 07-21-2011, 07:32 PM #5
Member
- Join Date
- Jul 2011
- Posts
- 8
- Rep Power
- 0
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?
This doesn't work not sure why.Java Code:CardManager man = new CardManager(); Panel2 panel = new Panel2(man); GUIpanel2 panel1 = new GUIpanel2(man);
- 07-21-2011, 07:47 PM #6
What happens? "doesn't work" is not a useful description of the problem.This doesn't work
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
- 07-21-2011, 07:50 PM #7
Member
- Join Date
- Jul 2011
- Posts
- 8
- Rep Power
- 0
its supposed to have only 1. I was experimenting with different things.
Java Code:CardManager manage; public GUIpanel2(CardManager manage) { this.manage = manage; }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.Java Code:public class main { public static void main(String[] args) { new GUI2(); new GUI(); CardManager man = new CardManager(); } }
[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]
- 07-21-2011, 08:20 PM #8
Can you show the error message and the code that generated it?When i do this i get that the credit card i opened is null.
- 07-21-2011, 08:21 PM #9
Member
- Join Date
- Jul 2011
- Posts
- 8
- Rep Power
- 0
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at GUIpanel2$OpenAccount.actionPerformed(GUIpanel2.ja va:51)
The bold is causing itJava 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"); } }
- 07-21-2011, 08:24 PM #10
Member
- Join Date
- Jul 2011
- Posts
- 8
- Rep Power
- 0
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
- 07-21-2011, 08:27 PM #11
Is the manage variable null?
Where is it given a value?
- 07-21-2011, 08:31 PM #12
Member
- Join Date
- Jul 2011
- Posts
- 8
- Rep Power
- 0
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
- 07-21-2011, 08:37 PM #13
Then you can't do this:Yes manage is null.
manage.openCreditCard(n, 5000.0, 5.0);
- 07-21-2011, 08:40 PM #14
Member
- Join Date
- Jul 2011
- Posts
- 8
- Rep Power
- 0
Last edited by jokkycaz; 07-21-2011 at 08:44 PM.
- 07-21-2011, 08:56 PM #15
You have already shown you know how to do this. Please explain your problem.How can I get the variable man in my main program into my constructor in GUIpanel2?
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.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
Similar Threads
-
Finding and Creating Instance of Every Class in Package
By Julian Trust in forum New To JavaReplies: 5Last Post: 06-25-2011, 08:34 AM -
Help with creating an instance of a class extension
By Inferno719 in forum New To JavaReplies: 19Last Post: 05-04-2011, 03:53 AM -
get java.lang.NullPointerException while creating an instance
By Basit56 in forum New To JavaReplies: 10Last Post: 01-06-2010, 08:33 AM -
Hello everyone! quick question.
By irishhokie in forum New To JavaReplies: 5Last Post: 04-03-2009, 04:13 AM -
passing an array into an instance
By lockmac in forum New To JavaReplies: 1Last Post: 08-08-2007, 09:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks