• Advertisement
Sign in to follow this  

[java] Parameters

This topic is 4477 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

It seems i'm missing something basic when passing objects as parameters to the constructor of other objects. For example: I create an object: A myA = new A(); If i pass this to the constructor of another object, within that constructor, the value of the myA i passed is null. All i want to do is call a method of the object that was passed. Why can't i do this? Dave

Share this post


Link to post
Share on other sites
Advertisement
i'm not sure i understand.

what exactly are you passing. actual code please( i dont think the types matter and classes aren't required; just the one line calling the ctor, maybe the constructor you are calling )

like so?

A myA = new A( new B() );

Share this post


Link to post
Share on other sites
Ok...

The object is

appdata globalData;

JTourneyTracker - where the object is created in teh SetData() method:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;

public class JTourneyTracker
{
/*
PUBLIC
*/

public JTourneyTracker()
{
SetData();
// Start the parser with a default file
databaseParser = new dbparser( "textfile.txt", globalData );

System.out.println( globalData.GetNumRows() );
SetWindow();
}

/*
PRIVATE
*/

private appdata globalData;
private dbparser databaseParser;

private void SetWindow()
{
JFrame.setDefaultLookAndFeelDecorated( true );

mainWindow window = new mainWindow( globalData );

Insets insets = window.getInsets();
window.setSize(590 + insets.left + insets.right,480 + insets.top + insets.bottom);
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
window.setVisible( true );
window.setResizable( false );
}

private void SetData()
{
globalData = new appdata();
}

/*
STATIC
*/

public static void main( String[] argv )
{
JTourneyTracker jtt = new JTourneyTracker();
}
}




mainwindow - where the object is recieved through the constructor, here the value of the object passed is not null, the if statement fails.

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class mainWindow extends JFrame
{
/*
PUBLIC
*/

public mainWindow( appdata dataBank )
{
m_dataBank = dataBank;
if ( dataBank == null )
System.out.println("In mainWindow() - dataBank = null");
GetInsets();
SetUpGui();


}

// PRIVATE
private appdata m_dataBank;
private String m_title = "JTourneyTracker";
private Insets m_insets;
private JPanel m_content = new JPanel();
private tabbedRegion m_tabbed = new tabbedRegion(m_dataBank);
private menuBar m_menu = new menuBar();


private void GetInsets()
{
m_insets = getInsets();
}

private void SetUpGui()
{
setTitle( m_title );

m_content.setLayout( new BorderLayout() );

Dimension dims = m_tabbed.getPreferredSize();

m_tabbed.setBounds( m_insets.left, m_insets.top, 600, 480 );

m_content.add( m_tabbed );

setJMenuBar( m_menu );

setContentPane( m_content );

pack();
}
}




tabbedregion - where the object is recieved through the constructor from the above class. Here the object is null and the if statement completes.

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class tabbedRegion extends JTabbedPane //implements ActionListener
{
// PUBLIC
public tabbedRegion( appdata dataBank )
{
m_dataBank = dataBank;
if ( dataBank == null )
System.out.println("In tabbedRegion() - dataBank = null");
InitTabbedRegion();
}

// PRIVATE
private String[] gameTypes = { "Limit Holdem",
"Pot Limit Holdem",
"No Limit Holdem" ,
"Limit Razz",
"Limit Omaha",
"Pot Limit Omaha",
"Limit Omaha Hi Lo",
"Pot Limit Omaha Hi Lo",
"Limit 7 Stud",
"Pot Limit 7 Stud",
"Mixed",
"H.O.R.S.E"
};

private JPanel resultsTab = new JPanel();
private appdata m_dataBank;

private void InitTabbedRegion()
{
BuildResultsTab();
addTab( "Results Editor", resultsTab );
}

private void BuildResultsTab()
{
JTextField finalPosition = new JTextField(5);
JTextField fieldSize = new JTextField(5);
JComboBox gameType = new JComboBox( gameTypes );
JTextArea notes = new JTextArea(3, 20);
JScrollPane scrollPaneNotes = new JScrollPane( notes );
JLabel finalPositionLabel = new JLabel("Final Position:");
JLabel fieldSizeLabel = new JLabel("Field Size:");
JLabel notesLabel = new JLabel("Notes:");
JLabel gameTypeLabel = new JLabel("Game Type:");
JTable entriesTable = new JTable( new tablemodel( m_dataBank ) );
JScrollPane tableScrollPane = new JScrollPane(entriesTable);

resultsTab.setLayout( null );

Dimension fpBounds = finalPosition.getPreferredSize();
Dimension fplBounds = finalPositionLabel.getPreferredSize();
Dimension fsBounds = fieldSize.getPreferredSize();
Dimension fslBounds = fieldSizeLabel.getPreferredSize();
Dimension scrollPaneNotesBounds = scrollPaneNotes.getPreferredSize();
Dimension nlBounds = notesLabel.getPreferredSize();
Dimension gtBounds = gameType.getPreferredSize();
Dimension gtlBounds = gameTypeLabel.getPreferredSize();
Dimension etBounds = entriesTable.getPreferredSize();
Dimension etstBounds = tableScrollPane.getPreferredSize();

finalPosition.setBounds( 110, 10, fpBounds.width, fpBounds.height );
finalPositionLabel.setBounds( 10, 10, fplBounds.width, fplBounds.height );
fieldSize.setBounds( 110, 30, fsBounds.width, fsBounds.height );
fieldSizeLabel.setBounds( 10, 30, fslBounds.width, fslBounds.height );
scrollPaneNotes.setBounds( 340, 10, scrollPaneNotesBounds.width, scrollPaneNotesBounds.height + 100 );
scrollPaneNotes.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
notes.setLineWrap( true );
notesLabel.setBounds( 270, 10, nlBounds.width, nlBounds.height );
gameType.setBounds( 110, 50, gtBounds.width, gtBounds.height );
gameTypeLabel.setBounds( 10, 50, gtlBounds.width, gtlBounds.height );

tableScrollPane.setBounds( 10, 180, 560, 210 );

resultsTab.add( finalPosition );
resultsTab.add( finalPositionLabel );
resultsTab.add( fieldSize );
resultsTab.add( fieldSizeLabel );
resultsTab.add( scrollPaneNotes );
resultsTab.add( notesLabel );
resultsTab.add( gameType );
resultsTab.add( gameTypeLabel );
resultsTab.add( tableScrollPane );
}


}





I can't understand why, with the second passing, the object is null.

Share this post


Link to post
Share on other sites
Quote:

private tabbedRegion m_tabbed = new tabbedRegion(m_dataBank);



this happens before the ctor is called.

move the initialisation of this member into the ctor.

so the value of m_dataBank, before the ctor of that class, is null.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement