Error when trying to create a new instance of a class

Java Heap memory error

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

37 Replies - 2659 Views - Last Post: 19 January 2009 - 12:12 PM Rate Topic: -----

#1 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Error when trying to create a new instance of a class

Post icon  Posted 17 January 2009 - 02:18 PM

hello Everyone,

I'm having a little issue with this school assignment I have. Whenever I create a new instance of the Read Data class, my gui will no longer show up(but it will launch) and I get this error:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
	at Assignment1.Gui.<init>(Gui.java:20)
	at Assignment1.ReadData.<init>(ReadData.java:23)
	at Assignment1.Gui.<init>(Gui.java:54)
	at Assignment1.ReadData.<init>(ReadData.java:23)
	at Assignment1.Gui.<init>(Gui.java:54)
	at Assignment1.ReadData.<init>(ReadData.java:23)
	at Assignment1.Gui.<init>(Gui.java:54)
	at Assignment1.ReadData.<init>(ReadData.java:23)
	at Assignment1.Gui.<init>(Gui.java:54)
	at Assignment1.ReadData.<init>(ReadData.java:23)
	at Assignment1.Gui.<init>(Gui.java:54)
	at Assignment1.ReadData.<init>(ReadData.java:23)
	at Assignment1.Gui.<init>(Gui.java:54)
	at Assignment1.ReadData.<init>(ReadData.java:23)
	at Assignment1.Gui.<init>(Gui.java:54)
	at Assignment1.ReadData.<init>(ReadData.java:23)
	at Assignment1.Gui.<init>(Gui.java:54)
	at Assignment1.ReadData.<init>(ReadData.java:23)
	at Assignment1.Gui.<init>(Gui.java:54)
	at Assignment1.ReadData.<init>(ReadData.java:23)
	at Assignment1.Gui.<init>(Gui.java:54)
	at Assignment1.ReadData.<init>(ReadData.java:23)
	at Assignment1.Gui.<init>(Gui.java:54)
	at Assignment1.ReadData.<init>(ReadData.java:23)
	at Assignment1.Gui.<init>(Gui.java:54)
	at Assignment1.ReadData.<init>(ReadData.java:23)
	at Assignment1.Gui.<init>(Gui.java:54)
	at Assignment1.ReadData.<init>(ReadData.java:23)
	at Assignment1.Gui.<init>(Gui.java:54)
	at Assignment1.ReadData.<init>(ReadData.java:23)
	at Assignment1.Gui.<init>(Gui.java:54)
	at Assignment1.ReadData.<init>(ReadData.java:23)


I have no idea what this error is... But when I comment the ReadData data = new ReadData(); line out then the error dissapears and my gui comes back. Here is my code..

Gui class with embedded main
package Assignment1;

import javax.swing.*;
import javax.swing.table.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class Gui extends JFrame
{
	//instances of the GUI
	private final int WINDOW_HEIGHT=600;
	private final int WINDOW_WIDTH=1000;
	private JPanel centerPanel;
	private JPanel sidePanel;
	private JTable moviesTable;
	private Object[] fieldTitles = {"ID", "Title", "Category", "Directory", "Leading Actors", "Supporting Actors", "Running Time", "Rating", "Release Date"};
	private Object[][] tableData = new Object[100][9];
	private Object[] gamesFieldTitles = {"ID", "Title", "Category", "Developer", "Platform", "Rating", "Release Date"};
	private Object[][] gamesTableData = new Object[100][7];
	private JPanel searchPanel;
	private JButton searchButton;
	private JTextField searchBar;
	private JButton addInventoryButton;
	private JButton printAllInventory;
	private JButton removeInventoryItem;
	private JPanel bottomPanel;
	private JTableHeader tableHeader;
	private JTableHeader gamesTableHeader;
	private JScrollPane scrollPane;
	private JPanel displayPanel;
	private JRadioButton movies;
	private JRadioButton games;
	private ButtonGroup buttonGroup;
	private JTable gamesTable;
	private JScrollPane gamesScrollPane;
	private JTabbedPane tabArea;
	
	public Gui()
	{
		//set the title of the window
		setTitle("Video Store");
		setLayout(new BorderLayout());
		
		//create the panels
		centerPanel = new JPanel(new GridLayout(1,2));
		sidePanel = new JPanel(new GridLayout(2,1));
		searchPanel = new JPanel();
		bottomPanel = new JPanel();
		displayPanel = new JPanel(new FlowLayout());
		
		//ReadData data = new ReadData();
		
		//set the JTabbedPane
		tabArea = new JTabbedPane();
		
		//create the contents of the center area
		moviesTable = new JTable(tableData,fieldTitles);
		moviesTable.setShowGrid(true);
		moviesTable.setGridColor(Color.GRAY);
		
		//set the preferred width of the table columns that are too big
	   TableColumn column1 = moviesTable.getColumnModel().getColumn(1);
	   column1.setPreferredWidth(150);
	   
	   TableColumn column4 = moviesTable.getColumnModel().getColumn(4);
	   column4.setPreferredWidth(150);
	    
	   TableColumn column5 = moviesTable.getColumnModel().getColumn(5);
	   column5.setPreferredWidth(150);
	   
	   TableColumn column6 = moviesTable.getColumnModel().getColumn(6);
	   column6.setPreferredWidth(100);
	   
	   TableColumn column8 = moviesTable.getColumnModel().getColumn(8);
	   column8.setPreferredWidth(100);
		
		//create the table header and set it to a different color
		tableHeader = moviesTable.getTableHeader();
		
	   //add the display table to the scroll Pane
	   scrollPane = new JScrollPane(moviesTable);
	   moviesTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
	   
	   //add teh movies area to the tabbed pane
	   tabArea.addTab("Movies", scrollPane);
	   
		//create the games table
		gamesTable = new JTable(gamesTableData,gamesFieldTitles);
		gamesTable.setShowGrid(true);
		gamesTable.setGridColor(Color.GRAY);
		gamesTable.setVisible(true);
		
		//set the title column to a little bigger to hold larger titles
		TableColumn gamesTitleColumn = gamesTable.getColumnModel().getColumn(1);
		gamesTitleColumn.setPreferredWidth(150);
		
		//create the header for the games table
		gamesTableHeader = gamesTable.getTableHeader();
		
	   //add the games panel to a scrollPane
	   gamesScrollPane = new JScrollPane(gamesTable);
	   gamesTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		
	   //create the tab area to hold the tables
		tabArea.addTab("Games", gamesScrollPane);
		
		//allow the movies table to be visible
		moviesTable.setVisible(true);
		
		//create the display panel on the right to showcase information selected
		displayPanel.setSize(100, 300);
		
		//create the contents of the serach panel
		searchButton = new JButton("Search");
		searchBar = new JTextField(20);
		
		//create the radio buttons
		movies = new JRadioButton("Movies");
		games = new JRadioButton("Games");
		buttonGroup = new ButtonGroup();
		buttonGroup.add(movies);
		buttonGroup.add(games);
		movies.setSelected(true);
		
		//create the contents of the bottom panel
		addInventoryButton = new JButton("Add Item");
		removeInventoryItem = new JButton("Remove Item");
		printAllInventory = new JButton("Print All Inventory");
		
		//add the contents of the bottom Panel to the bottom Panel
		bottomPanel.add(addInventoryButton);
		bottomPanel.add(removeInventoryItem);
		bottomPanel.add(printAllInventory);
		
		//set the default close operation
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	   
		//set the movies table to be visible
		//add the contents of the center Panel
		centerPanel.add(tabArea);
		centerPanel.add(displayPanel);
		
		//add the center panel to the main frame
		add(centerPanel, BorderLayout.CENTER);
	   
	   //add the radio buttons to the side panel
		searchPanel.add(searchBar, BorderLayout.CENTER);
		searchPanel.add(searchButton, BorderLayout.EAST);
	    
		//set the size of the frame
		setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
		
		//add the items to the main window
		add(sidePanel, BorderLayout.WEST);
		add(searchPanel, BorderLayout.NORTH);
		add(bottomPanel, BorderLayout.SOUTH);
		
		//set the window to be visible
		setVisible(true);
	}
	
	/**
	 * method used to set the data of the display table
	 * @param obj can be any type of data
	 * @param row_index what row
	 * @param col_index what column
	 */
	public void SetData(Object obj, int rowIndex, int colIndex)
	{
		moviesTable.getModel().setValueAt(obj,rowIndex,colIndex);
	}
	
	//main to initialize the program
	public static void main(String[] args)
	{
		new Gui();
	}
}


Here is my Read Data class
/**
 * This class is going to be used to read the data from the file and store it in the movies object
 * or games object classes respectively
 */
package Assignment1;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;
import javax.swing.JOptionPane;

/**
 * @author markhazlett
 *
 */
public class ReadData extends Gui
{
	private int counter;
	private static ArrayList<MoviesObject> movies;
	private static ArrayList<GamesObject> games;
	
	public ReadData()
	{
		//default constructor
		counter = 0;
	}
	
	/**
	 * This method counts the size of the file to determine how big the array of objects should be
	 * @return Returns an int for the number of lines in the file
	 */
	public int countFileSize()
	{
		try
		{
			FileReader reader = new FileReader("movies-games.txt");
			Scanner input = new Scanner(reader);
			
			while(input.hasNext())
			{
				counter++;
			}
			
			input.close();
		} 
		catch (FileNotFoundException e)
		{
			JOptionPane.showMessageDialog(null, "The file you requested was not found, " +
					"please make sure it is in the proper directory");
		}
		return counter;
	}
	
	/**
	 * This method is going to be used to determine if the ID number ends with a 0-4, if it does
	 * then it will store that line in a movies object
	 */
	public void loadArrayList()
	{
		try
		{
			//create the method for reading from the file
			FileReader reader = new FileReader("movies-games.txt");
			Scanner input = new Scanner(reader);
			
			for(int index = 0; index < counter; index++)
			{
				String tempString = input.nextLine();
				
				while(tempString != null)
				{
					StringTokenizer tokenizer = new StringTokenizer(tempString, ";");
					
					//create a string with the id number
					String id = tokenizer.nextToken();
					//parse it to an integer
					int idNumber = Integer.parseInt(id);
					//find the last digit of the id number to compare
					int lastDigit = idNumber % 10;
					
					//if the last digit of the number is 0,1,2,3 or 4 then create that string into the movies array list
					if(lastDigit == 0 || lastDigit == 1 || lastDigit == 2 || lastDigit == 3 || lastDigit == 4)
					{
						//create a movies object specific counter to ensure that it always gets input directly after the last one
						int moviesCounter = 0;
						
						//create the movies object from the parse
						MoviesObject object = new MoviesObject(idNumber, tokenizer.nextToken(), tokenizer.nextToken(), tokenizer.nextToken(), 
								tokenizer.nextToken(), tokenizer.nextToken(), Double.parseDouble(tokenizer.nextToken()), tokenizer.nextToken(), 
								tokenizer.nextToken());
						
						//add the movies object to the array list
						movies.add(moviesCounter, object);
						//increase the movies counter by one to ensure the next object is input directly after the previous one
						moviesCounter++;
					}
					//else it's not then create it into the games array list
					else
					{
						//create a games object specific counter to ensure they are all in order
						int gamesCounter = 0;
						
						//create a games object based on the input
						GamesObject gamesObject = new GamesObject(idNumber, tokenizer.nextToken(), tokenizer.nextToken(), tokenizer.nextToken(), 
								tokenizer.nextToken(), tokenizer.nextToken(), tokenizer.nextToken());
						
						//add the games object to the array list of games objects
						games.add(gamesCounter, gamesObject);
						
						//increase the games counter by one to ensure they are all in order
						gamesCounter++;
					}
					
				}
			}
		
		}
		catch(IOException e)
		{
			JOptionPane.showMessageDialog(null, "An Error Occured in locating the file, please ensure the file is in the correct directory");
		}
	}
	
	/**
	 * 
	 * @return
	 */
	public int returnMoviesObjectSize()
	{
		return movies.size();
	}
	
	/**
	 * 
	 * @return
	 */
	public int returnGamesObjectSize()
	{
		return games.size();
	}
	
	/**
	 * 
	 * @return
	 */
	public static ArrayList<MoviesObject> returnMoviesObject()
	{
		return movies;
	}
	
	/**
	 * 
	 * @return
	 */
	public static ArrayList<GamesObject> returnGamesObject()
	{
		return games;
	}
}


and then here is my movies and games object classes but they just have getters and setters and a toString method

/**
 * This is the movies object class
 * It holds all the data for each movies object
 */
package Assignment1;

/**
 * @author markhazlett
 *
 */
public class MoviesObject
{
	//attributes of the movies class
	private int itemId;
	private String title;
	private String catagory;
	private String director;
	private String leadingActor;
	private String supportingActor;
	private double runningTime;
	private String rating;
	private String date;
	
	public MoviesObject()
	{
		//default constructor
	}
	
	/**
	 * @param catagory is the category of the movie
	 * @param director is the director of the movie
	 * @param itemId is the item id from the database
	 * @param leadingActor is the leading actor of the movie
	 * @param rating is the rating of the movie
	 * @param runningTime is the running time of the movie
	 * @param supportingActor is the supporting actor of the movie
	 * @param title is the title of the movie
	 * @param date is the release date for the movie
	 */
	public MoviesObject(int itemId, String title, String catagory, String director, String leadingActor, 
			String supportingActor, double runningTime, String rating, String date)
	{
		this.catagory = catagory;
		this.director = director;
		this.itemId = itemId;
		this.leadingActor = leadingActor;
		this.rating = rating;
		this.runningTime = runningTime;
		this.supportingActor = supportingActor;
		this.title = title;
		this.date = date;
	}

	/**
	 * @return the itemId
	 */
	public int getItemId()
	{
		return itemId;
	}
	/**
	 * @param itemId the itemId to set
	 */
	public void setItemId(int itemId)
	{
		this.itemId = itemId;
	}
	/**
	 * @return the title
	 */
	public String getTitle()
	{
		return title;
	}
	/**
	 * @param title the title to set
	 */
	public void setTitle(String title)
	{
		this.title = title;
	}
	/**
	 * @return the category
	 */
	public String getCategory()
	{
		return catagory;
	}
	/**
	 * @param category the category to set
	 */
	public void setCategory(String category)
	{
		this.catagory = category;
	}
	/**
	 * @return the director
	 */
	public String getDirector()
	{
		return director;
	}
	/**
	 * @param director the director to set
	 */
	public void setDirector(String director)
	{
		this.director = director;
	}
	/**
	 * @return the leadingActor
	 */
	public String getLeadingActor()
	{
		return leadingActor;
	}
	/**
	 * @param leadingActor the leadingActor to set
	 */
	public void setLeadingActor(String leadingActor)
	{
		this.leadingActor = leadingActor;
	}
	/**
	 * @return the supportingActor
	 */
	public String getSupportingActor()
	{
		return supportingActor;
	}
	/**
	 * @param supportingActor the supportingActor to set
	 */
	public void setSupportingActor(String supportingActor)
	{
		this.supportingActor = supportingActor;
	}
	/**
	 * @return the runningTime
	 */
	public double getRunningTime()
	{
		return runningTime;
	}
	/**
	 * @param runningTime the runningTime to set
	 */
	public void setRunningTime(double runningTime)
	{
		this.runningTime = runningTime;
	}
	/**
	 * @return the rating
	 */
	public String getRating()
	{
		return rating;
	}
	/**
	 * @param rating the rating to set
	 */
	public void setRating(String rating)
	{
		this.rating = rating;
	}
	
	/**
	 * 
	 * @param date
	 */
	public void setDate(String date)
	{
		this.date = date;
	}
	/**
	 * 
	 * @return
	 */
	public String returnDate()
	{
		return date;
	}
	
	/**
	 * overides the to String method
	 */
	public String toString()
	{
		return "Item Id:         " + itemId +
				"Title:            " + title +
				"Catagory:         " + catagory +
				"Director:         " + director +
				"Leading Actor:    " + leadingActor +
				"Supporting Actor: " + supportingActor +
				"Running Time:     " + runningTime +
				"Rating:           " + rating +
				"Release Date:     " + date;
	}
	
	
}



and Games...
/**
 * This is the class for storing the games objects information
 */
package Assignment1;

/**
 * @author markhazlett
 *
 */
public class GamesObject
{
	//attributes
	private int itemId;
	private String title;
	private String catagory;
	private String developer;
	private String platform;
	private String rating;
	private String date;
	
	public GamesObject()
	{
		//default constructor
	}
	
	/**
	 * @param catagory is the catagory of the game
	 * @param developer is the developer of the game
	 * @param itemId is the item id from the database
	 * @param platform is the platform that the game runs on
	 * @param rating is the rating of the game
	 * @param title is the title of the game
	 */
	public GamesObject(int itemId, String title, String catagory, String developer, String platform,
			String rating, String date)
	{
		this.catagory = catagory;
		this.developer = developer;
		this.itemId = itemId;
		this.platform = platform;
		this.rating = rating;
		this.title = title;
		this.date = date;
	}
	
	/**
	 * @return the itemId
	 */
	public int getItemId()
	{
		return itemId;
	}
	/**
	 * @param itemId the itemId to set
	 */
	public void setItemId(int itemId)
	{
		this.itemId = itemId;
	}
	/**
	 * @return the title
	 */
	public String getTitle()
	{
		return title;
	}
	/**
	 * @param title the title to set
	 */
	public void setTitle(String title)
	{
		this.title = title;
	}
	/**
	 * @return the catagory
	 */
	public String getCatagory()
	{
		return catagory;
	}
	/**
	 * @param catagory the catagory to set
	 */
	public void setCatagory(String catagory)
	{
		this.catagory = catagory;
	}
	/**
	 * @return the developer
	 */
	public String getDeveloper()
	{
		return developer;
	}
	/**
	 * @param developer the developer to set
	 */
	public void setDeveloper(String developer)
	{
		this.developer = developer;
	}
	/**
	 * @return the platform
	 */
	public String getPlatform()
	{
		return platform;
	}
	/**
	 * @param platform the platform to set
	 */
	public void setPlatform(String platform)
	{
		this.platform = platform;
	}
	/**
	 * @return the rating
	 */
	public String getRating()
	{
		return rating;
	}
	/**
	 * @param rating the rating to set
	 */
	public void setRating(String rating)
	{
		this.rating = rating;
	}

	/**
	 * @return the date
	 */
	public String getDate()
	{
		return date;
	}

	/**
	 * @param date the date to set
	 */
	public void setDate(String date)
	{
		this.date = date;
	}
	
	/**
	 * overides the to string method
	 */
	public String toString()
	{
		return "Item Id:         " + itemId +
				"Title:            " + title +
				"Catagory:         " + catagory +
				"Developer:        " + developer +
				"Platform:         " + platform +
				"Rating:           " + rating +
				"Release Date:     " + date;
	}
}


I know it's alot of code... *Apologies* but I have never seen this error yet and I can't figure out the issue. Any help will be greatly appreciated. Cheers :^: :D

Is This A Good Question/Topic? 0
  • +

Replies To: Error when trying to create a new instance of a class

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,596
  • Joined: 16-October 07

Re: Error when trying to create a new instance of a class

Posted 17 January 2009 - 02:33 PM

This is obviously an infinite loop error. The trick is finding it...

public class ReadData extends Gui {
	public ReadData() {
		// note, this implicitly calls super()
		// call Gui
	}
}

public Gui() {
	// call this
	// which 
	ReadData data = new ReadData();
	// you never get here, you're in an infinite loop
}


Was This Post Helpful? 0
  • +
  • -

#3 javaguy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 17-January 09

Re: Error when trying to create a new instance of a class

Posted 17 January 2009 - 02:34 PM

Holy inheritance nightmare, Batman!

Your ReadData class extends your Gui class. That means that the VERY FIRST line in the ReadData class constructor is a call to the default constructor (since you don't explicitly call a different one) in class Gui. That one in turn calls the ReadData one again when it comes across the "new ReadData()" statement, and on and on and .... This goes on until you have filled the heap.

Since your ReadData class really doesn't do any gui type things, but is mainly a data class, just delete the "extends Gui" from the class declaration. BTW, you usually would not have more than one JFrame class/subclass in a single application anyway.
Was This Post Helpful? 1
  • +
  • -

#4 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: Error when trying to create a new instance of a class

Posted 17 January 2009 - 02:38 PM

View Postbaavgai, on 17 Jan, 2009 - 01:33 PM, said:

This is obviously an infinite loop error. The trick is finding it...

public class ReadData extends Gui {
	public ReadData() {
		// note, this implicitly calls super()
		// call Gui
	}
}

public Gui() {
	// call this
	// which 
	ReadData data = new ReadData();
	// you never get here, you're in an infinite loop
}




Ahhhhhhh! I see, i get it now, I think i have found my infinite loop. In my ReadData class under the loadArrayList() method, I actually don't increment the arraylist position to a point at which it will end the loop! Thank you!


View Postjavaguy, on 17 Jan, 2009 - 01:34 PM, said:

Holy inheritance nightmare, Batman!

Your ReadData class extends your Gui class. That means that the VERY FIRST line in the ReadData class constructor is a call to the default constructor (since you don't explicitly call a different one) in class Gui. That one in turn calls the ReadData one again when it comes across the "new ReadData()" statement, and on and on and .... This goes on until you have filled the heap.

Since your ReadData class really doesn't do any gui type things, but is mainly a data class, just delete the "extends Gui" from the class declaration. BTW, you usually would not have more than one JFrame class/subclass in a single application anyway.



Haha well that actually did fix the heap error! Very nice! And what exactly do you mean by you will usually have only one JFrame class/subclass in a single app anyway? isn't that what I have(of course without the extends GUI statement)?
Was This Post Helpful? 0
  • +
  • -

#5 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: Error when trying to create a new instance of a class

Posted 17 January 2009 - 02:43 PM

And just as a side note, Now i got rid of the java heap error. However now when I try and add the methods to the gui class like so...

		//create and instance of the read data class to use
		ReadData data = new ReadData();
		data.countFileSize();



the gui will no longer launch? Any thoughts?
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,596
  • Joined: 16-October 07

Re: Error when trying to create a new instance of a class

Posted 17 January 2009 - 02:44 PM

This was fun. I did a quick example:
class Foo {
	public static int limit = 0;
	public Foo() {
		System.out.println("Foo constructor: " + Foo.limit);
		Foo.limit++;
		if (Foo.limit < 5) {
			new Bar(Foo.limit);
		}
	}
}

class Bar extends Foo {
	public Bar(int current) {
		System.out.println("Bar constructor: " + current);
	}
}

public class Test {
	public static void main(String[] args) {
		new Foo();
	}
}



results:
Foo constructor: 0
Foo constructor: 1
Foo constructor: 2
Foo constructor: 3
Foo constructor: 4
Bar constructor: 4
Bar constructor: 3
Bar constructor: 2
Bar constructor: 1


Was This Post Helpful? 0
  • +
  • -

#7 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: Error when trying to create a new instance of a class

Posted 17 January 2009 - 02:48 PM

View Postbaavgai, on 17 Jan, 2009 - 01:44 PM, said:

This was fun. I did a quick example:
class Foo {
	public static int limit = 0;
	public Foo() {
		System.out.println("Foo constructor: " + Foo.limit);
		Foo.limit++;
		if (Foo.limit < 5) {
			new Bar(Foo.limit);
		}
	}
}

class Bar extends Foo {
	public Bar(int current) {
		System.out.println("Bar constructor: " + current);
	}
}

public class Test {
	public static void main(String[] args) {
		new Foo();
	}
}




Ok, thank you guys for the quick responses, I'm kinda wondering about that code though. I'm not sure I understand why you are putting Foo.Limit++ and not just limit++?
Was This Post Helpful? 0
  • +
  • -

#8 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Error when trying to create a new instance of a class

Posted 17 January 2009 - 02:50 PM

You (and me :) ) are running out of memory because:
public class ReadData extends Gui

When you call ReadData data = new ReadData(); in Gui()'s constructor, ReadData()'s constructor implicitly calls the super class's constructor - loop!

Remove the extends Gui.
Was This Post Helpful? 1
  • +
  • -

#9 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: Error when trying to create a new instance of a class

Posted 17 January 2009 - 02:56 PM

View Postn8wxs, on 17 Jan, 2009 - 01:50 PM, said:

You (and me :) ) are running out of memory because:
public class ReadData extends Gui

When you call ReadData data = new ReadData(); in Gui()'s constructor, ReadData()'s constructor implicitly calls the super class's constructor - loop!

Remove the extends Gui.



That definitely took care of the out of heap error, but now when I try to call the methods of the Read Data class my gui will no longer launch.. any ideas?
Was This Post Helpful? 0
  • +
  • -

#10 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Error when trying to create a new instance of a class

Posted 17 January 2009 - 03:00 PM

You have no ActionListener()s for the buttons.
Was This Post Helpful? 0
  • +
  • -

#11 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: Error when trying to create a new instance of a class

Posted 17 January 2009 - 03:02 PM

View Postn8wxs, on 17 Jan, 2009 - 02:00 PM, said:

You have no ActionListener()s for the buttons.



That is true, I haven't actually got to that yet. Do they need to be implemented in order for the method to be called though? I added the action listeners for all the buttons and i'm getting the same issue.

This post has been edited by markhazlett9: 17 January 2009 - 03:04 PM

Was This Post Helpful? 0
  • +
  • -

#12 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,596
  • Joined: 16-October 07

Re: Error when trying to create a new instance of a class

Posted 17 January 2009 - 03:03 PM

If you want to use ReadData, rather than Gui, do this: take "setVisible(true)" out of the constructor. Then call it after you call the form. e.g.
public static void main(String[] args) {
	//JFrame frm = new Gui();
	JFrame frm = new ReadData();
	// set the window to be visible
	frm.setVisible(true);
}


Was This Post Helpful? 0
  • +
  • -

#13 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: Error when trying to create a new instance of a class

Posted 17 January 2009 - 03:08 PM

View Postbaavgai, on 17 Jan, 2009 - 02:03 PM, said:

If you want to use ReadData, rather than Gui, do this: take "setVisible(true)" out of the constructor. Then call it after you call the form. e.g.
public static void main(String[] args) {
	//JFrame frm = new Gui();
	JFrame frm = new ReadData();
	// set the window to be visible
	frm.setVisible(true);
}




I gave what I think you are saying a try... Here is what i did...
	//main to initialize the program
	public static void main(String[] args)
	{
		JFrame frame = new Gui();
		
		frame.setVisible(true);
	}


and then commented out the set visible to be true in the constructor, but i'm still getting the same issue.
Was This Post Helpful? 0
  • +
  • -

#14 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Error when trying to create a new instance of a class

Posted 17 January 2009 - 03:12 PM

Using "Find Usages" I don't see any calls to any of the methods in ReadData in the code you have posted.
Was This Post Helpful? 0
  • +
  • -

#15 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: Error when trying to create a new instance of a class

Posted 17 January 2009 - 03:16 PM

ahh, right right sorry, I updated the code after I posted and totally forgot to post a new copy. here is the new copy. The call is directly below the created instance of ReadData()

package Assignment1;

import javax.swing.*;
import javax.swing.table.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class Gui extends JFrame
{
	//instances of the GUI
	private final int WINDOW_HEIGHT=600;
	private final int WINDOW_WIDTH=1000;
	private JPanel centerPanel;
	private JPanel sidePanel;
	private JTable moviesTable;
	private Object[] fieldTitles = {"ID", "Title", "Category", "Directory", "Leading Actors", "Supporting Actors", "Running Time", "Rating", "Release Date"};
	private Object[][] tableData = new Object[100][9];
	private Object[] gamesFieldTitles = {"ID", "Title", "Category", "Developer", "Platform", "Rating", "Release Date"};
	private Object[][] gamesTableData = new Object[100][7];
	private JPanel searchPanel;
	private JButton searchButton;
	private JTextField searchBar;
	private JButton addInventoryButton;
	private JButton printAllInventory;
	private JButton removeInventoryItem;
	private JPanel bottomPanel;
	private JTableHeader tableHeader;
	private JTableHeader gamesTableHeader;
	private JScrollPane scrollPane;
	private JPanel displayPanel;
	private JRadioButton movies;
	private JRadioButton games;
	private ButtonGroup buttonGroup;
	private JTable gamesTable;
	private JScrollPane gamesScrollPane;
	private JTabbedPane tabArea;
	private JSplitPane splitDisplayPane;
	
	public Gui()
	{
		//set the title of the window
		setTitle("Video Store");
		setLayout(new BorderLayout());
		
		//create the panels
		centerPanel = new JPanel(new GridLayout(1,2));
		sidePanel = new JPanel(new GridLayout(2,1));
		searchPanel = new JPanel();
		bottomPanel = new JPanel();
		displayPanel = new JPanel(new FlowLayout());
		splitDisplayPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
		
		//create and instance of the read data class to use
		ReadData data = new ReadData();
		//data.countFileSize();
		
		//set the JTabbedPane
		tabArea = new JTabbedPane();
		
		//create the contents of the center area
		moviesTable = new JTable(tableData,fieldTitles);
		moviesTable.setShowGrid(true);
		moviesTable.setGridColor(Color.GRAY);
		
		//set the preferred width of the table columns that are too big
	   TableColumn column1 = moviesTable.getColumnModel().getColumn(1);
	   column1.setPreferredWidth(150);
	   
	   TableColumn column4 = moviesTable.getColumnModel().getColumn(4);
	   column4.setPreferredWidth(150);
	    
	   TableColumn column5 = moviesTable.getColumnModel().getColumn(5);
	   column5.setPreferredWidth(150);
	   
	   TableColumn column6 = moviesTable.getColumnModel().getColumn(6);
	   column6.setPreferredWidth(100);
	   
	   TableColumn column8 = moviesTable.getColumnModel().getColumn(8);
	   column8.setPreferredWidth(100);
		
		//create the table header and set it to a different color
		tableHeader = moviesTable.getTableHeader();
		
	   //add the display table to the scroll Pane
	   scrollPane = new JScrollPane(moviesTable);
	   moviesTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
	   moviesTable.setRowSelectionAllowed(true);
	   
	   //add teh movies area to the tabbed pane
	   tabArea.addTab("Movies", scrollPane);
	   
		//create the games table
		gamesTable = new JTable(gamesTableData,gamesFieldTitles);
		gamesTable.setShowGrid(true);
		gamesTable.setGridColor(Color.GRAY);
		gamesTable.setVisible(true);
		
		//set the title column to a little bigger to hold larger titles
		TableColumn gamesTitleColumn = gamesTable.getColumnModel().getColumn(1);
		gamesTitleColumn.setPreferredWidth(150);
		
		//create the header for the games table
		gamesTableHeader = gamesTable.getTableHeader();
		
	   //add the games panel to a scrollPane
	   gamesScrollPane = new JScrollPane(gamesTable);
	   gamesTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
	   gamesTable.setRowSelectionAllowed(true);
		
	   //create the tab area to hold the tables
		tabArea.addTab("Games", gamesScrollPane);
		
		//allow the movies table to be visible
		moviesTable.setVisible(true);
		
		//create the display panel on the right to showcase information selected
		displayPanel.setSize(100, 300);
		
		//create the contents of the serach panel
		searchButton = new JButton("Search");
		searchBar = new JTextField(20);
		
		//create the radio buttons
		movies = new JRadioButton("Movies");
		games = new JRadioButton("Games");
		buttonGroup = new ButtonGroup();
		buttonGroup.add(movies);
		buttonGroup.add(games);
		movies.setSelected(true);
		
		//create the contents of the bottom panel
		addInventoryButton = new JButton("Add Item");
		removeInventoryItem = new JButton("Remove Item");
		printAllInventory = new JButton("Print All Inventory");
		
		//add the contents of the bottom Panel to the bottom Panel
		bottomPanel.add(addInventoryButton);
		bottomPanel.add(removeInventoryItem);
		bottomPanel.add(printAllInventory);
		
		//set the default close operation
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	   
		//set the movies table to be visible
		//add the contents of the center Panel
		splitDisplayPane.add(tabArea);
		splitDisplayPane.add(displayPanel);
		
		//add the center panel to the main frame
		add(splitDisplayPane, BorderLayout.CENTER);
	   
	   //add the radio buttons to the side panel
		searchPanel.add(searchBar, BorderLayout.CENTER);
		searchPanel.add(searchButton, BorderLayout.EAST);
	    
		//set the size of the frame
		setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
		
		//add the items to the main window
		add(sidePanel, BorderLayout.WEST);
		add(searchPanel, BorderLayout.NORTH);
		add(bottomPanel, BorderLayout.SOUTH);
		
		//set the window to be visible
		//setVisible(true);
	}
	
	/**
	 * method used to set the data of the display table
	 * @param obj can be any type of data
	 * @param row_index what row
	 * @param col_index what column
	 */
	public void SetData(Object obj, int rowIndex, int colIndex)
	{
		moviesTable.getModel().setValueAt(obj,rowIndex,colIndex);
	}
	
	public class SearchButtonActionListener implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{	
			
		}
	}
	
	public class AddButtonActionListener implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{	
			
		}
	}
	
	public class RemoveButtonActionListener implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{	
			
		}
	}
	
	public class DisplayAllButtonActionListener implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{	
			
		}
	}
	
	//main to initialize the program
	public static void main(String[] args)
	{
		JFrame frame = new Gui();
		
		frame.setVisible(true);
	}
}


My apologies again! Cheers
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3