5 Replies - 303 Views - Last Post: 30 July 2012 - 01:19 PM Rate Topic: -----

#1 aakhtar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 30-July 12

Action Listener issue Null point exception?

Posted 30 July 2012 - 07:35 AM

a Complete noob to java and programming in general I am trying to create a task list manager but at the moment when i try to add the task just so it is displayed back in the displayTask box i get the following

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at ie.wit.taskList.taskListGUI.actionPerformed(taskListGUI.java:151)
at javax.swing.JTextField.fireActionPerformed(Unknown Source)
at javax.swing.JTextField.postActionEvent(Unknown Source)
at javax.swing.JTextField$NotifyAction.actionPerformed(Unknown Source)
at javax.swing.SwingUtilities.notifyAction(Unknown Source)
at javax.swing.JComponent.processKeyBinding(Unknown Source)
at javax.swing.JComponent.processKeyBindings(Unknown Source)
at javax.swing.JComponent.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
I cant seem to be able to figure out what i am doing wrong
any help and suggestion on how to further this program would greatly be appreciated
regards





import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;

import javax.swing.Action;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JComboBox;

public class taskListGUI extends JFrame implements ActionListener {

	private JPanel contentPane;
	private JTextField addTaskTextField;
	private JTextArea tasksDisplay;
	private final static String newline = "\n";

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					taskListGUI frame = new taskListGUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public taskListGUI() {
		setTitle("Task List Manager");
		setBackground(new Color(128, 128, 128));
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 616, 450);

		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		JMenu FileMenu = new JMenu("File");
		menuBar.add(FileMenu);

		JMenuItem mItemOpen = new JMenuItem("Open");
		FileMenu.add(mItemOpen);

		JMenuItem mItemClose = new JMenuItem("Close");
		FileMenu.add(mItemClose);

		JMenu HelpMenu = new JMenu("Help");
		menuBar.add(HelpMenu);

		JMenuItem mItemHelp = new JMenuItem("Click Here For Help");
		HelpMenu.add(mItemHelp);
		contentPane = new JPanel();
		contentPane.setToolTipText("");
		contentPane.setBackground(new Color(128, 128, 128));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

	JTextArea tasksDisplay = new JTextArea();
		tasksDisplay.setEditable(false);
		tasksDisplay.setBounds(124, 41, 447, 259);
		contentPane.add(tasksDisplay);
		
		
/////////////////////////////////Task adding text field with action listener///////////////////////////////////////////////////
		addTaskTextField = new JTextField(250);
		addTaskTextField.setBounds(124, 330, 447, 38);
		contentPane.add(addTaskTextField);
		addTaskTextField.setColumns(10);
		addTaskTextField.addActionListener(this);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////		
		JLabel addTaskLabel = new JLabel("Please Add Tasks Below");
		addTaskLabel.setFont(new Font("Times New Roman", Font.BOLD | Font.ITALIC, 16));
		addTaskLabel.setBounds(144, 300, 171, 27);
		contentPane.add(addTaskLabel);

		JButton btnAddTask = new JButton("Add Task");
		btnAddTask.setBounds(281, 378, 89, 23);
		contentPane.add(btnAddTask);

		JLabel lblTasks = new JLabel("Tasks");
		lblTasks.setFont(new Font("Times New Roman", Font.BOLD | Font.ITALIC, 16));
		lblTasks.setBounds(265, 22, 68, 19);
		contentPane.add(lblTasks);

///////////////////////////////Task List Combo Box Already Populated Manually///////////////////////////////////
		JComboBox taskListComboBox = new JComboBox();
		taskListComboBox.setBounds(10, 44, 104, 18);
		contentPane.add(taskListComboBox);
		String[] taskLists = {"Work", "Personal", "Assignments", "Home"};
		for(int j=0; j<taskLists.length; j++){
			taskListComboBox.addItem(taskLists[j]);
			
			
////////////Priority combo box wiht the required priorties///////////////////////////////////////////////////////////
			JComboBox priorityComboBox = new JComboBox();
			priorityComboBox.setBounds(19, 330, 95, 23);
			contentPane.add(priorityComboBox);
			String[] priority = {"High", "Medium", "Low"};
			for(int i=0; i<priority.length; i++){
				priorityComboBox.addItem(priority[i]);
////////////////////////////////////////////////////////////////////////////////////////////////////


				JLabel lblTaskLists = new JLabel("Task Lists");
				lblTaskLists.setFont(new Font("Times New Roman", Font.BOLD | Font.ITALIC, 16));
				lblTaskLists.setBounds(10, 22, 104, 23);
				contentPane.add(lblTaskLists);

				JLabel lblPriority = new JLabel("Priority");
				lblPriority.setFont(new Font("Times New Roman", Font.BOLD | Font.ITALIC, 16));
				lblPriority.setBounds(25, 304, 89, 19);
				contentPane.add(lblPriority);
			}
		}

	}

	@Override
	public void actionPerformed(ActionEvent evt) {
		// TODO Auto-generated method stub
		String text = addTaskTextField.getText();
		tasksDisplay.append(text + newline);
		addTaskTextField.selectAll();
		
        tasksDisplay.setCaretPosition(tasksDisplay.getDocument().getLength());

	}



}



Is This A Good Question/Topic? 0
  • +

Replies To: Action Listener issue Null point exception?

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Action Listener issue Null point exception?

Posted 30 July 2012 - 07:49 AM

this

JTextArea tasksDisplay = new JTextArea();

tasksDisplay is not the one you reference in your actionPerformed
Remove "JTextArea" in this stement to reference the tasksDisplay of your instance variables
Was This Post Helpful? 1
  • +
  • -

#3 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7950
  • View blog
  • Posts: 13,540
  • Joined: 19-March 11

Re: Action Listener issue Null point exception?

Posted 30 July 2012 - 07:51 AM

The problem is in here - it's hard to localize it further at first glance, but my nose says to look at the result of that getDocument() call first. Does it return an object, or null?
If that's returning an object, then you know that tasksDisplay is not null, so it's likely to be addTaskTextField that's null.


	@Override
	public void actionPerformed(ActionEvent evt) {
		// TODO Auto-generated method stub
		String text = addTaskTextField.getText();
		tasksDisplay.append(text + newline);
		addTaskTextField.selectAll();
		
        tasksDisplay.setCaretPosition(tasksDisplay.getDocument().getLength());

	}


EDIT: pbl's spotted the problem. This is why getDocument() is failing.

This post has been edited by jon.kiparsky: 30 July 2012 - 07:54 AM

Was This Post Helpful? 1
  • +
  • -

#4 aakhtar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 30-July 12

Re: Action Listener issue Null point exception?

Posted 30 July 2012 - 08:30 AM

View Postpbl, on 30 July 2012 - 07:49 AM, said:

this

JTextArea tasksDisplay = new JTextArea();

tasksDisplay is not the one you reference in your actionPerformed
Remove "JTextArea" in this stement to reference the tasksDisplay of your instance variables


thanks a million PBL worked like a charm
another question how do i store these entries from the in an array from where i can add or remove the as required

[quote name='aakhtar' date='30 July 2012 - 08:27 AM' timestamp='1343662022' post='1675257']

View Postpbl, on 30 July 2012 - 07:49 AM, said:

this

JTextArea tasksDisplay = new JTextArea();

tasksDisplay is not the one you reference in your actionPerformed
Remove "JTextArea" in this stement to reference the tasksDisplay of your instance variables

EDIT*
thanks a million PBL worked like a charm
another question how do i store these entries from the addTaskTextField in to an array from where i can add or remove the as required and display them with tasksDisplay at the smae time
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Action Listener issue Null point exception?

Posted 30 July 2012 - 12:43 PM

An ArrayList of String would be more appropriate than an array as it will expand automatically

ArrayList<String> al = new ArrayList<String>();

...

       String text = addTaskTextField.getText();  
       tasksDisplay.append(text + newline);  
       al.add(text);


Was This Post Helpful? 1
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Action Listener issue Null point exception?

Posted 30 July 2012 - 01:19 PM

This was my answer to your 'duplicate' post before it was locked. The context isn't exactly correct, but the sentiments are still applicable:

If possible, provide some direct relationship between your question(s) and the code you post. Say something like, "At lines 110 - 120 I'm trying to copy the contents of the JTextArea (actual name) to the ArrayList (actual name), but I don't know how. Could you tell me how that's done?" If you've made those kind of comments in your code, say so.

As it is, your questions can be generalized, and I'm not going to search 140 lines of code to figure out how to answer your questions with specifics.

Quote

how can input be added to the arrayList from the Jtextfield while at the same time displaying it in JTextArea?

What input?

The contents of a JTextArea can be set using the JTextArea.setText() method (inherited from JTextComponent).

The contents of a JTextArea can be retrieved using JTextArea.getText() method (also inherited from JTextComponent).

After the contents of the JTextArea have been retrieved, the String object can be added to an ArrayList of Strings or Objects using the ArrayList.add() method.

Quote

how can one of the priority be assigned to the input as well as the tasklist

Not sure what you mean, but it's starting to sound like you should have a Task class with characteristics like priority. If not, it sounds like you'll have a number of parallel arrays (or not so parallel) to keep track of all that info, and that's a pain.

Quote

how can i get the add and remove buttons to function as in adding and deleting to an array

by adding actionListeners to the components and programming the corresponding actionPerformed() method(s) to do as you'd like.

These are basic Swing topics for which you'll find excellent tutorials in the Java Swing Tutorials. There are others here on DIC and sprinkled throughout the web.

Good luck!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1