1

I have this code, but it gives error at here: throw new MessageTooLongException();, it says "No enclosing instance of type MessageLenght is accessible. Must qualify the allocation with an enclosing instance of type MessageLenght (e.g. x.new A() where x is an instance of MessageLenght)." I am using Eclipse. I cannot figure it out why it gives error. Please help me.

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.*;
class MessageLenght extends MessageTooLongException
{
    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);
        try
        {
            char ch;
            do
            {   
                System.out.println("Please enter the text: ");;

                String hr = keyboard.nextLine();

                int count = hr.length();

                if(count>= 20)
                {
                    throw new MessageTooLongException();
                }
                else
                {
                    System.out.println("Text: " + count + "characrters which is acceptable length.");

                }

                System.out.println("Do you want to continue- Y/N");
                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                String s = br.readLine();
                ch = s.charAt(0);

            }
            while((ch =='Y') || (ch =='y'));
        }
        catch(MessageTooLongException ex)
        {
            System.out.println(ex.getMessage());

        }
        catch(IOException ex)
        {
            System.out.println(ex.getMessage());
        }
    }

    class MessageTooLongException extends Exception
    {
        public MessageTooLongException()
        {
            super("Message Too Long Exception");
        }
        public MessageTooLongException(String msg)
        {
            super(msg);
        }
    }
}
aa...
  • 11
  • 3
  • @selimssevgi Sure, that code doesn't make much sense. But as weird as his code is, he doesnt go for multiple inheritance. – GhostCat Nov 28 '16 at 20:19
  • @aa... Minor nitpick: you should change the name `MessageLenght` to `MessageLength` so that people who use the class have an easier time of it. – Chai T. Rex Nov 28 '16 at 20:28

1 Answers1

1

The problem is: your inner base exception MessageTooLongException is not marked as static!

That means: in order to create an instance of it (or an instance of a class that extends) you need an instance of that outer class to which MessageTooLongException actually belongs too. In other words: your code can't work. You cant create an instance of MessageLenght ... because you would already need an instance of that class in order to create an instance of the super class.

So, two solutions:

  1. you simply move that class declaration outside of class MessageLenght (for example in front of it)
  2. you make that inner class static

See here for more details.

But of course, there is more to it: exception classes are means to communicate something. So exceptions should normally go into their own classes. And then: by convention, exception class names end with "...Exception". MessageLenght does not. So this whole construct is simply surprising to other java programmers. Which is a bad thing. You do not want that your code surprises other people.

So, the real answer here is: you want to step back and clarify for yourself what you actually intend to do. Because as is, your code simply doesn't make much sense. And you want to study the java basics much more.

Community
  • 1
  • 1
GhostCat
  • 137,827
  • 25
  • 176
  • 248