Java Programmers -- run time error

Discussion in 'OT Technology' started by kcarlson78, Apr 23, 2003.

  1. kcarlson78

    kcarlson78 Meanwhile, Jesus continued to shake his head in ex

    Joined:
    Jun 27, 2002
    Messages:
    4,490
    Likes Received:
    0
    Location:
    Phoenix, AZ
    the applet runs, and does what it's supposed to do.. but it dumps all kinds of exception garbage into my command window when the "Show List" Button is pressed (see blue).. it only does it with that function.. not with the other actionEvents.. :confused:

    anybody have any thoughts? I'm sure I missed something stupid here..

    Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.applet.*;
    
    
    public class BookArray extends JApplet implements ActionListener
    {
    	Book[] array = new Book[10];
    	JButton display, addBook;
    	JTextField bTitle;
    	JRadioButton fic, non;
    	JLabel fiction, nonfic; 
    	Container c;
    	int arrayPlace = 0;
    	
    	
    	public void init()
    	{
    		display = new JButton("Show List");
    		display.addActionListener(this);
    		
    		addBook = new JButton("Add Book");
    		addBook.addActionListener(this);
    		
    		bTitle = new JTextField("Book Title", 50);
    		
    		fic = new JRadioButton();
    		fic.addActionListener(this);
    		fic.setSelected(true);
    		
    		non = new JRadioButton();
    		non.addActionListener(this);
    		
    		fiction = new JLabel("Fiction");
    		nonfic = new JLabel("Non-Fiction");
    		
    		c = getContentPane();
    		c.setLayout(new FlowLayout(FlowLayout.LEFT));
    
    		c.add(bTitle);			// title input text field
    		c.add(fic);				// radio button to note fiction
    		c.add(fiction);		// label to display "Fiction" (relating to fic)
    		c.add(non);				// radio button to not non-fiction
    		c.add(nonfic);			// label to display "Non-Fiction" (relating to non)
    		c.add(addBook);		// button to add a book title to list
    		c.add(display);		// button to display book list
    	}	// init end
    	
    	public void actionPerformed(ActionEvent e)
    	{
    [color=red]// this is the action in question:[/color][color=blue]
    		if(e.getSource() == display)			// if show list button is pressed
    		{
    			printList();
    		}[/color]else
    		if(e.getSource() == addBook)			// if add book button is pressed
    		{
    			if(fic.isSelected())					// if fic radio button is clicked
    			{
    				array[arrayPlace] = new Fiction(bTitle.getText());
    				arrayPlace++;
    			}
    			else
    			if(non.isSelected())					// if nonfic radio button if clicked
    			{
    				array[arrayPlace] = new NonFiction(bTitle.getText());
    				arrayPlace++;
    			}
    		}else
    		if(e.getSource()== fic && non.isSelected())		// when fic radio button is clicked 
    		{							             // and non radio button is already selected
    			non.setSelected(false);			// unselect non
    		}else		
    		if(e.getSource()== non && fic.isSelected())		// when non radio button is clicked 
    		{								// and fic radio button is already selected
    			fic.setSelected(false);			//unselect fic
    		}
    	} // actionPerformed end
    	
    	[color=blue]public void printList()			// display the array of book titles
    	{
    		Graphics g = getGraphics();
    		int y=130;
    	
    		for(int i=0; i<arrayPlace+1; i++)	
    		{
    			g.drawString(array[i].getTitle() + "  $ " + array[i].getPrice(), 50, y);
    			y+=10;
    		}		
    	}[/color] // end printList()	
    	
    } // BookArray class end
    Code:
            at java.awt.Component.processMouseEvent(Component.java:5021)
            at java.awt.Component.processEvent(Component.java:4818)
            at java.awt.Container.processEvent(Container.java:1380)
            at java.awt.Component.dispatchEventImpl(Component.java:3526)
            at java.awt.Container.dispatchEventImpl(Container.java:1437)
            at java.awt.Component.dispatchEvent(Component.java:3367)
            at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3214
    )
            at java.awt.LightweightDispatcher.processMouseEvent(Container.java:2929)
    
            at java.awt.LightweightDispatcher.dispatchEvent(Container.java:2859)
            at java.awt.Container.dispatchEventImpl(Container.java:1423)
            at java.awt.Component.dispatchEvent(Component.java:3367)
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:445)
            at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh
    read.java:190)
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
    ad.java:144)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:130)
    
            at java.awt.EventDispatchThread.run(EventDispatchThread.java:98)
    
    
     
  2. kcarlson78

    kcarlson78 Meanwhile, Jesus continued to shake his head in ex

    Joined:
    Jun 27, 2002
    Messages:
    4,490
    Likes Received:
    0
    Location:
    Phoenix, AZ
    :uh:

    nevermind.. catch the exception and ignore it....

    Code:
    
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.applet.*;
    
    
    public class BookArray extends JApplet implements ActionListener
    {
    	Book[] array = new Book[10];
    	JButton display = new JButton("Show List");
    	JButton addBook = new JButton("Add Book");
    	JTextField bTitle = new JTextField("Book Title", 50);
    	JRadioButton fic = new JRadioButton("Fiction", true);
    	JRadioButton non = new JRadioButton("Non-Fiction", false);
    	int arrayPlace = 0;
    	Container c;
    		
    	public void init()
    	{
    		display.addActionListener(this);
    		addBook.addActionListener(this);
    		fic.addActionListener(this);
    		non.addActionListener(this);
    		
    		c = getContentPane();
    		c.setLayout(new FlowLayout(FlowLayout.LEFT));
    
    		c.add(bTitle);				c.add(fic);				c.add(non);				c.add(addBook);
    		c.add(display);
    	}	
    	public void actionPerformed(ActionEvent e)
    	{
    		if(e.getSource() == display)				{
    			printList();
    		}
    		else
    		if(e.getSource() == addBook && fic.isSelected())		{																	
    			array[arrayPlace++] = new Fiction(bTitle.getText());
    		}
    		else
    		if(e.getSource() == addBook && non.isSelected())		{																	
    			array[arrayPlace] = new NonFiction(bTitle.getText());
    			arrayPlace++;
    		}
    		else
    		if(e.getSource()== fic && non.isSelected())
    		{															
    			non.setSelected(false);				}
    		else		
    		if(e.getSource()== non && fic.isSelected())			{											fic.setSelected(false);				}
    	}	
    	public void printList()				{
    		Graphics g = getGraphics();
    		int y=130;
    		[color=blue]try
    		{[/color]
    			for(int i=0; i<arrayPlace+1; i++)	
    			{
    				g.drawString(array[i].getTitle() + "  $ " + array[i].getPrice(), 50, y);
    				y+=10;
    			}
    		[color=blue]}
    		catch(NullPointerException e)
    		{
    		}[/color]		
    	}	
    } 
     

Share This Page