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?
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;}
}
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.
Code :
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.
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:
----------------------------------------------------------------------------------------------------------------
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
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
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.
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.
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.