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

New Topic/Question
Reply



MultiQuote







|