Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Compliing fine but wont run--please help!

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Compliing fine but wont run--please help!

    Hi there,
    Im trying to write code for an assignment that is due tommorow and my head is wrecked with this. My program compiles perfectly but when I try to run it Im getting a message that says : Exception in thread "main" java.lang.NoSuchMethodError: main
    Press any key to continue . . .

    can someone please tell me whats wrong with my code?

    import java.io.*;
     
    public class CreditUnion
    {
    	private int account;
    	private String lastName;
    	private String firstName;
    	private double balance;
     
    	//read a record from the specified RandomAccessFile
    	public void read(RandomAccessFile file) throws IOException
    	{
    		account=file.readInt();
     
    		char first[] =new char[ 15 ];
     
    		for (int i=0; i < first.length;i++)
    		first [ i ]=file.readChar();
     
    		firstName =new String ( first );
     
    		char last[]= new char [ 15 ];
     
    		for (int i=0; i < last.length; i++)
    		last [ i ]=file.readChar();
     
    		lastName =new String( last);
     
     
     
    		balance=file.readDouble();
     
    	}
     
    	//write a record to the specified RandomAccessFile
    	public void write( RandomAccessFile file ) throws IOException
    	{
    		StringBuffer buf;
     
    		file.writeInt( account );
     
    		if (firstName !=null )
    		buf= new StringBuffer( firstName );
    		else
    		buf =new StringBuffer( 15 );
     
    		buf.setLength( 15 );
     
    		file.writeChars( buf.toString() );
     
    		if (lastName != null)
    		   buf =new StringBuffer( lastName );
    		   else
    		   buf= new StringBuffer( 15 );
     
    		   buf .setLength( 15 );
     
    		   file.writeChars( buf.toString() );
     
    		   file.writeDouble( balance );
    	   }
     
    	   public void setAccount( int a ) {account = a;}
    	   public int getAccount() {return account; }
    	   public void setfirstName( String f ) {firstName =f;}
    	   public String getfirstName() {return firstName;}
    	   public void setlastName( String l ) {lastName =l;}
    	   public String getlastName() {return lastName;}
    	   public void setBalance( double b ) {balance =b;}
    	   public double getBalance() {return balance; }
     
    	   //Note: This method contains a hard coded value for the
    	   //size of a record of information
    	   public static int size() { return 72;}
       }
    Last edited by helloworld922; May 9th, 2010 at 11:24 PM. Reason: please use [code] tags!


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Compliing fine but wont run--please help!

    All java classes that you want to run as an "entry-point", i.e. starting point need to have a main method.

    public static void main(String[] args)
    {
      // starting code goes in here
    }

    It compiles ok because not all java classes need to have an entry point (in fact, many don't), but you can't run just that class without having the main method.

    There are two main solutions to this problem:

    1. Add the main method to this class.
    2. Create a "driver class" which contains the main method. Then you just run the driver class and use the appropriate functionality from your class via the driver class.

  3. #3
    Junior Member
    Join Date
    Dec 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Compliing fine but wont run--please help!

    Hi,

    Thanks for your reply. I`ve done as you suggested and here is the current code:
    ----------------------------------------------------------------------------------------------------------------
    import java.io.*;
    import java.awt.*;
    import java. awt.event.*;
     
    public static void main(String[] args)
    {
     
    public class CreditUnion
    {
    	private int account;
    	private String lastName;
    	private String firstName;
    	private double balance;
     
    	//read a record from the specified RandomAccessFile
    	public void read(RandomAccessFile file) throws IOException
    	{
    		account=file.readInt();
     
    		char first[] =new char[ 15 ];
     
    		for (int i=0; i < first.length;i++)
    		first [ i ]=file.readChar();
     
    		firstName =new String ( first );
     
    		char last[]= new char [ 15 ];
     
    		for (int i=0; i < last.length; i++)
    		last [ i ]=file.readChar();
     
    		lastName =new String( last);
     
     
     
    		balance=file.readDouble();
     
    	}
     
    	//write a record to the specified RandomAccessFile
    	public void write( RandomAccessFile file ) throws IOException
    	{
    		StringBuffer buf;
     
    		file.writeInt( account );
     
    		if (firstName !=null )
    		buf= new StringBuffer( firstName );
    		else
    		buf =new StringBuffer( 15 );
     
    		buf.setLength( 15 );
     
    		file.writeChars( buf.toString() );
     
    		if (lastName != null)
    		   buf =new StringBuffer( lastName );
    		   else
    		   buf= new StringBuffer( 15 );
     
    		   buf .setLength( 15 );
     
    		   file.writeChars( buf.toString() );
     
    		   file.writeDouble( balance );
    	   }
     
    	   public void setAccount( int a ) {account = a;}
    	   public int getAccount() {return account; }
    	   public void setfirstName( String f ) {firstName =f;}
    	   public String getfirstName() {return firstName;}
    	   public void setlastName( String l ) {lastName =l;}
    	   public String getlastName() {return lastName;}
    	   public void setBalance( double b ) {balance =b;}
    	   public double getBalance() {return balance; }
     
    	   //Note: This method contains a hard coded value for the
    	   //size of a record of information
           public static int size() {return 72;}
     
       }
    }
    --------------------------------------------------------------------------------------------------------

    However, since adding the public static void main, Im getting the following errors in the compilor:

    C:\Documents and Settings\Administrator\Desktop\Fourth Lab Notebook Review\CreditUnion.java:5: class, interface, or enum expected
    public static void main(String[] args)
    ^
    C:\Documents and Settings\Administrator\Desktop\Fourth Lab Notebook Review\CreditUnion.java:82: class, interface, or enum expected
    }
    ^
    2 errors

    Tool completed with exit code 1
    Last edited by JavaPF; May 10th, 2010 at 08:36 AM. Reason: please use code tags

  4. #4
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Compliing fine but wont run--please help!

    Better start at the beginning: (link below)
    The Java™ Tutorials

    A question for you: where does your "main" method end?

    db

  5. #5
    Junior Member
    Join Date
    Dec 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Compliing fine but wont run--please help!

    Daryl, your post is unhelpful and smug. I am clearly a student or I wouldn`t`be asking the question. If you have nothing helpful to add then don`t`post.

  6. #6
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Compliing fine but wont run--please help!

    The 'main' method belongs inside the class, like all Java methods. In it, you would normally create an instance of your application class and start it running.

    Daryl did have a point - the Java Tutorials give you full explanations and examples of how to do these basics - see HelloWorldSwing.java.
    Last edited by dlorde; May 10th, 2010 at 10:48 AM.

  7. #7
    Junior Member
    Join Date
    Dec 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Compliing fine but wont run--please help!

    It is his manner I have an issue with. It`s unneccesarily condescending. I am obviously well aware of the java tutorials but as I`ve asked a specific question I would like a specific answer, as the other poster gave , not a sarky remark which was all Daryl decided to offer.
    Last edited by Nova; May 12th, 2010 at 08:28 PM.

Similar Threads

  1. [SOLVED] Simple Grahpic porgram wont work.
    By kogo50 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 8th, 2010, 12:46 PM
  2. pictures wont load
    By wolfgar in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2010, 09:34 AM
  3. Craps- GAME fine tunning.
    By Beaney in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 3rd, 2010, 08:32 PM