0

This is basically a Java code converter. It involves a GUI let user input class type, name and method. To store the values, I've created a class VirtualClass with an ArrayList<VirtualClass> classes to store variables boolean isPrivate, String className and String methodName. However, I found that nothing was stored into the ArrayList...please help me to see what's the problem

Below is the class VirtualClass

import java.util.*;

public class VirtualClass {

    private static ArrayList<VirtualClass> classes = new ArrayList<VirtualClass>();
    private boolean isPrivate;
    private String className;
    private String methodName;

    public void setVirtualClass(String name, String method, boolean isP){
        this.className = name;
        this.isPrivate = isP;
        this.methodName = method;
    }

    public void createClass(String name, String method, boolean isP){
        this.className = name;
        this.isPrivate = isP;
        this.methodName = method;
        classes.add(this);
    }

For reference, here's some relevant code from the GUI which let users create class

public class GuiAddClass extends JFrame{
    private VirtualClass stObject;
        ...

private class Handler implements ActionListener{

    public void actionPerformed(ActionEvent event){

        String cName = inputClassName.getText();
        String mName = inputMethodName.getText();
        boolean isP = true;

        if (classObject.checkName(cName) == false){

            JOptionPane.showMessageDialog(null, "Class name invalid. " +
                    "\nEntered name should not contain java keywords or equal to other existing names. " +
                    "\nPlease try again."); 

        } else if (classObject.checkName(cName) == true) {

            JOptionPane.showMessageDialog(null, "Class saved."); 
                    // this message pane has popped up
            cName = inputClassName.getText();
            mName = inputMethodName.getText();

            if (event.getSource() == publicButton) {
                isP = false;
            } else if (event.getSource() == privateButton) {
                isP = true;
            }
            stObject = new VirtualClass();
            stObject.createClass(cName, mName, isP);
        }

    }// end actionPerformed()

}// end Handler class

And here's a couple of methods from another class for display the final javaCode

public String getClassName(){
    String cName = "classname";
    String c = "c";
    for (int i=0; i<classes.size(); i++){
        c = classes.get(i).className;
    }
    cName = c;
    return cName;
}    

public String getMethodName(){
    String mName = "methodname";
    String m = "m";
    for (int i=0; i<classes.size(); i++){
    m = classes.get(i).methodName;
    }
    mName = m;
    return mName;
}

public boolean getIsPrivate(){
    boolean isP = false;
    for (int i=0; i<classes.size(); i++){
        isP = classes.get(i).isPrivate;
    }
    return isP;
}

Here's the method to generate the Java code

    public String getJavaCode(){
        String javaCode = (classObject.getPublic() + " class " + 
stObject.getClassName() + stObject.getListSize() + 
"{\n"+"\t"+"public void "+stObject.getMethodName()+"{\n"+"\t}"+"\n}");
        return javaCode;

And what would display in my programme is like this, where c should be class name, m should be method name, and 0 = classes.size()

public class c0{
    public void m{
    }
}

Can anyone help me to spot out the problem please? I just have no idea and the answers I received doesn't seem to work. Please help!

9
  • I found that nothing was stored into the ArrayList how do you know that nothing stored in? Commented Oct 27, 2013 at 11:28
  • It would get stored, if the control went to the else if. Commented Oct 27, 2013 at 11:30
  • I have asked this question: stackoverflow.com/questions/19617263/… and the method in the question is supposed to return values stored in my ArrayList. but it returned the initialised value of the string Commented Oct 27, 2013 at 11:32
  • How do you determine that nothing was stored into the ArrayList? Commented Oct 27, 2013 at 11:32
  • @R.J thanks, I'll see if its the true false goes wrong Commented Oct 27, 2013 at 11:33

2 Answers 2

1

From information you posted, seems strange that you initiate VirtualClass stObject into actionPerformed method. Its mean that each time you recreate your object.

Make your VirtualClass stObject to be global for example, like:

private VirtualClass stObject;

...

stObject = new VirtualClass();

private class Handler implements ActionListener{

    public void actionPerformed(ActionEvent event){

    ...

   stObject.createClass(cName, mName, isP);
Sign up to request clarification or add additional context in comments.

2 Comments

I've got syntax error on stObject = = new VirtualClass();. Should I add this line just before handler?
Sorry, its mistake, use =. fixed my answer
0

You have a couple of problems with your code. But mainly, you creational logic does not make much sense. You should not add an instance into your Static collection using a method of the instance. My advice would be to use a static factory method to do this. Something like this instead :

public class VirtualClass {

private static List<VirtualClass> classes = new ArrayList<VirtualClass>();
private boolean isPrivate;
private String className;
private String methodName;

//A private constructor
private VirtualClass(String name, String method, boolean isP){
    this.className = name;
    this.isPrivate = isP;
    this.methodName = method;
}

private void setClassName(String className){
    this.className = className;
}

private void getClassName(){
    return className;
}

public static VirtualClass createClass(String name, String method, boolean isP){
    VirtualClass virtualClass = new VirtualClass(String name, String method, boolean isP);
    classes.add(virtualClass);
    return virtualClass;
}
}

The way your code is done now, the problems can be in a lot of places in your client classes. This structure is safer and generally used.

Also you should not type your collection with ArrayList but use the implemented interface instead, List. how to use an array list?

7 Comments

Sorry, I don't quite understand this sentence you should not type your collection with ArrayList but use the implemented interface instead. Do you mean that I should set VirtualClass as a interface, and let other classes implement VirtualClass to get its values?
No, what I meand is the ArrayList type is a Raw Java Type and it is not a good practice to use it according to java specs. You should use the List interface instead. Look at my declaration of my static collection, it is something like this, private static List<E> classes = new ArrayList<E>()
and is it a must for me to use a private constructor? Can I use setVirtualClass instead? and can I access the attributes inside the List interface?
I see. Thanks! I've just checked List interface out. I don't know about this type before :) thanks for teaching me!
It would make things a lot easier in your code. However you could remove what I did, have a public constructor and your setter method and add a static method that would add your instance into your collection. It depends of your intent. What I added is a way to ensure that all your instanced ends up in your array. But nothing prevents you add setters for your instance so they can be changed at creation. I will edit my code to show you how I would do it.
|

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.