0

When running my program, I get the following error:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 

at CylinderTest.main(Cylinder.java:42)

I'm sure there is an easy solution, but I am an inexperienced programmer and to me it seems like it should work.

Program Description: Write a class called CylinderTest.java and declare an array of three Cylinder objects to call the methods you declared in the Cylinder class. Make sure that all class methods are called from main(). Have main() display the value returned by volume() and verify the returned value by hand calculations (paper/pencil). Prompt the user to enter the values for the radius and height of each Cylinder object in the array.

public class Cylinder 
{
  private double radius;
  private double height;
  public Cylinder(double radius, double height)
  {
      this.radius = radius;
      this.height = height;
  }
  public double getRadius()
  {
      return radius;
  }
  public double getHeight()
  {
      return height;
  }
  public double volume()
  {
      return radius*radius*height*3.1416;
  }


}

public class CylinderTest
{

public static void main(String[] args) 
{

    Cylinder[] myCylinder = new Cylinder[3];
    myCylinder [0] = new Cylinder (2,7);
    myCylinder [1] = new Cylinder (9,3);
    myCylinder [2] = new Cylinder (12,4);
    for (Cylinder c: myCylinder)
    {
        System.out.println("*******");
        System.out.println("Radius: " + c.getRadius());
        System.out.println("Height: " + c.getHeight());
        System.out.println("Volume: " + c.volume());
        System.out.println("*******");
    }
}
}   
user1798926
  • 39
  • 1
  • 2
  • 4
  • Check out the accepted answer here: http://stackoverflow.com/questions/1124788/java-unresolved-compilation-problem – RB. Nov 14 '12 at 15:18

3 Answers3

1

Exception in thread "main" java.lang.Error: Unresolved compilation problem:

at CylinderTest.main(Cylinder.java:42)

You class Cylinder is unable to be compiled because of an error that exists at line 42 in the Cylinder.java file

Woot4Moo
  • 23,987
  • 16
  • 94
  • 151
  • Silly question, but when running eclipse, is there a way to display line numbers to the left of the code? – user1798926 Nov 13 '12 at 23:55
  • @user1798926 `“Windows” > “Preference” > “General” > “Editors” > “Text Editors” , check on the “Show line numbers” option.` – Woot4Moo Nov 14 '12 at 00:01
  • and the downvote because of? – Woot4Moo Nov 14 '12 at 00:01
  • Thank you for the directions Woot, that will help me for my future problems. And creating a separate class file rather than trying to combine them was my issue. A rookie mistake. – user1798926 Nov 14 '12 at 00:26
  • That's part of what StackOverflow is for. I've made the same mistake myself!. Note that in my answer I assumed that CylinderTest was part of a Test framework (e.g. with Junit). It would be a useful exercise to org.junit.Assert that the answers you get are the ones you calculated (remember you cannot compare doubles exactly - you need to include a small margin of error) – peter.murray.rust Nov 14 '12 at 01:01
1

It is because you have two separate public classes in one file. Split CylinderTest into its own file. In general it is useful to have a directory structure that separates test classes:

src
   main
      java
   test
      java

You should also create a package name for Cylinder (say) org.me

in which case both classes should have

package org.me;

at the top.

You should use an IDE (such as Eclipse or Netbeans) and this would tell you there were compilation errors before you tried to run it.In general running programs with errors is a poor idea and it is often difficult to tell where and what happened. However Eclipse will normally provide a stacktrace that will link to the offending line.

peter.murray.rust
  • 37,407
  • 44
  • 153
  • 217
0

Woot4Moo has explained the direct cause of the exception. Basically, you cannot run a program that uses a class that has compilation errors. Fix all compilation errors before you attempt to run your program:

  • If you visit the file in Eclipse, the compilation error should be flagged in red.

  • You can configure Eclipse to not allow you to Run code if there are compilation errors in your workspace.

Since we cannot see the actual compilation error, I cannot be sure of this. However, I suspect that the underlying problem is that you have two top-level public classes in one file (called "Cylinder.java"). You cannot do that in Java. Non-nested public classes have to be in separate source files. (You can get away with doing this with "package private" classes, but it is Bad Practice.)

The fix is to put the Cylinder and CylinderTest classes in separate source files.

(Ideally, you should also add package declarations, but I'm guessing that your instructor hasn't explained those to you yet.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Thank you everyone, the problem was in fact a simple fix by just creating a new class in Eclipse rather than trying to combine in one file. Rookie mistake. Thank you all for your help, it is greatly appreciated. – user1798926 Nov 14 '12 at 00:24