1

UPDATE:

I've fixed the syntax error and removed the exception handling, and I'm gettint the exact same error message (different line number, of course):

class Point {
    public Point (int x, int y) {
        this.x = x;
        this.y = y;
    }

    public void displayPointPosition() {
        System.out.println("Position: " + x + " " + y);
    }
    private int x, y;
}

public class Exinfo2 {
    public static void main(String args[]) {
        Point a = new Point(1, 4);
        a.displayPointPosition();
        a = new Point(-3, 5);
        a.displayPointPosition();
    }
}

This really must be a beginner's mistake, and my code editor (Visual Studio Code) doesn't highlight anything wrong...

==============

I'm learning Java and I'm now trying to handle custom exceptions, but without success. I'm having various error messages when compiling my code, and I even have an issue from a sample that I copied from a book, so it seems like there might be something else than just the code...

This is the sample that I'm trying to make work, and I'm getting the compiler message:

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

        at Exinfo2.main(Point.java:23)".

This is the code:

class Point {
    public Point (int x, int y) throws ErrConst {
        if ( (x < 0) || (y < 0) ) {
          throw new ErrConst ("Constructor error:" + x + " " + y);
        }
        this.x = x;
        this.y = y;
    }

    public void displayPointPosition() {
        System.out.println("Position: " + x + " " + y);
    }
    private int x, y;
}

class ErrConst extends Exception {
    ErrConst(String mes) {
        super(mes);
    }
}

public class Exinfo2 {
    public static void main(String args[]) {
        try {
            Point a = new Point(1, 4);
            a.displayPointPosition();
            a = new Point(-3, 5);
            a.displayPointPosition();
        }
        catch (ErrConst e) {
            System.out.println(e.getMessage());
            System.exit(-1);
        }
    }
}

I'm sure that this is very basic, but it's a headache for me, so I'm hoping that a kind coder could take a few minutes to help me out... :-)

Thanks in advance! :-)

3
  • 3
    System.out.println(Position: " + x + " " + y); doesn't look like valid Java - you're missing an opening quote before Position Commented Sep 18, 2020 at 3:40
  • Hello @PlayerOne, You are absolutely right, I've made a mistake while editing the code after pasting here. I had (and still have) the exact same compilation error after I fixed this. Thanks! Commented Sep 18, 2020 at 11:53
  • Hello @NathanHughes, thanks for the advice. I did remove all the exception handling and I have the same error. Must be a beginner's mistake and my code editor (Visual Studio Code) doesn't highlight anything wrong. I'll post the code without the exception handling. Commented Sep 18, 2020 at 12:03

2 Answers 2

2

Your code runs fine when i try to run it.

However, according to the exception you posted, it looks like you have saved the file as Point.java. You have several classes in the same file, but your public class is called Exinfo2. In Java, the filename and the name of the public class must be the same, so that's probably the problem.

Try saving the file as Exinfo2.java.


The error message you get from the compiler is strange, though. Normally you would get the error message "class Exinfo2 is public, should be declared in a file named Exinfo2.java". It looks like the error you get is from the Eclipse compiler, so maybe the Visual Studio Code plugin is using that compiler instead of the regular one. Try compiling it like this from the command-line instead:

javac Exinfo2.java
java Exinfo2
Sign up to request clarification or add additional context in comments.

1 Comment

Hi! Awesome! Yes, it was just a beginner's thing, and I won't make this basic mistake again. Now I'll remember always that I must use the name of the main public class as file name, especially if I have more than one class in the same file. The code runs fine now, including the exception handling! That's a great way to start my Friday! :-) Thanks so much!
1

I think that in your Point class..the method displayPointPosition .. the PrintStream not found your message.. can u try change it..for this : System.out.println("Position: " + x + " " + y);

regards!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.