Results 1 to 4 of 4
  1. #1
    k2k
    k2k is offline Member
    Join Date
    Feb 2009
    Posts
    6
    Rep Power
    0

    Default awt TextField nullPointerException error

    hi, it is my first post here.

    I am new learning how to make form and stuffs with awt. basically i did a form which save input from user to the text and i am now working on the reader to retrieve the info from the text back to the form for display.

    i was successfully done getting the info out from the text, and if i just System.out.println(variable) they would come out no problem. however, if i used the variables and set them to the TextField it pops the following errors
    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at Reader.readRecord(Reader.java:123)
    	at Reader.actionPerformed(Reader.java:65)
    	at java.awt.Button.processActionEvent(Unknown Source)
    	at java.awt.Button.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(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)
    :mad:


    my read method is as follow:
    Java Code:
    public void readRecord()
    	{
    		int recNum;
    		String name, address;
    		double hours, rate, grossPay, fedTax, stateTax;
    		char sex;
    		int age;
    		
    		try
    		{
    			recNum = scanner.readInt();
    			name= scanner.readUTF();
    			address = scanner.readUTF();
    			hours= scanner.readDouble();
    			rate= scanner.readDouble();
    			sex= scanner.readChar();
    			age= scanner.readInt();
    			
    			
    			
    			//Emprec employee = new Emprec(name, address, hours, rate, sex, age);
    			//grossPay= employee.calc_gross_pay();
    			//fedTax= employee.calc_fed_tax(employee.getHours(), employee.getRate());
    			//stateTax= employee.calc_state_tax(employee.getHours(), employee.getRate());
    			//grossPay= employee.calc_gross_pay();
    			
    			
    			System.out.println(recNum);
    			System.out.println(name);
    			System.out.println(address);
    			System.out.println(hours);
    			System.out.println(rate);
    			System.out.println(sex);
    			System.out.println(age);
    			/*
    			System.out.println(grossPay);
    			System.out.println(fedTax);
    			System.out.println(stateTax);
    			*/
    			
    			recordNumF.setText(String.valueOf(recNum));
    			nameF.setText(name);
    			addressF.setText(address);
    			hoursF.setText(String.valueOf(hours));
    			rateF.setText(String.valueOf(rate));
    			sexF.setText(String.valueOf(sex));
    			ageF.setText(String.valueOf(age));
    			//fedF.setText(String.valueOf(fedTax));
    			//stateF.setText(String.valueOf(stateTax));
    			//grossF.setText(String.valueOf(grossPay));		
    		} // end try
    		
    		catch(EOFException eof)
    		{
    			closeFile();
    			eof.toString();
    		}
    		
    		catch(IOException io)
    		{
    			System.err.println("Error during read from file \n" + io.toString());
    			System.exit(1);
    		}
    		
    		
    	}
    if anyone may point out what might be wrong it would be great. thanks very much.

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

    Default

    A NullPointerException tells you that you are trying to use an object that was never properly initialized, that is null. What you must do is look at the line that is throwing the exception (Reader.java:123 -- you can find this line; we can't), find out which object is null, and then look in your code and see why it is not initialized. Best of luck.

    A guess: has the Scanner object been constructed properly?

  3. #3
    k2k
    k2k is offline Member
    Join Date
    Feb 2009
    Posts
    6
    Rep Power
    0

    Default still can't figure out what was null

    maybe i should have posted the whole thing.. i spent hours couldn't figure it out wut was null... it is always saying line 123 is null.. i have commented where line 123 is... it really shouldn't be null there.. :(

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import javax.swing.JOptionPane;
    
    public class Reader extends Frame implements ActionListener
    {
    	private Emprec employee; 
    	private TextField recordNumF, nameF, addressF, hoursF, rateF, sexF, ageF,
    	fedF, stateF, grossF;
    	private Button next, done;
    	private DataInputStream scanner;
    	
    	
    	// constructor
    	public Reader()
    	{
    		super("Employee Information");
    		
    		try
    		{
    			scanner = new DataInputStream(new FileInputStream("emp.txt"));
    			//JOptionPane.showMessageDialog(null, "File Open Successfully");
    		}
    		
    		catch(IOException e)
    		{
    			JOptionPane.showMessageDialog(null, "File not opened properly"+"\n"+
    										  e.toString());
    			System.exit(1);
    		}
    		
    		setSize(500, 350);
    		setLayout(new GridLayout(10,2));
    		
    		/////////////// add components ///////////////
    		addField("Employee Record #", recordNumF);
    		addField("Address", addressF);
    		addField("Hours", hoursF);
    		addField("Rate", rateF);
    		addField("Sex", sexF);
    		addField("Age", ageF);
    		//addField("Gross Pay", grossF);
    		//addField("Federal Tax", fedF);
    		//addField("State Tax", stateF);
    		
    		next = new Button("Next Record");
    		next.addActionListener(this);
    		add(next);
    		
    		done = new Button("Done");
    		done.addActionListener(this);
    		add(done);
    		
    		
    		setVisible(true);
    		
    	}// end reader constructor
    	
    	
    	public void actionPerformed(ActionEvent e)
    	{
    		if(e.getSource()== next)
    		{
    			readRecord();        // line 65
    		}
    		
    		if(e.getSource()== done)
    		{ 
    			try{scanner.close();}
    			
    			catch(IOException io)
    			{
    				System.out.println("File not closed properly");
    				System.exit(1);
    			}
    			System.exit(0);  
    		}
    		
    	}
    	
    	
    	public void readRecord()
    	{
    		int recNum;
    		String name, address;
    		double hours, rate, grossPay, fedTax, stateTax;
    		char sex;
    		int age;
    		
    		try
    		{
    			recNum = scanner.readInt();
    			name= scanner.readUTF();
    			address = scanner.readUTF();
    			hours= scanner.readDouble();
    			rate= scanner.readDouble();
    			sex= scanner.readChar();
    			age= scanner.readInt();
    			
    			
    			
    			//Emprec employee = new Emprec(name, address, hours, rate, sex, age);
    			//grossPay= employee.calc_gross_pay();
    			//fedTax= employee.calc_fed_tax(employee.getHours(), employee.getRate());
    			//stateTax= employee.calc_state_tax(employee.getHours(), employee.getRate());
    			//grossPay= employee.calc_gross_pay();
    			
    			
    			System.out.println(recNum);
    			System.out.println(name);
    			System.out.println(address);
    			System.out.println(hours);
    			System.out.println(rate);
    			System.out.println(sex);
    			System.out.println(age);
    			/*
    			System.out.println(grossPay);
    			System.out.println(fedTax);
    			System.out.println(stateTax);
    			*/
    			
    			recordNumF.setText(String.valueOf(recNum));  // line 123
    			nameF.setText(name);
    			addressF.setText(address);
    			hoursF.setText(String.valueOf(hours));
    			rateF.setText(String.valueOf(rate));
    			sexF.setText(String.valueOf(sex));
    			ageF.setText(String.valueOf(age));
    			//fedF.setText(String.valueOf(fedTax));
    			//stateF.setText(String.valueOf(stateTax));
    			//grossF.setText(String.valueOf(grossPay));		
    		} // end try
    		
    		catch(EOFException eof)
    		{
    			closeFile();
    			eof.toString();
    		}
    		
    		catch(IOException io)
    		{
    			System.err.println("Error during read from file \n" + io.toString());
    			System.exit(1);
    		}
    		
    		
    	}
    	
    	public void closeFile()
    	{
    		try
    		{
    			scanner.close();
    		}
    		
    		catch(EOFException eof)
    		{
    			closeFile();
    		}
    		
    		catch(IOException e)
    		{
    			e.toString();
    		}
    	}
    	
    	
    	
    	
    	// private help method for adding field 
    	private void addField(String label, TextField field)
    	{
    		add(new Label(label));
    		field = new TextField();
    		field.setEditable(false);
    		add(field);
    	}
    	
    	/*
    	public static void main(String args[])
    	{
    		new Reader();
    		
    	}
    	*/
    
    } // end class Reader

  4. #4
    k2k
    k2k is offline Member
    Join Date
    Feb 2009
    Posts
    6
    Rep Power
    0

    Default

    uh -oh,, i think i got it. it was the stupid method addField() messed it up... thanks for all the help anyway.

Similar Threads

  1. java.lang.NullPointerException Error
    By Manfizy in forum NetBeans
    Replies: 3
    Last Post: 02-24-2011, 07:27 AM
  2. Replies: 4
    Last Post: 10-09-2008, 07:08 PM
  3. ERROR: nullPointerException in applet
    By barney in forum Java Applets
    Replies: 1
    Last Post: 08-07-2007, 08:11 AM
  4. ERROR: nullPointerException
    By mathias in forum New To Java
    Replies: 1
    Last Post: 08-05-2007, 07:54 AM
  5. Replies: 2
    Last Post: 07-02-2007, 03:29 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
  •