5 Replies - 765 Views - Last Post: 07 December 2012 - 01:51 PM Rate Topic: -----

#1 BigD959919  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 91
  • Joined: 26-March 12

Null Pointer Error when using JFileChooser

Posted 07 December 2012 - 12:30 PM

if (event.getSource() == openDoc) {
			state = fileChooser.showOpenDialog(GUI); //This is line 112
			if(state == JFileChooser.APPROVE_OPTION)
				doc = fileChooser.getSelectedFile();
			try {
				FileReader in = new FileReader(doc);
				BufferedReader out = new BufferedReader(in);
				while ((line = out.readLine()) != null)
					document.append(line + "\n");
					out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}


Error:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at SpellCheckerGUI.actionPerformed(SpellCheckerGUI.java:112)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(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.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(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.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(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)

The goal is to have a gui that has two "open" buttons. One to open a dictionary (list of words) and another to open text document that you make. The program is going to be a low end spell checker. My problem is I dont understand why I am getting a null pointer error. I read the .showOpenDiaglog() oracle page and I believe the JFrame component GUI should work as the parrent component so I am not sure why I am getting this error.

This segment of code it under the action listener method I am implementing in the GUI construction class. If the "Open DOc" button is pressed it will bring up the open dialog and allow the user to select a document. Once selected it is read into the filereader and appeneded to a jtextframe named document so that it can be viewed.

Any help would be much obliged.

This post has been edited by BigD959919: 07 December 2012 - 12:34 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Null Pointer Error when using JFileChooser

#2 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Null Pointer Error when using JFileChooser

Posted 07 December 2012 - 01:26 PM

if (event.getSource() == openDoc) {
	state = fileChooser.showOpenDialog(GUI); //This is line 112
	if(state == JFileChooser.APPROVE_OPTION)
		doc = fileChooser.getSelectedFile();
	try {
		FileReader in = new FileReader(doc);


if state != JFileChooser.APPROVE_OPTION then dfoc is not initialized and probably null when you try to new FileReader(doc)

And are you sure that the fileChooser was correctly initialized
seems that fileChooser is null
Was This Post Helpful? 1
  • +
  • -

#3 BigD959919  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 91
  • Joined: 26-March 12

Re: Null Pointer Error when using JFileChooser

Posted 07 December 2012 - 01:32 PM

But shouldnt the open dialog be appearing before the check to see if is the selected file is run?

Missing parrends...thanks pbl
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Null Pointer Error when using JFileChooser

Posted 07 December 2012 - 01:34 PM

so it means that fileChooser is not initialized, show use the code where it is initialized and the few lines before and after .
Was This Post Helpful? 0
  • +
  • -

#5 BigD959919  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 91
  • Joined: 26-March 12

Re: Null Pointer Error when using JFileChooser

Posted 07 December 2012 - 01:39 PM

your first post solved it...I was missing a set or parenthesis on the initalization and for whatever reason it wasnt caught as an error when I was compiling.
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7875
  • View blog
  • Posts: 13,365
  • Joined: 19-March 11

Re: Null Pointer Error when using JFileChooser

Posted 07 December 2012 - 01:51 PM

Curly braces != parentheses.

This post has been edited by jon.kiparsky: 07 December 2012 - 01:52 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1