Results 1 to 12 of 12
  1. #1
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default catch error statement

    I have the following method, and it is returning this:
    Java Code:
    Error: null
    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());
    		}
    }
    why doesn't it just print out the lines from the .txt file?
    Last edited by droidus; 03-15-2012 at 04:16 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default 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();
    }

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default 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();
    }

  4. #4
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: catch error statement

    alright. doesn't help me much because all i get is this...

    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)
    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:
    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();
    		}
    		
    
    	}
    here is my method that calls on list task:

    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;
    		}
    	}

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default 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.

    ** This space for rent **

  6. #6
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: catch error statement

    hm... well here is line 192...
    Java Code:
    textArea.append(userInputText.toUpperCase());
    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?

    also, do you know how to show the line numbers in eclipse?

  7. #7
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: catch error statement

    i also get this error in gui:
    Java Code:
    The type gui must implement the inherited abstract method WindowListener.windowOpened(WindowEvent)
    on line:
    Java Code:
    public class gui implements WindowListener,ActionListener {

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default Re: catch error statement

    Quote Originally Posted by droidus View Post
    hm... well here is line 192...
    Java Code:
    textArea.append(userInputText.toUpperCase());
    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?
    Either textArea or userInputText is null when this line is executed.

    Quote Originally Posted by droidus View Post
    also, do you know how to show the line numbers in eclipse?
    Window->Preferences->Editor->somewhere under here.

    Quote Originally Posted by droidus View Post
    i also get this error in gui:
    Java Code:
    The type gui must implement the inherited abstract method WindowListener.windowOpened(WindowEvent)
    on line:
    Java Code:
    public class gui implements WindowListener,ActionListener {
    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.

    ** This space for rent **

  9. #9
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: catch error statement

    in my gui class, i have the following:
    Java Code:
    textArea = new JTextArea(5, 20);
    won't that initialize the text area?

    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?");

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default 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.

    ** This space for rent **

  11. #11
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default 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?

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default Re: catch error statement

    If textArea is null then you need to figure out why it isn't being set.
    You cannot call a method on a null reference.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. SQL Statement Error
    By Dcalladi in forum New To Java
    Replies: 3
    Last Post: 01-30-2012, 05:38 PM
  2. try/catch and if statement conundrum
    By thatguywhoprograms in forum New To Java
    Replies: 4
    Last Post: 12-19-2011, 11:20 PM
  3. try, catch, exception error
    By Asvin in forum New To Java
    Replies: 5
    Last Post: 04-11-2011, 11:12 PM
  4. error in if statement
    By carman12 in forum New To Java
    Replies: 11
    Last Post: 12-25-2010, 02:13 AM
  5. Error with my Try Catch
    By kewlgeye in forum New To Java
    Replies: 9
    Last Post: 05-03-2008, 04:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •