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!

Sue
  • 199
  • 1
  • 3
  • 10
  • `I found that nothing was stored into the ArrayList` how do you know that nothing stored in? – Maxim Shoustin Oct 27 '13 at 11:28
  • It would get stored, if the control went to the `else if`. – Rahul Oct 27 '13 at 11:30
  • I have asked this question: http://stackoverflow.com/questions/19617263/how-to-return-a-value-inside-a-loop-in-a-return-method?noredirect=1#comment29121536_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 – Sue Oct 27 '13 at 11:32
  • How do you determine that nothing was stored into the ArrayList? – JB Nizet Oct 27 '13 at 11:32
  • @R.J thanks, I'll see if its the `true false` goes wrong – Sue Oct 27 '13 at 11:33
  • @Sue: If you print `classes.size();` what do you see? – anubhava Oct 27 '13 at 11:34
  • @ JB Nizet @ Maxim Shoustin, I've asked this question: http://stackoverflow.com/questions/19617263/how-to-return-a-value-inside-a-loop-in-a-return-method?noredirect=1#comment29121536_19617263 about a failed return method, and people said it failed because the `ArrayList` stored no values. – Sue Oct 27 '13 at 11:34
  • @anubhava I've got `0` so I am sure nothing is stored in the list – Sue Oct 27 '13 at 11:38
  • @R.J it did go to `else if` because the `messageDialog` had popped up – Sue Oct 27 '13 at 11:55

2 Answers2

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);
Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225
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?

Community
  • 1
  • 1
DomLavoie
  • 81
  • 2
  • 4
  • 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? – Sue Oct 27 '13 at 12:01
  • 1
    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 classes = new ArrayList()` – DomLavoie Oct 27 '13 at 12:05
  • 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? – Sue Oct 27 '13 at 12:06
  • I see. Thanks! I've just checked List interface out. I don't know about this type before :) thanks for teaching me! – Sue Oct 27 '13 at 12:07
  • 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. – DomLavoie Oct 27 '13 at 12:10
  • Thanks you! It's just `List inferface` is fairly new to me, maybe I need some time to get familiar with it. But I need to finish the project asap...so I just hope I can make it work first. – Sue Oct 27 '13 at 12:13
  • I've just tried your edited code, and it seems it doesn't work. Now the GUI displays `null` instead of the string – Sue Oct 27 '13 at 12:24