1,105,416 Community Members

Class Communication/ Main Method

Member Avatar
javaNooblet
Junior Poster
133 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi all,

I have a main GUI class as seen here:

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

/*
 * Class is to communicate with various other classes in the program to
 * create a roster for user based on their selection on GUI.
 */
public class MainGUI extends JFrame
{
	private static final long serialVersionUID = 1L;
	//declares window width and height for program
	final int WINDOW_WIDTH = 1000;
	final int WINDOW_HEIGHT= 1000;
	
	private PlatoonPanel pp;
	private SquadPanel sp;
	private ManualPanel mp;
	private ActionButtons ab;
	private static Container pane;

	public MainGUI()
	{
		//sets title
		setTitle("Roster Generator Version 0.0.1");
		//sets window width and height
		setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
		//sets default close operation
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		pane = getContentPane();
		
		//creates new class objects
		pp = new PlatoonPanel();
		sp = new SquadPanel();
		mp = new ManualPanel();
		ab = new ActionButtons();
		
		//calls buildGUI() method
		buildGUI();
		//sets visible to true
		setVisible(true);
	}
	
	//method that combines all the other panels from other classes and
	//combines them into one
	public void buildGUI(){
		//sets border layout for main panel
		pane.setLayout(new BorderLayout());
		//adds other panels to this main panel
		pane.add(pp.getPlatoonRosterPanel(), BorderLayout.WEST);
		pane.add(sp.getSquadRosterPanel(), BorderLayout.CENTER);
		pane.add(mp.getManualRosterPanel(), BorderLayout.EAST);
		pane.add(ab.getButtonPanel(), BorderLayout.SOUTH);
		
		}
}

And trying to have a driver class here for the main method:

/*
 * Driver Class for program
 */
public class MainDriver {

	public static void main(String[] args) {
		
		MainGUI mg = new MainGUI();

	}

}

How can I communicate between these classes so that I do not receive this error:

Exception in thread "main" java.lang.NoSuchMethodError: main

Thanks for any help!

Member Avatar
stevanity
Posting Whiz
304 posts since Oct 2010
Reputation Points: 4 [?]
Q&As Helped to Solve: 28 [?]
Skill Endorsements: 0 [?]
 
0
 

On which OS are u doing this?

Have u setup ur classpath properly?

Member Avatar
javaNooblet
Junior Poster
133 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

OSX, I wrote it in eclipse compiler.

Member Avatar
stevanity
Posting Whiz
304 posts since Oct 2010
Reputation Points: 4 [?]
Q&As Helped to Solve: 28 [?]
Skill Endorsements: 0 [?]
 
0
 

You need to setup ur classpath properly. Thats the problem.

Member Avatar
javaNooblet
Junior Poster
133 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

Ok I'm not sure if I did it correctly, but I right clicked on the project in the project explorer and followed the build path option, now when I run the program I get:

Exception in thread "main" java.lang.NullPointerException
	at java.awt.Container.addImpl(Container.java:1045)
	at java.awt.Container.add(Container.java:365)
	at SquadPanel.<init>(SquadPanel.java:49)
	at MainGUI.<init>(MainGUI.java:35)
	at MainDriver.main(MainDriver.java:8)
Member Avatar
stultuske
Posting Expert
5,332 posts since Jan 2007
Reputation Points: 938 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
1
 

looks like you're having a nullpointerexception in your MainGui class, on line 35. might want to check that out. something you're missing in the constructor of SquadPanel, perhaps?

Member Avatar
stevanity
Posting Whiz
304 posts since Oct 2010
Reputation Points: 4 [?]
Q&As Helped to Solve: 28 [?]
Skill Endorsements: 0 [?]
 
0
 

could u post code of SquadPanel?

Member Avatar
javaNooblet
Junior Poster
133 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

Here is SquadPanel Class:

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
/*
 * Class is to create a panel to be added to MainGUI class. Panel is to contain four 
 * checkbox options for a squad roster. User can select any combination of checkboxes 
 * to have a roster printed out when a button is clicked on GUI to generate roster.
 */
