CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Bug? Help?

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

    Bug? Help?

    So I'm trying to make it so whenever I click More Nouns it adds whatever is in nounbox1 to the arraylist nouns and then clear nounbox1. I looked at the error it says that its on line 237 which is when it adds nounbox1's text. Help!

    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.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.Box;
    import java.io.*;
    import java.net.*;
    public class PhrasebotMac implements ActionListener {
    	JTextArea midgui;
    	JButton phrasemaker;
    	Box boxleft;
    	
    	JTextField nounbox1;
    	JTextField nounbox2;
    	JTextField nounbox3;
    	
    	JTextField adjbox1;
    	JTextField adjbox2;
    	JTextField adjbox3;
    	
    	JTextField verbbox1;
    	JTextField verbbox2;
    	JTextField verbbox3;
    	
    	String[] nounarray;
    	String[] adjarray;
    	String[] verbarray;
    	
    	int countnoun;
    	int countadj;
    	int countverb;
    	
    	int ran1;
    	int ran2;
    	int ran3;
    	
    	String funny;
    	
    	ArrayList<String> nouns;
    	ArrayList<String> adjectives;
    	ArrayList<String> verbs;
    public static void main(String[] args){
    PhrasebotMac gui = new PhrasebotMac();
    gui.go();
    }
    public void go() {
    	JPanel pcenter = 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);
    	
    	
    	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("More Nouns");//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
    	JButton clearnoun = new JButton("Clear Nouns");//makes a new button called clearnoun with the text "Clear Nouns"
    	clearnoun.addActionListener(new clearnounListener());//clearnoun adds an actionlistener which is a clearnounlistener()
    	peast.add(clearnoun);//peast adds clearnoun
    	JLabel spacer2 = new JLabel(" ");//makes a new label called spacer2
    	peast.add(spacer2);//peast adds spacer2
    	JButton moreadj = new JButton("More Adjectives");//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
    	JButton clearadj = new JButton("Clear Adjectives");//makes a new button called clearadj with the text "Cear Adjectives"
    	clearadj.addActionListener(new clearadjListener());//clearadj adds an actionlistener which is a clearadjlistener()
    	peast.add(clearadj);//peast adds clearadj
    	JLabel spacer3 = new JLabel(" ");//makes a new label called spacer3
    	peast.add(spacer3);//peast adds
    	JButton moreverb = new JButton("More Verbs");//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
    	JButton clearverb = new JButton("Clear Verbs");//makes a new button called clearverb with the text "Clear Verbs"
    	clearverb.addActionListener(new clearverbListener());//clearverb adds an actionlistener which is a clearverbListener()
    	peast.add(clearverb);//peast adds clearverb
    	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.add(savewords);//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("Quickie Button 1");//makes a new button called quick1 with the text "Quickie Button 1"
    	quick1.addActionListener(new quick1Listener());//
    	peast.add(quick1);
    	JButton quick2 = new JButton("Quickie Button 2");
    	quick2.addActionListener(new quick2Listener());
    	peast.add(quick2);
    	JButton quick3 = new JButton("Quickie Button 3");
    	quick3.addActionListener(new quick3Listener());//100
    	peast.add(quick3);
    	
    	JPanel pwest = new JPanel();
    	pwest.setLayout(new BoxLayout(pwest,BoxLayout.Y_AXIS));
    	JPanel bigpanel2 = new JPanel();
    	bigpanel2.add(pwest);
    	
    	
    	
    	Font midfont = new Font("Comic Sans MS", Font.BOLD, 18);
    	midgui = new JTextArea(10,20);
    	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);
    	
    	
    	
    	
    	nounbox1 = new JTextField("Noun	");
    	nounbox2 = new JTextField("Noun	");
    	nounbox3 = new JTextField("Noun	");
    	JLabel spacer4 = new JLabel(" ");
    	pwest.add(nounbox1);
    	nounbox1.addActionListener(this);
    	pwest.add(nounbox2);
    	nounbox2.addActionListener(new nounbox2Listener());
    	pwest.add(nounbox3);
    	nounbox3.addActionListener(new nounbox3listener());
    	pwest.add(spacer4);
    	adjbox1 = new JTextField("Adjective  ");
    	adjbox2 = new JTextField("Adjective  ");
    	adjbox3 = new JTextField("Adjective  ");
    	JLabel spacer5 = new JLabel(" ");
    	pwest.add(adjbox1);
    	adjbox1.addActionListener(new adjbox1Listener());
    	pwest.add(adjbox2);
    	adjbox2.addActionListener(new adjbox2Listener());
    	pwest.add(adjbox3);
    	adjbox3.addActionListener(new adjbox3Listener());
    	pwest.add(spacer5);
    	verbbox1 = new JTextField("Verb  ");
    	verbbox2 = new JTextField("Verb  ");
    	verbbox3 = new JTextField("Verb  ");
    	pwest.add(verbbox1);
    	verbbox1.addActionListener(new verbbox1Listener());
    	pwest.add(verbbox2);
    	verbbox2.addActionListener(new verbbox2Listener());
    	pwest.add(verbbox3);
    	verbbox3.addActionListener(new verbbox3Listener());
    	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(800,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);
    
    }
    public void actionPerformed(ActionEvent e) {
    	
    	
    }
    class phrasemakerListener implements ActionListener{
    	public void actionPerformed(ActionEvent ev) {
    		try{
    			nouns = new ArrayList<String>();
    			adjectives = new ArrayList<String>();
    			verbs = new ArrayList<String>();
    			
    			nouns.add(nounbox1.getText());
    			nouns.add(nounbox2.getText());//184?
    			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());
    			
    			nounarray = nouns.toArray(new String[nouns.size()]);
    			adjarray = adjectives.toArray(new String[adjectives.size()]);
    			verbarray = verbs.toArray(new String[verbs.size()]);
    			
    			countnoun = nounarray.length;
    			countadj = adjarray.length;
    			countverb = verbarray.length;
    			
    			ran1 = (int) (Math.random() * countverb);
    			ran2 = (int) (Math.random() * countadj);
    			ran3 = (int) (Math.random() * countnoun);
    			
    			funny = adjarray [ran2]+ " " +  nounarray [ran3]+" " + verbarray [ran1]+" ";
    			
    		midgui.append(funny + "\n ");
    		phrasemaker.setText("Make Another!");
    		}
    		catch(Exception ex){
    		ex.printStackTrace();	
    		}
    	}
    	
    }
    
    class morenounListener implements ActionListener{
    
    	public void actionPerformed(ActionEvent e) {
    		try{
    		nouns.add(nounbox1.getText());
    		nounbox1.setText("");
    	}catch(Exception x){
    		x.printStackTrace();
    	}
    	}
    	
    }
    class clearnounListener implements ActionListener{
    
    	public void actionPerformed(ActionEvent e) {
    		
    		
    	}
    	
    }
    class moreadjListener implements ActionListener{
    
    	public void actionPerformed(ActionEvent e) {
    		
    		
    	}
    	
    }
    class clearadjListener implements ActionListener{
    
    	public void actionPerformed(ActionEvent e) {
    		 
    		
    	}
    	
    }
    class moreverbListener implements ActionListener{
    
    	public void actionPerformed(ActionEvent e) {
    		 
    		
    	}
    	
    }
    class clearverbListener implements ActionListener{
    
    	public void actionPerformed(ActionEvent e) {
    		 
    		
    	}
    	
    }
    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{
    			 FileWriter writer = new FileWriter("PhraseGallery.txt");
    			 writer.write(midgui.getText());
    			 writer.close();
    			 midgui.append("phrase saved");
    		 }catch(IOException x){
    			 x.printStackTrace();
    		 }
    		
    	}
    	
    }
    class quick1Listener implements ActionListener{
    
    	public void actionPerformed(ActionEvent e) {
    		 
    		
    	}
    	
    }
    class quick2Listener implements ActionListener{
    
    	public void actionPerformed(ActionEvent e) {
    		 
    		
    	}
    	
    }
    class quick3Listener implements ActionListener{
    
    	public void actionPerformed(ActionEvent e) {
    		 
    		
    	}
    	
    }
    class nounbox2Listener implements ActionListener{
    
    	public void actionPerformed(ActionEvent e) {
    		 
    		
    	}
    	
    }
    class nounbox3listener implements ActionListener{
    
    	public void actionPerformed(ActionEvent e) {
    		 
    		
    	}
    	}
    class adjbox1Listener implements ActionListener{
    
    	public void actionPerformed(ActionEvent e) {
    		 
    		
    	}
    	
    }
    class adjbox2Listener implements ActionListener{
    
    	public void actionPerformed(ActionEvent e) {
    		 
    		
    	}
    	
    }
    class adjbox3Listener implements ActionListener{
    
    	public void actionPerformed(ActionEvent e) {
    		 
    		
    	}
    	
    }
    class verbbox1Listener implements ActionListener{
    
    	public void actionPerformed(ActionEvent e) {
    		 
    		
    	}
    	
    }
    class verbbox2Listener implements ActionListener{
    
    	public void actionPerformed(ActionEvent e) {
    		
    		
    	}
    	
    }
    class verbbox3Listener implements ActionListener{
    
    	public void actionPerformed(ActionEvent e) {
    		
    		
    	}
    	
    }
    class midguiListener implements ActionListener{
    
    	public void actionPerformed(ActionEvent e) {
    		
    		
    	}
    	
    }
    }
    Error:


    java.lang.NullPointerException
    at PhrasebotMac$morenounListener.actionPerformed(PhrasebotMac.java:238)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1882)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2202)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.java:5602)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3135)
    at java.awt.Component.processEvent(Component.java:5367)
    at java.awt.Container.processEvent(Container.java:2010)
    at java.awt.Component.dispatchEventImpl(Component.java:4068)
    at java.awt.Container.dispatchEventImpl(Container.java:2068)
    at java.awt.Component.dispatchEvent(Component.java:3903)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4256)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3936)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3866)
    at java.awt.Container.dispatchEventImpl(Container.java:2054)
    at java.awt.Window.dispatchEventImpl(Window.java:1801)
    at java.awt.Component.dispatchEvent(Component.java:3903)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
    at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:176)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
    Last edited by Shaken_Earth; June 1st, 2009 at 04:53 PM.

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

    Re: Bug? Help?

    What error ? If you're asking for help on an error, post up the full error message and stack trace.

    Questions are the important thing, answers are less important. Learning to ask a good question is the heart of intelligence. Learning the answer---well, answers are for students. Questions are for thinkers...
    R. Schank
    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.

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

    Re: Bug? Help?

    Okay I posted the error.

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

    Re: Bug? Help?

    OK, this is how I would debug this error:

    The error says there's a NullPointerException at line 238 in PhrasebotMac.java (in 'morenounListener'). On that line are two variables, 'nouns' and 'nounbox1', so one of them must be null to cause the exception:

    nouns.add(nounbox1.getText());

    If you look to find where they get initialized, you'll see that 'nounbox1' is initialized in the 'go' method, which is always called before anything else happens, so it must be 'nouns' that is null (you can put a print statement before line 238 to check this if you don't have a debugger). Variable 'nouns' is initialized in the 'phrasemakerListener' action, so it will only be initialized if this action is called. I guess you selected 'More nouns' before 'Make A Phrase', so 'nouns' never got initialized. I suggest you initialize 'nouns' when you initialize 'nounbox1' to avoid this.

    The general solution is to make sure all your class member variables are initialized as soon as possible, preferably in the constructor, so that they will never be null when you try to use them. This is what a constructor is for.

    Testing can show the presence of errors, but not their absence...
    E. Dijkstr
    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: Bug? Help?

    Thanks! That helped a lot!

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