CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    May 2009
    Location
    New Jersey, US
    Posts
    42

    Unhappy null null null and no components show up at launch

    So whenever I click phrasemaker in my program it prints "null null null" in midgui. It should be returning a random String from nouns, adjectives, and verbs. I don't understand. It's supposed to print the contents of currentAra which apparently is null. Why is it null? It has the Strings because it adds the text of all the nounboxes, adjboxes, and verbboxes when you click phrasemaker. I also have a Phrase class I made the other day which could also be causing the problem.

    here's the code:
    Code:
    //Phrasebot is property of shakenearth.com
    //Code by Thomas Lisankie
    //You may not use any of this code
    //you may not distribute this program either
    
    import java.awt.*;//imports java.awt
    import java.awt.event.ActionEvent;//imports java.awt.event.ActionEvent
    import java.awt.event.ActionListener;//imports java.awt.event.ActionListener
    import java.util.*;//imports java.util
    import javax.swing.*;//imports javax.swing
    import javax.swing.event.ListSelectionEvent;//imports javax.swing.event.ListSelectionEvent
    import javax.swing.event.ListSelectionListener;//imports javax.swing.event.ListSelectionListener
    import java.io.*;//imports java.io
    import java.net.*;//imports java.net
    public class Phrasebot{
    	
    	JTextArea midgui;//Makes a new instance variable of type JTextArea called midgui with a value of null
    	JButton phrasemaker;//Makes a new instance variable of type Jbutton called phrasemaker with a value of null
    	Box boxleft;//makes a new instance variable of type Box called boxleft with a value of null
    	
    	JTextField nounbox1;//makes an instance variable of type JTextField called nounbox1 with a value of null
    	JTextField nounbox2;//makes an instance variable of type JTextField called nounbox2 with a value of null
    	JTextField nounbox3;//makes an instance variable of type JTextField called nounbox3 with a value of null
    	
    	JTextField adjbox1;//makes an instance variable of type JTextField called adjbox1 with a value of null
    	JTextField adjbox2;//makes an instance variable of type JTextField called adjbox2 with a value of null
    	JTextField adjbox3;//makes an instance variable of type JTextField called adjbox3 with a value of null
    	
    	JTextField verbbox1;//makes an instance variable of type JTextField called verbbox1 with a value of null
    	JTextField verbbox2;//makes an instance variable of type JTextField called verbbox2 with a value of null
    	JTextField verbbox3;//makes an instance variable of type JTextField called verbbox3 with a value of null
    	
    	HashSet<String> nouns = new HashSet<String>();//makes new HashSet of type String called nouns
    	HashSet<String> adjectives = new HashSet<String>();//makes new HashSet of type String called adjectives
    	HashSet<String> verbs = new HashSet<String>();//makes new HashSet of type String called verbs
    	
    	JList list;//makes a new instance variable of type JList called list with a value of null
    
    	Phrase funny1;//makes a new instance variable of type String called funny1 with a value of null
    	Phrase funny2;
    	Phrase funny3;
    	Phrase funny4;
    	
    	JTextField savefield;//makes a new instance variable of type JTextField called savefield with a value of null
    	JButton save;//makes a new instance variable of type JButton called save with a value of null
    	
    	
    	
    	Phrase currentAra;//makes a new instance variable of type String called p with a value of null
    	String[] nounarray;
    	String[] adjarray;
    	String[] verbarray;
    	int countnoun;
    	int countadj;
    	int countverb;
    	int ran1;
    	int ran2;
    	int ran3;
    	String a;
    	String b;
    	String c;
    	Phrase funny;
    	
    	
    public static void main(String[] args){
    try{
    	
    	Phrasebot bot = new Phrasebot();//makes a new instance of Phrasebot called bot
    
    }catch(Error Ex){
    	Ex.printStackTrace();
    }
    }
    public Phrasebot(){
    	nounbox1 = new JTextField("Noun	");//sets nounbox1's value to a new JTextField
    	nounbox2 = new JTextField("Noun	");//sets nounbox2's value to a new JTextField
    	nounbox3 = new JTextField("Noun	");//sets nounbox3's value to a new JTextField
    	adjbox1 = new JTextField("Adjective  ");//sets adjbox1's value to a new JTextField
    	adjbox2 = new JTextField("Adjective  ");//sets adjbox2's value to a new JTextField
    	adjbox3 = new JTextField("Adjective  ");//sets adjbox3's value to a new JTextField
    	verbbox1 = new JTextField("Verb  ");//sets verbbox1's value to a new JTextField
    	verbbox2 = new JTextField("Verb  ");//sets verbbox2's value to a new JTextField
    	verbbox3 = new JTextField("Verb  ");//sets verbbox3's value to a new JTextField
    	a = "noun";
    	b = "adjective";
    	c = "verb";
    	
    	nouns.add(a);//adds the current text of nounbox1 to nouns
    
    	adjectives.add(b);//adds the current text of adjbox1 to nouns
    
    	verbs.add(c);//adds the current text of verbbox1 to nouns
    	nounarray = nouns.toArray(new String[nouns.size()]);/*makes an array of Strings called nounarray with the value of whatever
    	 is in nouns at the moment*/
    	adjarray = adjectives.toArray(new String[adjectives.size()]);/* makes an array of Strings called adjarray with the value of
    	 whatever is in adjectives at the moment*/
    	verbarray = verbs.toArray(new String[verbs.size()]);/*makes an array of Strings called verbarray with the value of whatever 
    	is in verbs at the moment*/
    	countnoun = nounarray.length;//makes a new variable of type int called countnoun with the value of nounarray's length
    	countadj = adjarray.length;//makes a new variable of type int called countadj with the value of adjarray's length
    	countverb = verbarray.length;//makes a new variable of type int called countverb with the value of verbarray's length
    	
    	ran1 = (int) (Math.random() * countverb);//makes a new variable of type int called ran1 with a value of a random word from countverb
    	ran2 = (int) (Math.random() * countadj);//makes a new variable of type int called ran2 with a value of a random word from countadj
    	ran3 = (int) (Math.random() * countnoun);//makes a new variable of type int called ran3 with a value of a random word from countnoun
    	funny1 = new Phrase(adjarray[ran2]+ " ", nounarray[ran3]+ " ", verbarray[ran1]);/*sets funny1's value to a random word from adjarray,
    	 nounarray, and verbarray*/
    	funny2 = new Phrase(nounarray [ran3]+ " ",adjarray [ran2]+ " ",verbarray [ran1]);
    	
    	funny3 = new Phrase(adjarray [ran2]+ " ",verbarray [ran1]+ " ",nounarray [ran3]);
    	funny4 = new Phrase(verbarray[ran1]+ " ",adjarray[ran2]+ " ",nounarray[ran3]);
    	
    	
    	
    
    	JPanel pcenter = new JPanel();//makes a new variable of type JPanel called pcenter with a value of a new JPanel()
    	JFrame frame = new JFrame("Phrasebot");//makes a new JFrame called "Phrasebot"
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//sets the default close operation to EXIT_ON_CLOSE
    	boxleft = new Box(BoxLayout.Y_AXIS);//sets boxleft's value to a new box on the y_axis
    	
    	
    	JPanel bigpanel = new JPanel();//makes a new JPanel called bigpanel
    	bigpanel.setLayout(new BoxLayout(bigpanel,BoxLayout.Y_AXIS));//sets bigpanel's default layout to BoxLayout
    	JPanel spacer = new JPanel();//makes a new JPanel called spacer
    	spacer.setLayout(new BoxLayout(spacer, BoxLayout.Y_AXIS));//sets spacer's layout to BoxLayout
    	JLabel label = new JLabel(" ");//makes a new JLabel
    	spacer.add(label);//adds label to spacer
    	bigpanel.add(spacer);//adds spacer to bigpanel
    	JPanel peast = new JPanel();//makes a new panel called peast
    	bigpanel.add(peast);//bigpanel adds peast
    	peast.setLayout(new  BoxLayout(peast,BoxLayout.Y_AXIS));//sets peast's layout to boxlayout
    	JButton morenoun = new JButton("Add a Noun");//makes a new button called more noun with the text "More Nouns"
    	morenoun.addActionListener(new morenounListener());//morenoun adds an actionlistener which is a morenounlistener()
    	peast.add(morenoun);//peast adds morenoun
    	
    	JLabel spacer2 = new JLabel(" ");//makes a new label called spacer2
    	peast.add(spacer2);//peast adds spacer2
    	JButton moreadj = new JButton("Add an Adjective");//makes a new button called moreadj with the text "More Adjectives"
    	moreadj.addActionListener(new moreadjListener());//moreadj adds an actionlistener which is a moreadjlistener()
    	peast.add(moreadj);//peast adds moreadj
    	
    	JLabel spacer3 = new JLabel(" ");//makes a new label called spacer3
    	peast.add(spacer3);//peast adds spacer3
    	JButton moreverb = new JButton("Add a Verb");//makes a new button called moreverb with the text "More Verbs"
    	moreverb.addActionListener(new moreverbListener());//moreverb adds an actionlistener which is a moreverblistener()
    	peast.add(moreverb);//peast adds moreverb
    	
    	
    	JLabel savespacer = new JLabel(" ");//makes a new label called savespacer
    	peast.add(savespacer);//peast adds savespacer
    	JButton savewords = new JButton("Save Words");//makes a new button called save words
    	savewords.addActionListener(new savewordsListener());//savewords adds an actionlistener which is a savewordsListener()
    	//peast adds savewords
    	JButton savesession = new JButton("Save Phrases");//creates a new button called savesession with the text "Save Phrases"
    	savesession.addActionListener(new savesessionListener());//savesession adds an actionlistener which is a savesessionListener()
    	peast.add(savesession);//peast adds savesession
    	JLabel quickspacer = new JLabel(" ");//makes a new label called quickspacer
    	peast.add(quickspacer);//peast adds quickspacer
    	JButton quick1 = new JButton("Arrangements");//makes a new button called quick1 with the text "Quickie Button 1"
    	quick1.addActionListener(new quick1Listener());//quick1 adds an actionListener which is a quick1Listener()
    	peast.add(quick1);//peast adds quick1
    	JButton restart = new JButton("Restart");//makes a new variable of type JButton called restart with a value of a new JButton
    	restart.addActionListener(new quick2Listener());//restart adds an actionListener which is a quick2Listener()
    	peast.add(restart);//peast adds restart
    	
    	JPanel pwest = new JPanel();//makes a new variable of type JPanel called pwest with a value of a new JPanel
    	pwest.setLayout(new BoxLayout(pwest,BoxLayout.Y_AXIS));//pwest set's its layout to the y axis
    	JPanel bigpanel2 = new JPanel();//makes a new variable of type JPanel called bigpanel2 with a value of a new JPanel
    	bigpanel2.add(pwest);//bigpanel2 adds pwest
    	
    	
    	
    	Font midfont = new Font("Comic Sans MS", Font.BOLD, 18);//makes a new variable of type Font called mid font with a value of a new Font
    	midgui = new JTextArea(10,20);//sets midgui's value to a new JTextArea
    	midgui.setEditable(false);
    	midgui.setLineWrap(true);
    	midgui.setFont(midfont);
    	JScrollPane scroll = new JScrollPane(midgui);
    	scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    	scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    	pcenter.add(scroll);
    	
    	
    	currentAra = funny1;
    	
    	
    	JLabel spacer4 = new JLabel(" ");
    	pwest.add(nounbox1);
    	
    	pwest.add(nounbox2);
    	
    	pwest.add(nounbox3);
    	
    	pwest.add(spacer4);
    	
    	JLabel spacer5 = new JLabel(" ");
    	pwest.add(adjbox1);
    
    	pwest.add(adjbox2);
    
    	pwest.add(adjbox3);
    	
    	pwest.add(spacer5);
    	
    	pwest.add(verbbox1);
    
    	pwest.add(verbbox2);
    
    	pwest.add(verbbox3);
    
    	JPanel psouth = new JPanel();
    	JPanel bigpanel3 = new JPanel();
    	bigpanel3.add(psouth);
    	phrasemaker = new JButton("Make A Phrase!");
    	phrasemaker.addActionListener(new phrasemakerListener());
    	psouth.add(phrasemaker);
    	boxleft.add(nounbox1);
    	boxleft.add(nounbox2);
    	boxleft.add(nounbox3);
    	Box.createVerticalGlue();
    	boxleft.add(adjbox1);
    	boxleft.add(adjbox2);
    	boxleft.add(adjbox3);
    Box.createVerticalGlue();
    	boxleft.add(verbbox1);
    	boxleft.add(verbbox2);
    	boxleft.add(verbbox3);
    	frame.setSize(1000,600);//sets the size of the JFrame
    	frame.setVisible(true);//sets the ability to see the JFrame
    	
    	
    	frame.getContentPane().add(BorderLayout.EAST, bigpanel);
    	frame.getContentPane().add(BorderLayout.WEST, boxleft);
    	frame.getContentPane().add(BorderLayout.SOUTH, bigpanel3);
    	frame.getContentPane().add(BorderLayout.CENTER, scroll);
    	
    }
    
    
    class phrasemakerListener implements ActionListener{
    	public void actionPerformed(ActionEvent ev) {
    		try{
    		
    			nouns.add(nounbox1.getText());
    			nouns.add(nounbox2.getText());
    			nouns.add(nounbox3.getText());
    			adjectives.add(adjbox1.getText());
    			adjectives.add(adjbox2.getText());
    			adjectives.add(adjbox3.getText());
    			verbs.add(verbbox1.getText());
    			verbs.add(verbbox2.getText());
    			verbs.add(verbbox3.getText());
    			
    			
    		midgui.append(currentAra + "\n ");
    		phrasemaker.setText("Make Another!");
    		}
    		catch(Exception ex){
    		ex.printStackTrace();	
    		}
    	}
    	
    }
    
    class morenounListener implements ActionListener{
    
    	public void actionPerformed(ActionEvent e) {
    		
    		nouns.add(nounbox1.getText());
    		nounbox1.setText("");
    		
    	}
    	
    }
    
    class moreadjListener implements ActionListener{
    
    	public void actionPerformed(ActionEvent e) {
    		adjectives.add(adjbox1.getText());
    		adjbox1.setText("");
    		
    	}
    	
    }
    
    class moreverbListener implements ActionListener{
    
    	public void actionPerformed(ActionEvent e) {
    		verbs.add(verbbox1.getText());
    		verbbox1.setText(""); 
    		
    	}
    	
    }
    
    class savewordsListener implements ActionListener{
    
    	public void actionPerformed(ActionEvent e) {
    		try{
    			FileOutputStream fileout = new FileOutputStream("words.pb");
    		ObjectOutputStream out = new ObjectOutputStream(fileout);
    		
    		out.writeObject(nouns);
    		out.close();
    		midgui.append("words saved");
    		}catch(IOException ex){
    			ex.printStackTrace();
    		}
    	}
    	
    }
    class savesessionListener implements ActionListener{
    
    	public void actionPerformed(ActionEvent e) {
    		 try{
    			 JFrame frame = new JFrame();
    			 savefield = new JTextField("Untitled.txt");
    			 JButton save = new JButton("Save");
    			 save.addActionListener(new savebuttonListener());
    			 frame.setVisible(true);
    			 frame.setLayout(new FlowLayout());
    			 frame.add(savefield);
    			 frame.add(save);
    			 frame.setSize(400,75);
    			 
    		 }catch(Exception x){
    			 x.printStackTrace();
    		 }
    		
    	}
    	
    }
    class quick1Listener implements ActionListener{
    
    	public void actionPerformed(ActionEvent e) {
    		
    		
    		JFrame frame = new JFrame("Arrangements");
    	
    	
    	list = new JList();
    	DefaultListModel dlm = new DefaultListModel();
    ListItem item1 = new ListItem("ANV", funny1);
    ListItem item2 = new ListItem("NAV", funny2);
    ListItem item3 = new ListItem("AVN", funny3);
    ListItem item4 = new ListItem("VAN", funny4);
    	for(int i = 0; i<1;i++){
    	    // add a new ListItem containing display text and a new value
    	    dlm.addElement(item1);
    	    dlm.addElement(item2);
    	    dlm.addElement(item3);
    	    dlm.addElement(item4);
    	}
    	list.setModel(dlm);
    	
    	list.setSelectedIndex(0);
    	list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
    	list.addListSelectionListener(new listListener());
    	JButton set = new JButton("Done");
    	set.addActionListener(new setListener());
    	frame.getContentPane().add(BorderLayout.CENTER, list);
    	frame.getContentPane().add(BorderLayout.SOUTH, set);
    	frame.setSize(250,250);
    	frame.setVisible(true);
    		
    	}
    	
    }
    class quick2Listener implements ActionListener{
    
    	public void actionPerformed(ActionEvent e) {
    		 nouns.clear();
    		 adjectives.clear();
    		 verbs.clear();
    		 nounbox1.setText("");
    		 nounbox2.setText("");
    		 nounbox3.setText("");
    		 adjbox1.setText("");
    		 adjbox2.setText("");
    		 adjbox3.setText("");
    		 verbbox1.setText("");
    		 verbbox2.setText("");
    		 verbbox3.setText("");
    		 midgui.setText("");
    	}
    	
    }
    class quick3Listener implements ActionListener{
    
    	public void actionPerformed(ActionEvent e) {
    		 
    		
    	}
    	
    }
    
    class midguiListener implements ActionListener{
    
    	public void actionPerformed(ActionEvent e) {
    		
    		
    	}
    	
    }
    class savebuttonListener implements ActionListener{
    
    	public void actionPerformed(ActionEvent e) {
    		try{
    			String fileName = savefield.getText();
    		FileWriter writer = new FileWriter(fileName);
    		 writer.write(midgui.getText());
    		 writer.close();
    		 
    		}catch(IOException ex){
    			ex.printStackTrace();
    		}
    		
    	}
    	
    }
    public void establishConnection(){
    	try{
    	Socket sock = new Socket("192.168.0.195", 5620);
    	
    	}catch(Exception ex){
    		midgui.append("Couldn't get connection");
    	}
    }
    
    class setListener implements ActionListener{
    
    	public void actionPerformed(ActionEvent e) {
    		
    		
    	}
    	
    }
    class listListener implements ListSelectionListener{
    
    	public void valueChanged(ListSelectionEvent e) {
    		if(!e.getValueIsAdjusting()){
    			
    			ListItem selection = (ListItem) list.getSelectedValue();
    			
    			
    			setCurrentAra(selection.getValue());
    		}else{
    			
    		}
    		
    	}
    	
    }
    public void setCurrentAra(Phrase thingy){
    	currentAra = thingy;
    }
    }
    Code:
    public class Phrase {
    String noun;
    String adjective;
    String verb;
    String all =adjective + " "+noun+" "+verb;
    public Phrase(String n,String a,String v){
    	noun = n;
    	adjective = a;
    	verb = v;
    }
    
    String getNoun(){
    	return noun;
    }
    String getAdj(){
    	return adjective;
    }
    String getVerb(){
    	return verb;
    }
    public String toString(){
    	return all;	
    }
    }
    Another problem I'm having is that no components show up when I launch my program. All that shows up is the JFrame. To fix this I usually make the window a bit bigger and then the components show up. I don't know what could be causing this! HELP!!!

  2. #2
    Join Date
    Apr 2007
    Posts
    442

    Re: null null null and no components show up at launch

    As to null null and null, you initialize the variable all, when no values are present for the three string variables composing it. They are initialized only in the constructor. Move initializing all -variable also inside the constructor and you are home free.

    as to frame issue. Have to say the constructor was too large to read through. However, quick plaster on behaviour such as you explained... set the frame visible first, and set size after. This will force resize and repaint.

  3. #3
    Join Date
    May 2009
    Location
    New Jersey, US
    Posts
    42

    Re: null null null and no components show up at launch

    thanks this helped a lot! but the thing is it doesn't show anything but the variables a, b, and c. Plus when I click arrangements and change the arrangement it doesn't change until you click another arrangement. It's pretty messed up.

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: null null null and no components show up at launch

    Quote Originally Posted by Shaken_Earth View Post
    It's pretty messed up.
    The source certainly isn't as clear as it could be - I strongly recommend that you get rid of the trivially obvious comments - they just get in the way, and can be positively misleading, e.g:
    Code:
    Phrase currentAra;//makes a new instance variable of type String called p with a value of null
    Ideally, comments should be kept to a minimum to explain code that isn't obvious or self-explanatory. Kernighan & Plaugher said "Don't document bad code - rewrite it." Consider how to write or rewrite code so comments are not required.

    The way to make programs easy to read is not to stuff them with comments... A good programming language ought to be better for explaining software than English. You should only need comments when there is some kind of kludge you need to warn readers about, just as on a road there are only arrows on parts with unexpectedly sharp curves...
    P. Graham
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  5. #5
    Join Date
    May 2009
    Location
    New Jersey, US
    Posts
    42

    Re: null null null and no components show up at launch

    Thanks for the advice. But could you or someone else help me find what is making it only show Strings a, b, and c? It feels like I'm searching for a needle in a haystack.

  6. #6
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: null null null and no components show up at launch

    Quote Originally Posted by Shaken_Earth View Post
    But could you or someone else help me find what is making it only show Strings a, b, and c? It feels like I'm searching for a needle in a haystack.
    C'mon dude - think about it...

    Just look what happens when you push the phraseMaker button - you add the same 3 nouns, 3 verbs, and 3 adjectives to their respective HashSets every time (why?), then display 'currentAra' (whatever that's supposed to mean), which isn't changed by that method - and when it displays Phrase 'currentAra', it outputs the String variable 'all' which is always going to be three nulls because it gets created and initialised even before the constructor values are assigned, and is never updated after that.

    All it takes is for you to step through the code by hand with pencil and paper for a few minutes (e.g. starting where you think the problem may show), and it should be obvious. You really need to be able to debug your own code. Even better, learn how to use an IDE like NetBeans or Eclipse to debug your code - you can step through it looking at the values of all your variables, watching them change. Think about writing code that you can understand, follow, and debug easily.

    As soon as we started programming, we found out to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs...
    M. Wilkes
    Last edited by dlorde; August 12th, 2009 at 03:43 AM.
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  7. #7
    Join Date
    May 2009
    Location
    New Jersey, US
    Posts
    42

    Re: null null null and no components show up at launch

    I don't add the same 3 nouns, adjectives, and verbs everytime because the user is supposed to enter new ones. Besides, a HashSet puts in absolutely no duplicates so the probability of the same phrase over and over again goes down. currentAra means 'Current Arangement'. I changed it so 'all' is initialized in the constructor of the Phrase object.

    I also figured out what the bug is. It all has to do with the arrays and when they're being initialized. The reason it never uses anything besides a, b, and c is because they're the only ones being added in the constructor when it's initialized so when i click the phraseMaker button it's adding the the words to there HashSets not their arrays. and since the funnys are the arrays (not the HashSets) and that's what midgui is supposed to be displaying it's only displaying a, b, and c. The only problem is I don't know how to fix the bug =P.

  8. #8
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: null null null and no components show up at launch

    Quote Originally Posted by Shaken_Earth View Post
    I don't add the same 3 nouns, adjectives, and verbs everytime because the user is supposed to enter new ones. Besides, a HashSet puts in absolutely no duplicates so the probability of the same phrase over and over again goes down.
    I was just trying to point out that the nouns, adjectives and verbs HashSets should be updated when nouns, adjectives and verbs are entered, not in a separate listener method that has nothing to do with them.

    I also figured out what the bug is. It all has to do with the arrays and when they're being initialized. The reason it never uses anything besides a, b, and c is because they're the only ones being added in the constructor when it's initialized so when i click the phraseMaker button it's adding the the words to there HashSets not their arrays. and since the funnys are the arrays (not the HashSets) and that's what midgui is supposed to be displaying it's only displaying a, b, and c. The only problem is I don't know how to fix the bug.
    I think there are design problems - as I said above, going by its name, the PhraseMakerListener is there to make a phrase, not to update the words storage. Currently the PhraseMakerListener doesn't even make a phrase, it just displays an existing one...

    I don't know why you've got both HashSets and arrays to store these words, but if they must be updated with new words, it seems sensible to do it when the new words are input. The arrays only seem to be used when generating new random phrases, so it would probably be simpler and easier to create them temporarily from the HashSets when a new random phrase is required, instead of storing them as class fields and trying to keep them synchronized with the HashSets.

    It's important to remember that, in a good design, a class or method should have a single clear and well-defined task, and that its name should indicate what that task is.

    Points arise in most development where enough is known about the code and how it should work that it can be restructured into a simple, clearer, more effective design. This is known as refactoring. It is a programming skill in its own right, and ideally it should be done often, to keep the design optimized and avoid awkward problems later. I would recommend that you consider refactoring your code before it gets out of hand.

    Any fool can write code that a computer can understand. Good programmers write code that humans can understand...
    M. Fowler, "Refactoring: Improving the Design of Existing Code"
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  9. #9
    Join Date
    May 2009
    Location
    New Jersey, US
    Posts
    42

    Re: null null null and no components show up at launch

    Yes I think there are design problems too. I never designed Phrasebot from the start, I just started writing code which pretty much led to the mess today.

    Thanks I'll try using it as a local variable instead of a field.

    I think I should try refactoring. The design is too messy.

  10. #10
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: null null null and no components show up at launch

    Quote Originally Posted by Shaken_Earth View Post
    I never designed Phrasebot from the start, I just started writing code which pretty much led to the mess today.
    Unfortunately this is the pretty much the worst possible approach because not only is there no design, but you have to try to figure out what to do on-the-fly while translating it into Java. Writing Java code is tricky when you're starting out, without having to invent a program as you go.

    Get the revised design worked out fully on paper before writing any code. You now understand most of the technical challenges, so you can focus on a good structure. Keep each object and function as simple as possible, so you can easily follow what it does. Make sure the names describe exactly what the named item does. Once you know all the objects involved, their roles, data, functions, and how they will co-operate, you can translate them into Java classes, data and methods.

    There are two ways of constructing a software design. One way is to make it so simple that there are obviously no deficiencies. And the other way is to make it so complicated that there are no obvious deficiencies...
    C.A.R. Hoare
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured