Results 1 to 12 of 12
Thread: catch error statement
- 03-15-2012, 03:07 AM #1
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
catch error statement
I have the following method, and it is returning this:
Java Code:Error: null
why doesn't it just print out the lines from the .txt file?Java Code:public void listTask() { gui setStatus = new gui(); try { BufferedReader in = new BufferedReader(new FileReader("tasks.txt")); Scanner scanner = new Scanner(new File("tasks.txt")); Pattern pattern = Pattern.compile("(.+)Ø(.+)Ø(.+)§"); while (scanner.hasNextLine()) { Matcher m = pattern.matcher(scanner.nextLine()); if (m.find()) { setStatus.writeToTextArea('n', "\n" + "\nTask Title: " + m.group(1) + " \nTask Content: " + m.group(2) + " \nTask Priority: " + m.group(3) + "\n\n"); } } in.close(); } catch (Exception e) { System.err.println("\nError: " + e.getMessage()); } }Last edited by droidus; 03-15-2012 at 03:16 AM.
- 03-15-2012, 03:33 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: catch error statement
It ouputs "Error: null" if you hit an exception with a null message. For details:
Java Code:} catch(Exception e) { //System.err.println("Error: " + e.getMessage()); e.printStackTrace(); }
- 03-15-2012, 03:33 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: catch error statement
It ouputs "Error: null" if you hit an exception with a null message. For details:
Java Code:} catch(Exception e) { //System.err.println("Error: " + e.getMessage()); e.printStackTrace(); }
- 03-15-2012, 02:16 PM #4
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
Re: catch error statement
alright. doesn't help me much because all i get is this...
I'm thinking that it doesn't like my listTask() method. before I had it do a "throws IOException". I changed it to try catch to see if I could get it working this way...Java Code:java.lang.NullPointerException at gui.writeToTextArea(gui.java:192) at taskBckg.listTask(taskBckg.java:221) at taskBckg.processRequest(taskBckg.java:109) at gui.actionPerformed(gui.java:177) 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$000(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.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$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)
here is my method that calls on list task:Java Code:public void listTask() { gui setStatus = new gui(); try { BufferedReader in = new BufferedReader(new FileReader("tasks.txt")); Scanner scanner = new Scanner(new File("tasks.txt")); Pattern pattern = Pattern.compile("(.+)Ø(.+)Ø(.+)§"); while (scanner.hasNextLine()) { Matcher m = pattern.matcher(scanner.nextLine()); if (m.find()) { setStatus.writeToTextArea('n', "\n " + ": \nTask Title: " + m.group(1) + " \nTask Content: " + m.group(2) + " \nTask Priority: " + m.group(3) + "\n\n"); } } in.close(); } catch (Exception e) { e.printStackTrace(); } }
Java Code:public void processRequest(String command) { command = command.toUpperCase(); // gui3.writeToTextArea('n', task); switch (command) { case "L": gui updateInput = new gui(); listTask(); System.out.print("Now Listing tasks..."); break; } }
- 03-15-2012, 02:29 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: catch error statement
That helps a lot.
You now know what the error is (a null pointer), and where it is occurring (line 192 of gui).
So you now know something on line 192 is null and you are attempting to call a method or access an attribute of it.Please do not ask for code as refusal often offends.
- 03-15-2012, 02:36 PM #6
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
Re: catch error statement
hm... well here is line 192...
not sure how it helps without any context. let me know if you would like to see other code. but not sure why that wouldn't be working. it seems like there are errors in other places too?Java Code:textArea.append(userInputText.toUpperCase());
also, do you know how to show the line numbers in eclipse?
- 03-15-2012, 02:45 PM #7
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
Re: catch error statement
i also get this error in gui:
on line:Java Code:The type gui must implement the inherited abstract method WindowListener.windowOpened(WindowEvent)
Java Code:public class gui implements WindowListener,ActionListener {
- 03-15-2012, 03:06 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: catch error statement
Either textArea or userInputText is null when this line is executed.
Window->Preferences->Editor->somewhere under here.
That says your code doesn't compile, which begs the question of how you've managed to run it.
Anyway, gui is implementing WindowListener, so must implement all the methods of that, including windowOpened.Please do not ask for code as refusal often offends.
- 03-15-2012, 05:15 PM #9
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
Re: catch error statement
in my gui class, i have the following:
won't that initialize the text area?Java Code:textArea = new JTextArea(5, 20);
and i had a method called before that modified the text area:
Java Code:gui.writeToTextArea('s', "What would you like to do? [L]ist tasks or [C]reate a new one or [E]dit a task or [D]elete a task or [H]elp?");
- 03-15-2012, 05:26 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: catch error statement
Then, if textArea is definitely not null (and until you stick a System.out.println to show that you are probably guessing) it must be userInputText.
I'd suggest sticking some debugging Sysouts in there to see what's going on.
Put simply, the JVM doesn't lie. Something on the line that throws a NullPointer is null.Please do not ask for code as refusal often offends.
- 03-15-2012, 09:36 PM #11
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
Re: catch error statement
user input text seems to work. when i print out textArea, it says null. then when i use .getText() on it, it returns "java.lang.NullPointerException". it should be textArea.getText(), not textArea, right?
- 03-16-2012, 09:49 AM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
SQL Statement Error
By Dcalladi in forum New To JavaReplies: 3Last Post: 01-30-2012, 04:38 PM -
try/catch and if statement conundrum
By thatguywhoprograms in forum New To JavaReplies: 4Last Post: 12-19-2011, 10:20 PM -
try, catch, exception error
By Asvin in forum New To JavaReplies: 5Last Post: 04-11-2011, 10:12 PM -
error in if statement
By carman12 in forum New To JavaReplies: 11Last Post: 12-25-2010, 01:13 AM -
Error with my Try Catch
By kewlgeye in forum New To JavaReplies: 9Last Post: 05-03-2008, 03:38 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks