We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,610 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Class Communication/ Main Method

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!

3
Contributors
10
Replies
15 Hours
Discussion Span
1 Year Ago
Last Updated
11
Views
Question
Answered
javaNooblet
Junior Poster
133 posts since Sep 2011
Reputation Points: 28
Solved Threads: 2
Skill Endorsements: 0

On which OS are u doing this?

Have u setup ur classpath properly?

stevanity
Posting Whiz
302 posts since Oct 2010
Reputation Points: 43
Solved Threads: 28
Skill Endorsements: 0

OSX, I wrote it in eclipse compiler.

javaNooblet
Junior Poster
133 posts since Sep 2011
Reputation Points: 28
Solved Threads: 2
Skill Endorsements: 0

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

stevanity
Posting Whiz
302 posts since Oct 2010
Reputation Points: 43
Solved Threads: 28
Skill Endorsements: 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)
javaNooblet
Junior Poster
133 posts since Sep 2011
Reputation Points: 28
Solved Threads: 2
Skill Endorsements: 0

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?

stultuske
Industrious Poster
4,366 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 23

could u post code of SquadPanel?

stevanity
Posting Whiz
302 posts since Oct 2010
Reputation Points: 43
Solved Threads: 28
Skill Endorsements: 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;
}
}
javaNooblet
Junior Poster
133 posts since Sep 2011
Reputation Points: 28
Solved Threads: 2
Skill Endorsements: 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.

stultuske
Industrious Poster
4,366 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 23

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!

javaNooblet
Junior Poster
133 posts since Sep 2011
Reputation Points: 28
Solved Threads: 2
Skill Endorsements: 0

Glad that uve got ur problem solved!

stevanity
Posting Whiz
302 posts since Oct 2010
Reputation Points: 43
Solved Threads: 28
Skill Endorsements: 0
Question Answered as of 1 Year Ago by stevanity and stultuske

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1018 seconds using 2.73MB