File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How do I pass an instance of an object to a different object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How do I pass an instance of an object to a different object" Watch "How do I pass an instance of an object to a different object" New topic
Author

How do I pass an instance of an object to a different object

Darren Wilkinson
Greenhorn

Joined: Feb 09, 2004
Posts: 21
I have a class ApplicationGui2 (ag) which creates an instance of MyTableModel2 (mtm)

MyTableModel2 has all the methods required to create instances of the class RecordData (rd)

The user uses a modal dialog which instantiates an instance of class NewRecordInput (nri)

nri gathers the user data and then needs to pass this data (stored as a String[]) to the mtm method createNewRecord() which creates a new instance of class RecordData and stores this object in mtm's Vector

What I want to do is; pass a reference to mtm, from ag to nri, so that nri can call mtm.createNewRecord() but my attempts so far produce a NullPointerException

Can anyone help ???
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
change the constructor for NewRecordInput() from this

public NewRecordInput(MyTableModel2 testTableModel)
{
MyTableModel2 tempTableModel = new MyTableModel2();
tempTableModel = testTableModel;
...
}

to this

MyTableModel2 tempTableModel;//<---moved to here
public NewRecordInput(MyTableModel2 testTableModel)
{
//MyTableModel2 tempTableModel = new MyTableModel2();//<----remove line
tempTableModel = testTableModel;
...
}

this should get rid of your NPE.
Don't know if it will get you the funcionality you want.

You may also want to add this line to the constructor for ApplicationGui2

setDefaultCloseOperation(EXIT_ON_CLOSE);
Darren Wilkinson
Greenhorn

Joined: Feb 09, 2004
Posts: 21
Thank you :-)

My question is: why does this work and my code not work. I am having trouble following the reason for this.

Could you explain please.

Thank you.

By the way, this isn't a homework assignment. I am studying towards the J2SE Programmers certification but on my own using Mughal & Rasmussens' Guide to Java Certification... oh and the WhizLabs exam sim.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
Declaring 'MyTableModel2 tempTableModel' within the constructor makes
tempTableModel local to the constructor only, but access to tempTableModel is
required from setUserInput()
Darren Wilkinson
Greenhorn

Joined: Feb 09, 2004
Posts: 21
Thank you :-)
 
Don't get me started about those stupid light bulbs.
 
subject: How do I pass an instance of an object to a different object
 
Similar Threads
Add a new row to JTable
adding a row to a jtable by pressing a key
J Table Update (it makes me mad)
gsl.sql.driv
JTable, Table Models, Vectors & Objects : Confused