hi, i am trying to read the file and parse the info to the text field of the awt gui form thing. i got the info out successfully by testing with the "system.out.pritnln(variable )" .. however, using the variable values wouldn't successfully set to the TextFeild for output.

//the first 7 lines are the exact info i need from the text. however i couldn't get them to appear in the TextField

1                        
bob
23 pond street
40.0
32.3
f
23
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)

below is my method to read the file

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 + e.toString()");
			System.exit(1);
		}
		
		
	}

i basically followed my professor's example doing this one, i have never seem those errors before. if anyone can tell what might be wrong that would be great. Thanks.

The textfields themselves are probably null.

The TestFields are null until they are assigned the values got from the text. it doesn't look like the problem though.

below is how i assigned the TextField.

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

masijade means that the TextField variables themselves might be null, as in you have not initialized them with "new TextField()". He wasn't referring to them being empty.

The code you posted above doesn't really answer that question, because you didn't post the addField() method code.

masijade means that the TextField variables themselves might be null, as in you have not initialized them with "new TextField()". He wasn't referring to them being empty.

The code you posted above doesn't really answer that question, because you didn't post the addField() method code.

i spent hours couldn't figure it out what was null, sorry maybe i should have posted the whole thing. it isn't that long anyway

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

opps,, finally, it is the addField() messed it up.. i got it thanks very much

Yep, this

private void addField(String label, TextField field)
	{
		...
		field = new TextField();
		...
	}

will only create a TextField within the local method, and as soon as the method is done, the TextField is unreachable and will be GCed. And, the textfield, is never associated with the instance field that it should have been associated with. Put your textfields in a hashmap and use a String equivalent to the current field name as the key, if you wish to be able to deal with them in this manner.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.