Hello if anyone can help I would like to know why I get this error when executing.. exception in thread "main" java.lang.NuSuchMethodError: main

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GridLayoutFrame extends JPanel
 {
// These are the components
	private JButton FindB,AddB,RemoveB;
	private JLabel theOne,theTwo,theThree,theFour,studentID,theMidTerm,theMidterm,theFinal1,theFinal2,FinalG1,FinalG2,FGrade2;
	private JList studentNumbers;
	private JTextField mark1,mark2,mark3,mark4,markM,MarksF,FinalG;
 Integer[] students;
	public JList getActionList() {return studentNumbers;}
 public JTextField getMark1() {return mark1;}
 public JTextField getMark2() {return mark2;}
 public JTextField getMark3() {return mark3;}
 public JTextField getMark4() {return mark4;}
 public JTextField getMarkM() {return markM;}
 public JTextField getMarkF() {return MarksF;}
 public JTextField getFinalGrade() {return FinalG;}
public GridLayoutFrame()
 {
 setLayout(new GridLayout(4,1,0,0));
 add(theStudentList());
 add(theTextField());
 add(theLabel());
	add(theButton());
}
public JPanel theStudentList()
{
  JPanel sIdpanel = new JPanel();
  sIdpanel.setLayout(null);
  sIdpanel.setSize(430,353);
  sIdpanel.setBorder(BorderFactory.createLineBorder  (Color.GRAY));
  JLabel studentID = new JLabel("Student ID");
  studentID.setSize(153,23);
  studentID.setLocation(18,12);
  studentID.setFont(new Font("Times", Font.BOLD, 17));
  sIdpanel.add(studentID);
  JList studentNumbers = new JList();
  JScrollPane scrollPane = new JScrollPane(studentNumbers,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
  scrollPane.setLocation(15,38);
  scrollPane.setSize(390,149);
  sIdpanel.add(scrollPane);
  return sIdpanel;
}
public JPanel theLabel()
{
 JPanel theLabel2 = new JPanel();
  theLabel2.setLayout(new GridLayout(1,8));
  theLabel2.setLocation(30,500);
  theLabel2.setSize(100,53);
	 theLabel2.setBorder(BorderFactory.createLineBorder  (Color.GRAY));
  theOne = new JLabel("A1");
  theOne.setFont(new Font("Times", Font.BOLD, 16));
  theLabel2.add(theOne);

  theTwo = new JLabel("A2");
  theTwo.setFont(new Font("Times", Font.BOLD, 16));
  theLabel2.add(theTwo);
  theThree = new JLabel("A3");
  theThree.setFont(new Font("Times", Font.BOLD, 16));
  theLabel2.add(theThree);
  theFour = new JLabel("A4");
  theFour.setFont(new Font("Times", Font.BOLD, 16));
  theLabel2.add(theFour);
  theMidTerm = new JLabel("Midterm");
  theMidTerm.setFont(new Font("Times", Font.BOLD, 17));
  theLabel2.add(theMidTerm);
  theMidterm = new JLabel("Exam");
  theMidterm.setFont(new Font("Times", Font.BOLD, 17));
  theLabel2.add(theMidterm);
  theFinal1 = new JLabel("Final");
  theFinal1.setFont(new Font("Times", Font.BOLD, 17));
  theLabel2.add(theFinal1);
  theFinal2 = new JLabel("Exam");
  theFinal2.setFont(new Font("Times", Font.BOLD, 17));
  theLabel2.add(theFinal2);
  FinalG1 = new JLabel("Final");
  FinalG1.setFont(new Font("Times", Font.BOLD, 17));
  theLabel2.add(FinalG1);

  FGrade2  = new JLabel("Grade");
  FGrade2.setFont(new Font("Times", Font.BOLD, 17));
  theLabel2.add(FGrade2);
return theLabel2;
}
public JPanel theTextField()
{
 JPanel markValuesPanel = new JPanel();
  markValuesPanel.setLayout(new GridLayout(1,7,5,5));
  markValuesPanel.setSize(10,10);
  markValuesPanel.setLocation(18,100);
  mark1 = new JTextField("25");
  markValuesPanel.add(mark1);
  mark2 = new JTextField("14.5");
  markValuesPanel.add(mark2);
  mark3 = new JTextField("31.5");
  markValuesPanel.add(mark3);
  mark4 = new JTextField("30");
  markValuesPanel.add(mark4);
  markM = new JTextField("19.5");
  markValuesPanel.add(markM);
  MarksF = new JTextField("60");
  markValuesPanel.add(MarksF);
  FinalG = new JTextField("A-");
  markValuesPanel.add(FinalG);
  return markValuesPanel;
  
}
public JPanel theButton()
{
  JPanel theButtons = new JPanel();
  theButtons.setLayout(new GridLayout(1,3,24,24));
  theButtons.setSize(100,200);
  theButtons.setLocation(50,150);

  FindB = new JButton("Search");
  FindB.setFont(new Font("Times", Font.BOLD, 17));
  theButtons.add(FindB);
  AddB = new JButton("Add");
  AddB.setFont(new Font("Times", Font.BOLD, 17));
  theButtons.add(AddB);
  RemoveB = new JButton("Remove");
  RemoveB.setFont(new Font("Times", Font.BOLD, 17));
  theButtons.add(RemoveB);
return theButtons;
}
	public JTextField getA1TextField(){return mark1;}
 public JTextField getA2TextField(){return mark2;}
 public JTextField getA3TextField(){return mark3;}
 public JTextField getA4TextField(){return mark4;}
 public JTextField getMidTermTextField(){return markM;}
 public JTextField getFinalExamTextField(){return MarksF;}
 public JTextField getFinalGradeTextField(){return FinalG;}
 public JList getStudentIDList(){return studentNumbers;}
 public void setStudentIDList(Integer[] studentIDs){studentNumbers.setListData(studentIDs)  ;}
}

Thanks!

Recommended Answers

All 14 Replies

you don't have a 'main' method in your class.

Add the following:

public static void main(String[] args)
{
  // Create an instance of your window here.

  // set the window to visible(true);
}
Member Avatar for iamthwee

you don't have a 'main' method in your class.

Add the following:

public static void main(String[] args)
{
  // Create an instance of your window here.

  // set the window to visible(true);
}

If he/she had to be told that, then they've got much greater problems. heh heh.

I don't think so. :)

I think the code looks good overall. I didn't give it the critical eye, but sometimes the simple stuff, like a main method, gets missed.

Member Avatar for iamthwee

>I don't think so
>but sometimes the simple stuff, like a main method, gets missed.

If he wrote that code and after compiling it got this message:

exception in thread "main" java.lang.NuSuchMethodError: main

and still didn't know what was going on I would be extremely scared if he eventually gets a job in programming.

Would'nt you?

Although in his defense, it is possible his main could be called from another class.

>I don't think so
>but sometimes the simple stuff, like a main method, gets missed.

If he wrote that code and after compiling it got this message:

exception in thread "main" java.lang.NuSuchMethodError: main

and still didn't know what was going on I would be extremely scared if he eventually gets a job in programming.

Would'nt you?

Although in his defense, it is possible his main could be called from another class.

Thanks for the reply, and I didn't get any errors on compiling and you wouldn't either if you had complied it also.
As for the the other comments that are not irrelevant to the subject I will just ignore. We are not here to criticize others but to help each other out, if you don't want to help then just don't reply.

DyCE

Member Avatar for iamthwee

Thanks for the reply, and I didn't get any errors on compiling and you wouldn't either if you had complied it also.
As for the the other comments that are not irrelevant to the subject I will just ignore. We are not here to criticize others but to help each other out, if you don't want to help then just don't reply.

DyCE

Sorry, you misunderstood me, I meant upon compilation and execution.

Remember you posted this:

exception in thread "main" java.lang.NuSuchMethodError: main

Therefore, I would have hoped you knew that you were missing a public static void main somewhere.

God bless.

I wouldn't expect that class to have a main method btw as it doesn't look to be something that stands on its own.
It looks far more like a panel to be used as part of either a larger application window or as a the content of a dialog box called from within such an application.

Independent applications would normally derive from JFrame instead of JPanel for example.

Member Avatar for iamthwee

I wouldn't expect that class to have a main method btw as it doesn't look to be something that stands on its own.
It looks far more like a panel to be used as part of either a larger application window or as a the content of a dialog box called from within such an application.

Independent applications would normally derive from JFrame instead of JPanel for example.

Well I did say:

Although in his defense, it is possible his main could be called from another class

;)

