Results 1 to 7 of 7
  1. #1
    blkshrk81 is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default Increment Counter for ID

    I am trying to add a counter to use as an ID in my text file. My application allows users to add records to a text file and I need to have an ID for each new record. I want it to display as:

    1, Hangover, Robert, 90 min, Comedy
    2, ...,...,...,...
    3, ...and so on

    I keep getting the following error:

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Video$AcceptButtonHandler.actionPerformed(Video.ja va:119)
    at javax.swing.AbstractButton.fireActionPerformed(Unk nown 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.mouseRe leased(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(U nknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn 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.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)


    Any help you could provide would be greatly appreciated. Thanks

    Java Code:
    try
    			{
    				File inputFile = new File ("video.txt"); //setup input file for reading
    				 FileReader myFileReader = new FileReader(inputFile);
    				 BufferedReader myBufferedReader = new BufferedReader(myFileReader);
    				 String inputBuffer = "";
    				 int count = 0;
    					
    						while( (inputBuffer = myBufferedReader.readLine()) != null) //read lines from the file
    					{
    						StringTokenizer myStringTokenizer = new StringTokenizer(inputBuffer, ",");
    						System.out.println(inputBuffer);
    						count = Integer.parseInt((myStringTokenizer.nextToken()));
    					}
    					myBufferedReader.close();	
    					count ++;
    				 
    				outFile = new File("video.txt"); //create File Object
    				aFileWriter = new FileWriter(outFile, true);
    				aPrintWriter = new PrintWriter(aFileWriter);
    				outputStr = Integer.toString(count) + ", " +outputStr;
    				aPrintWriter.println(outputStr); //write string to output file
    			}
    			catch(IOException error)
    			{
    				System.out.println("I/O Error");
    			}//end catch
    			finally
    			{
    				aPrintWriter.close();
    			}
    		}//end method

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    blkshrk81 is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    count = Integer.parseInt((myStringTokenizer.nextToken()));

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  5. #5
    blkshrk81 is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    System.out.println(inputBuffer);

    For instance, this is what is added to my text file right now when I add 2 records:

    1, Hangover, Robert, 90 min, Comedy
    1, Billy Madison, Lucas, 120, Comedy

  6. #6
    blkshrk81 is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    This is the full code:

    Java Code:
    import java.awt.Container;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.*;
    import java.util.*;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    
    public class Video extends JFrame //inherits from the JFrame class
    {
    	//static final variables to hold frame dimensions (in pixels)
    	private static final int WIDTH = 500;
    	private static final int HEIGHT = 175;
    	
    	private JLabel titleLabel, directorLabel, runningTimeLabel, genreLabel;
    	private JTextField titleTextField, directorTextField, runningTimeTextField, genreTextField;
    	private JButton acceptB, clearB;
    	
    	//declare event handlers
    	private AcceptButtonHandler abHandler;
    	private ClearButtonHandler cbHandler;
    	
    	public Video() //constructor defines frame
    	{
    		setTitle ("Add Video"); //set the title of the frame
    		setSize (WIDTH, HEIGHT); //set the frame size
    		
    		//prepare the container
    		Container pane = getContentPane(); //get content pane
    		GridLayout aGrid = new GridLayout(5,2); //create a 5-row, 2-column layout
    		pane.setLayout(aGrid); //set the layout for the pane
    		
    		//instantiate JLabel objects
    		titleLabel 			= new JLabel("Enter Title:");
    		directorLabel 		= new JLabel("Enter Director:");
    		runningTimeLabel 	= new JLabel("Enter Running Time:");
    		genreLabel 			= new JLabel("Enter Genre:");
    		
    		//instantiate JTextField objects
    		titleTextField			= new JTextField(30);
    		directorTextField		= new JTextField(50);
    		runningTimeTextField	= new JTextField(50);
    		genreTextField			= new JTextField(20);
    		
    		//instantiate JTextField objects
    		acceptB = new JButton("Accept");
    		clearB 	= new JButton("Clear");
    		
    		//instantiate button and handler, register handler
    		abHandler = new AcceptButtonHandler(); //create button handler
    		acceptB.addActionListener(abHandler); //register handler with button
    		cbHandler = new ClearButtonHandler(); //create button handler
    		clearB.addActionListener(cbHandler); //register handler with button
    		
    		//add components to the pane
    		pane.add(titleLabel);
    		pane.add(titleTextField);
    		pane.add(directorLabel);
    		pane.add(directorTextField);
    		pane.add(runningTimeLabel);
    		pane.add(runningTimeTextField);
    		pane.add(genreLabel);
    		pane.add(genreTextField);
    		pane.add(acceptB);
    		pane.add(clearB);
    	}//end constructor
    	
    	private class AcceptButtonHandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			//declare I/O objects
    			File outFile;
    			FileWriter aFileWriter;
    			PrintWriter aPrintWriter = null;
    			
    			//get data from text fields
    			String aTitle = titleTextField.getText();
    			String aDirector = directorTextField.getText();
    			String aRunningTime = runningTimeTextField.getText();
    			String aGenre = genreTextField.getText();
    			
    			//place data in string
    			String outputStr = aTitle + ", " + aDirector + ", "
    			+ aRunningTime + ", " + aGenre + "\r\n";
    			
    			try
    			{
    				File inputFile = new File ("video.txt"); //setup input file for reading
    				 FileReader myFileReader = new FileReader(inputFile);
    				 BufferedReader myBufferedReader = new BufferedReader(myFileReader);
    				 String inputBuffer = "";
    				 int count = 0;
    					
    						while( (inputBuffer = myBufferedReader.readLine()) != null) //read lines from the file
    					{
    						StringTokenizer myStringTokenizer = new StringTokenizer(inputBuffer, ",");
    						System.out.println(inputBuffer);
    						count = Integer.parseInt((myStringTokenizer.nextToken()));
    					}
    					myBufferedReader.close();	
    					count ++;
    				 
    				outFile = new File("video.txt"); //create File Object
    				aFileWriter = new FileWriter(outFile, true);
    				aPrintWriter = new PrintWriter(aFileWriter);
    				outputStr = Integer.toString(count) + ", " +outputStr;
    				aPrintWriter.println(outputStr); //write string to output file
    			}
    			catch(IOException error)
    			{
    				System.out.println("I/O Error");
    			}//end catch
    			finally
    			{
    				aPrintWriter.close();
    			}
    		}//end method
    	}//end class
    	
    	//inner class for button handler
    	private class ClearButtonHandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			//set text fields to empty strings
    			titleTextField.setText("");
    			directorTextField.setText("");
    			runningTimeTextField.setText("");
    			genreTextField.setText("");
    		}
    	}//end class
    	
    	public static void main(String[] args)//declare main method
    	{
    		JFrame aVideo = new Video(); //create the frame object
    		aVideo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		aVideo.setVisible(true); //display the frame
    	}//end main
    }//end class

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    and the data file?

    Also, please change your error handling to this:
    Java Code:
             } catch (IOException error) {
                //!!System.out.println("I/O Error");
                error.printStackTrace();
             }// end catch
             finally {
                aPrintWriter.close();
             }
    And reprint your error message. Also, please double check that you're NPE is occurring on the line indicated above.

    edit:
    and the finally block may be better written like so:
    Java Code:
             finally {
                if (aPrintWriter != null) {
                   aPrintWriter.close();
                }
             }

Similar Threads

  1. Increment Operator Example
    By abimaran in forum New To Java
    Replies: 10
    Last Post: 11-03-2009, 05:45 PM
  2. Counter
    By ks1615 in forum New To Java
    Replies: 6
    Last Post: 02-20-2009, 04:02 AM
  3. Increment a Variable
    By rhm54 in forum New To Java
    Replies: 2
    Last Post: 06-14-2008, 03:57 AM
  4. How to create auto-increment
    By Albert in forum JDBC
    Replies: 2
    Last Post: 07-04-2007, 06:23 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
  •