Results 1 to 4 of 4
  1. #1
    Titus_85 is offline Member
    Join Date
    Apr 2016
    Posts
    11
    Rep Power
    0

    Default How to update the content of a txt file after the aplication has started

    Hello! My application writes content to a text file and then reads it.The problem is that it displays the content at the time the application started.I need it to read the content real time.I tried to call the method "filereader" via an actionListener but it throws an error. Please help!

    Java Code:
    public static void main(String[] args) throws IOException {
    		Test7 obj_1 = new Test7();
    
    		FileOutputStream fop = null;
    		File file;
    
    		Scanner textfile = new Scanner(new File("/home/reborn/workspace/Salvate/zz7.txt"));
    		filereader(textfile)
    		
    		
    		
    	}
    
    	static void filereader(Scanner scanner_2) {
    
    		while(scanner_2.hasNextInt()){
    			sum += scanner_2.nextInt();
    		}
    		System.out.println(sum);
    	}
    Last edited by Titus_85; 05-12-2016 at 03:05 AM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: How to update the content of a txt file after the aplication has started

    What error are you getting?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Titus_85 is offline Member
    Join Date
    Apr 2016
    Posts
    11
    Rep Power
    0

    Default Re: How to update the content of a txt file after the aplication has started

    This are the errors.I get this when I click the JButton of the actionListener

    Java Code:
    public void actionPerformed(ActionEvent evt) {
    		Object src = evt.getSource();
    
    		if (src == add_distance) {
    			filereader(textfile);
    			
    			try {
    				
    				
    				label_3.setText("Total distance is " + String.valueOf(sum));													
                    
    				String f = textF_1.getText() + "\n";
    
    				label_1.setText(f);
    ***
    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at Test7.filereader(Test7.java:66)
    	at Test7.actionPerformed(Test7.java:134)
    	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    	at java.awt.Component.processMouseEvent(Component.java:6516)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3312)
    	at java.awt.Component.processEvent(Component.java:6281)
    	at java.awt.Container.processEvent(Container.java:2229)
    	at java.awt.Component.dispatchEventImpl(Component.java:4872)
    	at java.awt.Container.dispatchEventImpl(Container.java:2287)
    	at java.awt.Component.dispatchEvent(Component.java:4698)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    	at java.awt.Container.dispatchEventImpl(Container.java:2273)
    	at java.awt.Window.dispatchEventImpl(Window.java:2719)
    	at java.awt.Component.dispatchEvent(Component.java:4698)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:747)
    	at java.awt.EventQueue.access$300(EventQueue.java:103)
    	at java.awt.EventQueue$3.run(EventQueue.java:706)
    	at java.awt.EventQueue$3.run(EventQueue.java:704)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:77)
    	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87)
    	at java.awt.EventQueue$4.run(EventQueue.java:720)
    	at java.awt.EventQueue$4.run(EventQueue.java:718)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:77)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:717)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: How to update the content of a txt file after the aplication has started

    Something on line 66 of Test7 is null.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 1
    Last Post: 09-29-2014, 07:31 PM
  2. Replies: 1
    Last Post: 03-17-2014, 02:03 AM
  3. Update content on a JTextArea.
    By Masochist in forum New To Java
    Replies: 7
    Last Post: 05-13-2010, 10:18 PM
  4. Update the JFrame after change the Content Pane
    By alisonchan30 in forum AWT / Swing
    Replies: 1
    Last Post: 04-26-2010, 06:22 AM
  5. Replies: 2
    Last Post: 05-11-2009, 10:07 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
  •