Hello if anyone can help I would like to know why I get this error when executing.. exception in thread "main" java.lang.NuSuchMethodError: main

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GridLayoutFrame extends JPanel
 {
// These are the components
    private JButton FindB,AddB,RemoveB;
    private JLabel theOne,theTwo,theThree,theFour,studentID,theMidTerm,theMidterm,theFinal1,theFinal2,FinalG1,FinalG2,FGrade2;
    private JList studentNumbers;
    private JTextField mark1,mark2,mark3,mark4,markM,MarksF,FinalG;
 Integer[] students;
    public JList getActionList() {return studentNumbers;}
 public JTextField getMark1() {return mark1;}
 public JTextField getMark2() {return mark2;}
 public JTextField getMark3() {return mark3;}
 public JTextField getMark4() {return mark4;}
 public JTextField getMarkM() {return markM;}
 public JTextField getMarkF() {return MarksF;}
 public JTextField getFinalGrade() {return FinalG;}
public GridLayoutFrame()
 {
 setLayout(new GridLayout(4,1,0,0));
 add(theStudentList());
 add(theTextField());
 add(theLabel());
    add(theButton());
}
public JPanel theStudentList()
{
  JPanel sIdpanel = new JPanel();
  sIdpanel.setLayout(null);
  sIdpanel.setSize(430,353);
  sIdpanel.setBorder(BorderFactory.createLineBorder  (Color.GRAY));
  JLabel studentID = new JLabel("Student ID");
  studentID.setSize(153,23);
  studentID.setLocation(18,12);
  studentID.setFont(new Font("Times", Font.BOLD, 17));
  sIdpanel.add(studentID);
  JList studentNumbers = new JList();
  JScrollPane scrollPane = new JScrollPane(studentNumbers,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
  scrollPane.setLocation(15,38);
  scrollPane.setSize(390,149);
  sIdpanel.add(scrollPane);
  return sIdpanel;
}
public JPanel theLabel()
{
 JPanel theLabel2 = new JPanel();
  theLabel2.setLayout(new GridLayout(1,8));
  theLabel2.setLocation(30,500);
  theLabel2.setSize(100,53);
     theLabel2.setBorder(BorderFactory.createLineBorder  (Color.GRAY));
  theOne = new JLabel("A1");
  theOne.setFont(new Font("Times", Font.BOLD, 16));
  theLabel2.add(theOne);
 
  theTwo = new JLabel("A2");
  theTwo.setFont(new Font("Times", Font.BOLD, 16));
  theLabel2.add(theTwo);
  theThree = new JLabel("A3");
  theThree.setFont(new Font("Times", Font.BOLD, 16));
  theLabel2.add(theThree);
  theFour = new JLabel("A4");
  theFour.setFont(new Font("Times", Font.BOLD, 16));
  theLabel2.add(theFour);
  theMidTerm = new JLabel("Midterm");
  theMidTerm.setFont(new Font("Times", Font.BOLD, 17));
  theLabel2.add(theMidTerm);
  theMidterm = new JLabel("Exam");
  theMidterm.setFont(new Font("Times", Font.BOLD, 17));
  theLabel2.add(theMidterm);
  theFinal1 = new JLabel("Final");
  theFinal1.setFont(new Font("Times", Font.BOLD, 17));
  theLabel2.add(theFinal1);
  theFinal2 = new JLabel("Exam");
  theFinal2.setFont(new Font("Times", Font.BOLD, 17));
  theLabel2.add(theFinal2);
  FinalG1 = new JLabel("Final");
  FinalG1.setFont(new Font("Times", Font.BOLD, 17));
  theLabel2.add(FinalG1);
 
  FGrade2  = new JLabel("Grade");
  FGrade2.setFont(new Font("Times", Font.BOLD, 17));
  theLabel2.add(FGrade2);
return theLabel2;
}
public JPanel theTextField()
{
 JPanel markValuesPanel = new JPanel();
  markValuesPanel.setLayout(new GridLayout(1,7,5,5));
  markValuesPanel.setSize(10,10);
  markValuesPanel.setLocation(18,100);
  mark1 = new JTextField("25");
  markValuesPanel.add(mark1);
  mark2 = new JTextField("14.5");
  markValuesPanel.add(mark2);
  mark3 = new JTextField("31.5");
  markValuesPanel.add(mark3);
  mark4 = new JTextField("30");
  markValuesPanel.add(mark4);
  markM = new JTextField("19.5");
  markValuesPanel.add(markM);
  MarksF = new JTextField("60");
  markValuesPanel.add(MarksF);
  FinalG = new JTextField("A-");
  markValuesPanel.add(FinalG);
  return markValuesPanel;
 
}
public JPanel theButton()
{
  JPanel theButtons = new JPanel();
  theButtons.setLayout(new GridLayout(1,3,24,24));
  theButtons.setSize(100,200);
  theButtons.setLocation(50,150);
 
  FindB = new JButton("Search");
  FindB.setFont(new Font("Times", Font.BOLD, 17));
  theButtons.add(FindB);
  AddB = new JButton("Add");
  AddB.setFont(new Font("Times", Font.BOLD, 17));
  theButtons.add(AddB);
  RemoveB = new JButton("Remove");
  RemoveB.setFont(new Font("Times", Font.BOLD, 17));
  theButtons.add(RemoveB);
return theButtons;
}
    public JTextField getA1TextField(){return mark1;}
 public JTextField getA2TextField(){return mark2;}
 public JTextField getA3TextField(){return mark3;}
 public JTextField getA4TextField(){return mark4;}
 public JTextField getMidTermTextField(){return markM;}
 public JTextField getFinalExamTextField(){return MarksF;}
 public JTextField getFinalGradeTextField(){return FinalG;}
 public JList getStudentIDList(){return studentNumbers;}
 public void setStudentIDList(Integer[] studentIDs){studentNumbers.setListData(studentIDs)  ;}
}

Thanks!

you just need to add

public static void main(String args[]){
GridLayoutFrame gf = new GridLayoutFrame();
}


by adding this you make an instance of your class and fulfill the requirement of having a method 'main'(suggest using this at the very end of class, and use it in every gui)

You do realise that this thread is over a year and half old, right?

yeh this is a year old thread but about 4 kids i know googled this error and got your page. I wonder how many more new kids are reading this in response to a common error from textbook application examples heheh all those guys, picking out the one who wants to force main into every app, are trying to make this standalone. You DONT need a main method in applets which this app so obviously is. Embed this code in HTML <applet code="this app"></applet> I commend the most recent entry advising to create an instance of class. but they obviously didnt try it as where they located the main won't compile.

I am a novice and got the same error. I changed the class and object names and them it worked. hope this helps

THIS IS SUCH A BIG HELP :) THANK YOU!!!

Embed this code in HTML <applet code="this app"></applet> I commend the most recent entry advising to create an instance of class. but they obviously didnt try it as where they located the main won't compile.

JavaDuke: this thread is 6 years old, there's no use in reviving it every year.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.