public class SquadPanel {

//creates data fields for checkboxes, borders, and panels.
private JCheckBox firstSquadRosterCB, secondSquadRosterCB, thirdSquadRosterCB, fourthSquadRosterCB, 
				  excludeRankCB, excludeSocCB, excludeBloodTypeCB, excludeRifleTypeCB,excludeRifleSerialCB, 
				  excludePeqSerialCB, excludeAcogSerialCB;
private TitledBorder squadSelectBorder, exclusionBorder, squadRosterBorder;
private JPanel squadRosterPanel, squadSelectPanel, exclusionPanel;
//private SquadPanel sp;

//constructor
public SquadPanel(){
	
	//creates new panel objects
	squadRosterPanel = new JPanel();
	squadSelectPanel = new JPanel();
	exclusionPanel = new JPanel();
	
	//creates new checkbox objects 
	firstSquadRosterCB = new JCheckBox("First Squad");
	secondSquadRosterCB = new JCheckBox("Second Squad");
	thirdSquadRosterCB = new JCheckBox("Third Squad");
	fourthSquadRosterCB = new JCheckBox("Fourth Squad");
	
	//sets grid layout for squad selection panel
	squadSelectPanel.setLayout(new GridLayout(4,1));
	//adds squad check boxes to squad panel
	squadSelectPanel.add(firstSquadRosterCB);
	squadSelectPanel.add(secondSquadRosterCB);
	squadSelectPanel.add(thirdSquadRosterCB);
	squadSelectPanel.add(fourthSquadRosterCB);
	
	//creates new border object
	squadSelectBorder = new TitledBorder("Select Squads for Roster");
	//sets border to squad select panel
	squadSelectPanel.setBorder(squadSelectBorder);
	
	//sets grid layout for excluded information panel
	exclusionPanel.setLayout(new GridLayout(7,1));
	//adds checks boxes to excluded panel 
	exclusionPanel.add(excludeRankCB);
	exclusionPanel.add(excludeSocCB);
	exclusionPanel.add(excludeBloodTypeCB);
	exclusionPanel.add(excludeRifleTypeCB);
	exclusionPanel.add(excludeRifleSerialCB);
	exclusionPanel.add(excludePeqSerialCB);
	exclusionPanel.add(excludeAcogSerialCB);
	
	//creates new border object
	exclusionBorder = new TitledBorder("Select Information to be excluded");
	//sets border to exclusion panel
	exclusionPanel.setBorder(exclusionBorder);
	
	//sets grid layout for squad roster panel
	squadRosterPanel.setLayout(new GridLayout(2,1));
	//adds squad select panel and exclusion panel to squad roster panel
	squadRosterPanel.add(squadSelectPanel);
	squadRosterPanel.add(exclusionPanel);
	
	//creates new border object
	squadRosterBorder = new TitledBorder("Squad Roster");
	//sets border to squad roster panel
	squadRosterPanel.setBorder(squadRosterBorder);
}	
//getter that returns squad roster panel
public JPanel getSquadRosterPanel() {
	return squadRosterPanel;
}
}
Member Avatar
stultuske
Posting Expert
5,332 posts since Jan 2007
Reputation Points: 938 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

on line 49 you're trying to add a excludeRankCB variable to the exclusionPanel, but you didn't instantiate one yet.
if you just put

JCheckBox excludeRankCB;

it will have default a null-value, so if you try to add this, you'll get a nullpointerexception.

Member Avatar
javaNooblet
Junior Poster
133 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

OH JEEZE!!!!!! Of all the things I messed up on it was one that simple(to make it worse I forgot to create all of the excludeCB objects.....) Thanks for the help guys! Yeah I decided to add that group of JCheckBoxes to the program after I planned the GUI and I guess I just forgot a step. But the GUI runs now! Time for the hard part now. Thanks again!

Member Avatar
stevanity
Posting Whiz
304 posts since Oct 2010
Reputation Points: 4 [?]
Q&As Helped to Solve: 28 [?]
Skill Endorsements: 0 [?]
 
0
 

Glad that uve got ur problem solved!

Question Answered as of 2 Years Ago by stevanity and stultuske
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: