Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

843806Aug 2 2007 — edited Aug 7 2007
I searched the forums but did not find the same thing that was happening to me. Normally, this error is easy to figure out why it is happening. My guess is that it is happening because too many threads are being created. It does not seem to affect my GUI though. But, at the same time, I do not want this error being printed out. I wrote generic exception catches into my program but the errors must be happening somewhere else because they are never caught in my program. Also, look at the error output that I am getting. It does not mention any lines in my program. So basically, my question is, is this exception being raised because too many threads are created? How can I prevent or at least catch the error? You should be able to just copy and paste my code and it work. (SHOULD) You can recreate the error by holding down the ENTER key inside the JTextArea. Around line 300 is where the exception is raised. If you need more info, let me know.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
public class cfmCodeCounter1_1 extends JFrame implements Scrollable
{
	private JLabel lblEnter;
	private JTextArea input, output;
	private JList row;
	private JButton parse, clear, exit;
	private JScrollPane scroller, scroller2, scroller3;
	private Container c;
	private Vector <String> rowVec;
	private int maxUnitIncrement = 1;  // Used for scrolling
	private boolean doit = false; // Used for keylistener
	
	private cfmCodeCounter1_1()
	{
		try
		{
			this.setTitle("ColdFusion Parser - NGIT");
			
			// Vector
			rowVec = new Vector<String>();
			rowVec.add("" + 1);
			
			// Display Label
			lblEnter = new JLabel("Enter Code Below: ");
			lblEnter.setVisible(true);
			
			// JTextAreas
			input = new JTextArea(10, 30); // (rows, columns)
			input.setEditable(true);
			scroller = new JScrollPane(input);
			output = new JTextArea(5, 19); // (rows, columns)
			output.setEditable(false);
			scroller2 = new JScrollPane(output);
			
			// Selectable List
			row = new JList(rowVec);
			row.setVisibleRowCount(10);
			row.setFixedCellWidth(50);
			row.setFixedCellHeight((input.getPreferredSize().height/10));
			scroller.setRowHeaderView(row);
			
			// Set up listener for input and row
			input.addCaretListener(new TextHandler());
			input.addKeyListener(new MyKeyListener());
			
			// Button
			parse = new JButton("Parse Code");
			clear = new JButton("Clear");
			exit = new JButton("Exit");
				
			// Set Layout
			c = getContentPane();
			SpringLayout layout = new SpringLayout();
			c.setLayout(layout);
			c.add(lblEnter);
			c.add(scroller);
			c.add(scroller2);
			c.add(parse);
			c.add(clear);
			c.add(exit);
			
			// Place lblEnter
			layout.putConstraint(SpringLayout.WEST, lblEnter,
							 10,
                             SpringLayout.WEST, c);
        	layout.putConstraint(SpringLayout.NORTH, lblEnter,
                             10,
                             SpringLayout.NORTH, c);
			
			// Place scroller aka input
			layout.putConstraint(SpringLayout.WEST, scroller,
                             5,
                             SpringLayout.WEST, c);
        	layout.putConstraint(SpringLayout.NORTH, scroller,
                             5,
                             SpringLayout.SOUTH, lblEnter);
                             
           	// Place scroller2 aka output
			layout.putConstraint(SpringLayout.WEST, scroller2,
                             5,
                             SpringLayout.EAST, scroller);
        	layout.putConstraint(SpringLayout.NORTH, scroller2,
                             0,
                             SpringLayout.NORTH, scroller);
                             
            // Place parse
			layout.putConstraint(SpringLayout.WEST, parse,
                             10,
                             SpringLayout.WEST, c);
        	layout.putConstraint(SpringLayout.NORTH, parse,
                             5,
                             SpringLayout.SOUTH, scroller);
            
            // Place clear
			layout.putConstraint(SpringLayout.WEST, clear,
                             5,
                             SpringLayout.EAST, parse);
        	layout.putConstraint(SpringLayout.NORTH, clear,
                             0,
                             SpringLayout.NORTH, parse);
             
            // Place exit
			layout.putConstraint(SpringLayout.WEST, exit,
                             5,
                             SpringLayout.EAST, clear);
        	layout.putConstraint(SpringLayout.NORTH, exit,
                             0,
                             SpringLayout.NORTH, clear);
			
			// Button Event Handler
			ButtonHandler handler = new ButtonHandler();
			parse.addActionListener(handler);
			clear.addActionListener(handler);
			exit.addActionListener(handler);
			
			this.addWindowListener(new WindowAdapter()
				{
					public void windowClosing(WindowEvent e)
					{
						System.exit(0);
					}			
				}
			);
	
			setSize(650, 300);
			setVisible(true);
			input.requestFocus();
		}
		catch(Exception e)
		{
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "General Error: See Command Line.", "ERROR - NGIT", JOptionPane.ERROR_MESSAGE);
			System.exit(0);
		}
	} 
		
	// Needed for Scrollable implementation
	public Dimension getPreferredSize() 
	{
        return getPreferredSize();
    }

    public Dimension getPreferredScrollableViewportSize()
    {
        return getPreferredSize();
    }

    public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) 
    {
        //Get the current position.
        int currentPosition = 0;
        if (orientation == SwingConstants.HORIZONTAL) 
        {
            currentPosition = visibleRect.x;
        } 
        else 
        {
            currentPosition = visibleRect.y;
        }

        //Return the number of pixels between currentPosition
        //and the nearest tick mark in the indicated direction.
        if (direction < 0) 
        {
            int newPosition = currentPosition - (currentPosition / maxUnitIncrement) * maxUnitIncrement;
            
            return (newPosition == 0) ? maxUnitIncrement : newPosition;
        } 
        else 
        {
            return ((currentPosition / maxUnitIncrement) + 1) * maxUnitIncrement - currentPosition;
        }
    }

    public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction)
    {
        if (orientation == SwingConstants.HORIZONTAL)
        {
            return visibleRect.width - maxUnitIncrement;
        } 
        else
        {
            return visibleRect.height - maxUnitIncrement;
        }
    }

    public boolean getScrollableTracksViewportWidth()
    {
        return false;
    }

    public boolean getScrollableTracksViewportHeight()
    {
        return false;
    }    
    // End Scrollable functions  				
	
	private class MyKeyListener extends KeyAdapter
    {
    	public void keyPressed(KeyEvent e)
    	{
    		if (e.getKeyCode() == KeyEvent.VK_ENTER)
    		{
    			doit = true;
    		}
    		else 
    			if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE)
    		{
    			doit = true;
    		}
    		else if ((e.isControlDown()) && (e.getKeyCode() == KeyEvent.VK_V))
    		{
    			doit = true;
    		}
    		else if ((e.isControlDown()) && (e.getKeyCode() == KeyEvent.VK_X))
    		{
    			doit = true;
    		}
    		else
    		{
    			doit = false;
    		}
    	}
    }
	
	private class TextHandler implements Runnable, CaretListener
	{
		public void caretUpdate(CaretEvent e) 
		{
			try
			{
			if (doit)
			{
				doit = false;
				Thread txtThread = new Thread(this);
				txtThread.start();
			}
			}
			catch(Exception i)
			{
				System.out.println("DONE");
				System.exit(0);
			}
		}
		public synchronized void run()
		{
			try
			{
				rowVec.removeAllElements();
				for (int i = 1; i <= input.getLineCount(); i++)
				{
					rowVec.add("" + i);		
				}
				row.setListData(rowVec);
				row.updateUI();
			}
			catch (Exception e)
			{
				e.printStackTrace();
				System.exit(0);
			}
		}
	}
	
	private class ButtonHandler implements Runnable, ActionListener
	{
		char butt;
		public void actionPerformed(ActionEvent e)
		{
			if (e.getSource() == parse)
			{
				butt = 'p';
				parse.setEnabled(false);
				Thread parseThread = new Thread(this);
				parseThread.start();
			}
			else if (e.getSource() == clear)
			{
				butt = 'c';
				clear.setEnabled(false);
				Thread parseThread = new Thread(this);
				parseThread.start();
			}
			else if (e.getSource() == exit)
			{
				butt = 'e';
				exit.setEnabled(false);
				Thread parseThread = new Thread(this);
				parseThread.start();
			}
		}
		
		public void run()
		{
			if (butt == 'p')
			{				
				// Set Enabled
				parse.setEnabled(true);
			}
			else if (butt == 'c')
			{
				input.setText("");
				output.setText("");
				rowVec.removeAllElements();
				row.setListData(rowVec);
				input.requestFocus();
				
				// Set Enabled
				clear.setEnabled(true);
			}
			else if (butt == 'e')
			{
				// Confirm that the user wants to exit
				int selection = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit?", "Exit - NGIT", JOptionPane.YES_NO_OPTION);
				if (selection == JOptionPane.YES_OPTION) 
				{
					System.exit(0);
				}
				
				// Set Enabled
				exit.setEnabled(true);
			}
		}
	
	}

	public static void main(String[] args)
	{
		 cfmCodeCounter1_1 runprog = new cfmCodeCounter1_1();
	}
}
[error]Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.plaf.basic.BasicListUI.updateLayoutState(BasicListUI.java:1307)
at javax.swing.plaf.basic.BasicListUI.maybeUpdateLayoutState(BasicListUI.java:1287)
at javax.swing.plaf.basic.BasicListUI.getPreferredSize(BasicListUI.java:559)
at javax.swing.JComponent.getPreferredSize(JComponent.java:1627)
at javax.swing.ViewportLayout.layoutContainer(ViewportLayout.java:123)
at java.awt.Container.layout(Container.java:1432)
at java.awt.Container.doLayout(Container.java:1421)
at java.awt.Container.validateTree(Container.java:1519)
at java.awt.Container.validateTree(Container.java:1526)
at java.awt.Container.validate(Container.java:1491)
at javax.swing.RepaintManager.validateInvalidComponents(RepaintManager.java:635)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:127)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)[error]
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 4 2007
Added on Aug 2 2007
22 comments
2,902 views