-1

I am trying to offload my graphics to another class, however when I initialize that class I get:

at java.security.AccessController.getContext(Unknown Source)
at java.awt.Component.<init>(Unknown Source)
at java.awt.Canvas.<init>(Unknown Source)
at Display.<init>(Display.java:11)
at Display.<init>(Display.java:7)

I researched and found something saying that I have to have a constructor with no arguments however the constructor did not seem to solve the problem.

----This is where I initilize my display class----

import java.util.Scanner;

public class Ytube2URL 
{

public static void main(String[] args) {
    Display d = new Display();
    d.initGraphics();

    String Userurl = askUserForURL();
    System.out.println(Userurl);
    String Downloadurl = GetYoutubeOnlyURL(Userurl);    
}
}

-------This is my Display class------

import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class Display extends Canvas
{
Display d = new Display();
JFrame f = new JFrame();
JOptionPane jo =new JOptionPane();

public Display()
{

}

public void initGraphics()
{
    f.add(d);
    f.setSize(200,600);
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jo.add(f);
    f.pack();
}
}

Thank you for your help and explanations.

Sree
  • 1,694
  • 1
  • 18
  • 29
Matthew R
  • 23
  • 4
  • 3
    You left out part of the exception -- the actual error. – KevinO Mar 23 '19 at 13:24
  • 1
    Why are you creating a new instance of the `Display` class in the `Display` class itself? You do not seem to be using this anywhere. You are creating an instance in your `main` method. Seems like it doesn't need to be there. – KevinO Mar 23 '19 at 13:27
  • That makes so much sense thank you, I was not trying to add an instance of display in the display class. I should have known because the error was recursive what it was trying to do. – Matthew R Mar 23 '19 at 13:31
  • Please fix your question -- it doesn't yet show the critical part of the error, the part that states that you're throwing a StackOverflowError, and that states which lines are causing it. You then need to indicate within the question which lines correspond to the line numbers. – Hovercraft Full Of Eels Mar 23 '19 at 14:07
  • My question is not throwing any errors it is just not running in the way I was expecteing, also an answer has been selected. – Matthew R Nov 20 '19 at 23:20

2 Answers2

0

You are creating a new instance of Display class in itself. So, whenever you create an object of Display type it again creates a new object and this process goes again and again recursively. Don't create new Display type object in Display class.

Working code: ------ Display class ------

import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class Display extends Canvas
{
    JFrame f = new JFrame();
    JOptionPane jo =new JOptionPane();

    public void initGraphics()
    {
        f.add(d);
        f.setSize(200,600);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jo.add(f);
        f.pack();
    }
}
Zain Arshad
  • 1,885
  • 1
  • 11
  • 26
0

If you need to add a Display inside the JFrame you can do f.add (this); since this refers to the Display object using the initGraphics method

alvinalvord
  • 394
  • 2
  • 11