0

I am writing a program in java were I have to make a class (InvoiceTest) that will have main in it and a second class (Invoice). When I run the main InvoiceTest class it should show the message in the Invoice class. I keep getting an error

Exception in thread "main" java.lang.ExceptionInInitializerError
    at InvoiceTest.main(InvoiceTest.java:4)
Caused by: java.lang.RuntimeException: Uncompilable source code - constructor Invoice() is already defined in class Invoice
    at Invoice.<clinit>(Invoice.java:20)
    ... 1 more

I dont know what to do, please let me know what I am doing wrong and any suggestions on how to fix it. Thanks

public class InvoiceTest{
    public static void main(String [] args) {
        Invoice invoiceObject = new Invoice();
        invoiceObject.simpleMessage();
    }
}

// This is the second class. I am using NetBeans so each class is in its own window.

public class Invoice {        
    public void simpleMessage() {
        System.out.println("This is another class");
    }
}
Jeffrey
  • 44,417
  • 8
  • 90
  • 141
user2005617
  • 9
  • 1
  • 4
  • `constructor Invoice() is already defined in class Invoice at Invoice.` instead of window you mean different files? – nachokk Jul 03 '13 at 01:07
  • this is your complete code?? – nachokk Jul 03 '13 at 01:11
  • Yes for now, I just started so I will be adding on. What did you mean by decode? – user2005617 Jul 03 '13 at 01:12
  • check this old thread on SO http://stackoverflow.com/questions/2333285/java-lang-runtimeexception-uncompilable-source-code-what-can-cause-this – user2506840 Jul 03 '13 at 01:27
  • i tried to reproduce your error but nothing... i used default-package, no constructor declaration.. do you have the classes in a project? – nachokk Jul 03 '13 at 01:58
  • dear @user2005617, could you please check if there's another class in the same classpath with the same name? Another way to check this.. change the name of your class `Invoice` to `InvoiceX` or something like that. – zEro Jul 03 '13 at 02:46
  • @zEro but this would get a compile time error rather than a runtimeexception i guess.. but whos knows xD – nachokk Jul 03 '13 at 02:55

3 Answers3

0

Are they both in the same .java file? The java compiler requires exactly one java class per source file.

Jan Dörrenhaus
  • 6,581
  • 2
  • 34
  • 45
0

You don't need two separate classes. One class works.

public class Invoice {        
    public void simpleMessage() {
        System.out.println("This is another class");
    }

    public static void main(String[] args) {
        Invoice invoiceObject = new Invoice();
        invoiceObject.simpleMessage();
    }
}
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
  • What did you mean by separate? Into different files? – user2005617 Jul 03 '13 at 01:19
  • @user2005617: You defined two separate classes, Invoice and InvoiceTest. I've always had the main method instantiate the class that contains the main method, so that's what I put in my answer. – Gilbert Le Blanc Jul 03 '13 at 01:21
0

Strange error indeed.

How about adding back the constructor for the class Invoice ?

public Invoice(){ }

Black Maggie
  • 496
  • 2
  • 15