1

I am building my own GUI that will display a list of Friend's objects in list form. The first problem I ran into is that when I run the code without a constructor, everything works fine. But when I create a constructor for my GUI class, the error message displayed:

load: GUIapp.class is not public or has no public constructor.
java.lang.IllegalAccessException: Class sun.applet.AppletPanel can not access a member of             class GUIapp with modifiers ""
at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:65)
at java.lang.Class.newInstance0(Class.java:349)
at java.lang.Class.newInstance(Class.java:308)
at sun.applet.AppletPanel.createApplet(AppletPanel.java:807)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:714)
at sun.applet.AppletPanel.run(AppletPanel.java:368)
at java.lang.Thread.run(Thread.java:680)

My Code:

public class GUIapp extends JApplet{

/*
 * Attributes
 */

//** Friends Objects**//
private FriendsGroup a;
private ArrayList<friends> friendList;

//** PANEL **//
private JPanel outerPanel;

//** Button **//
private JButton button1;


/*
 * Constructor for Getting all the friends set up
 */

private GUIapp(){
    a = null;  //initialize variable

    try {
        a = new FriendsGroup("friends.txt"); //import friend list
    } catch (IOException e) {
        System.out.println("Fail Import.");
    }

    friendList = a.getFriendsGroup(); //return an arrayList of Friends Object
}


/*
 * Create Stuff
 */
public void createStuff() {
    outerPanel = new JPanel(); //create outer panel
    button1 = new JButton("Click Me");
    outerPanel.add(button1,BorderLayout.SOUTH);
}


/*
 * Initialize Stuff
 * 
 */
public void init(){
    createStuff(); //initialize create stuff

    this.add (outerPanel); 
}
}

In the Above Code, if you take out the constructor, it seems to work perfectly. My Question is, what is wrong with the code? Why can't I seem to create a constructor to load in data first?

My Second Question is how would I go about create a panel whereby it displays a list of friends names? Theses names are imported and stored in the arraylist of friends Object called friendList stored in the constructor.

Thanks,

6
  • There is nothing wrong to create a constructor, unless it's not private, and the only constructor in the class. Commented Feb 15, 2013 at 6:00
  • This might be helpful: docs.oracle.com/javase/tutorial/reflect/member/ctorTrouble.html Commented Feb 15, 2013 at 6:05
  • 2
    Re: your edit: please don't move the goalposts with edits. The NullPointerException is a new problem that should go into a new SO question and that you should try to work out first. Don't expect people to walk you through your whole program once you have someone's attention. Commented Feb 15, 2013 at 6:09
  • what does getFriendsGroup() do? Commented Feb 15, 2013 at 6:13
  • Apologies, I wont do that again. getriendsGroup() is a method of FriendsGroup that returns an ArrayList(). The FriendsGroup class is used to parse a text file that and create Friends object along the way. Commented Feb 15, 2013 at 6:17

4 Answers 4

1

when you are defining a constructor by yourself compiler will not create the default constructor since your defined constructor is private you will not have a public constructor

so simply create a public constructor

public GUIapp(){
    // your code
}
Sign up to request clarification or add additional context in comments.

Comments

0

because you define constructor private change it to;

public GUIapp(){
    a = null;  //initialize variable

    try {
        a = new FriendsGroup("friends.txt"); //import friend list
    } catch (IOException e) {
        System.out.println("Fail Import.");
    }

    friendList = a.getFriendsGroup(); //return an arrayList of Friends Object
}

1 Comment

I just made an edit to the question. When I declare it public, i get a whole new error
0

The problem is this: private GUIapp(){. That means that your constructor is available only to that class. Usually constructors are public, although there do exist exceptions, example of which could be the Singleton Pattern.

Removing the constructor works because each class, by default has a parameterless constructor. Take a look at this tutorial for more information on access modifiers.

Alternatively, you could have a method like so in your GUIapp class:

public static GUIapp getInstance() { return new GUIapp(); }

and you call that from your main class, but I think that in this case, changing your constructor from private to public should be enough.

Regarding your second question, this tutorial should be of help.

Comments

0

You ened to change syour constructor to public and debug into:

a.getFriendsGroup();

Its not clear what this methode does, and i assume for some reason (maby the list from the file is empt) the methode tries to access a non assigned object which causes null reference exception, try to debug into the methode to see where this happends or post the code of the methode.

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.