Results 1 to 4 of 4
- 02-23-2009, 10:09 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 6
- Rep Power
- 0
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
:mad: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)
my read method is as follow:
if anyone may point out what might be wrong it would be great. thanks very much.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); } }
-
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?
- 02-24-2009, 04:05 AM #3
Member
- Join Date
- Feb 2009
- Posts
- 6
- Rep Power
- 0
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
- 02-24-2009, 04:24 AM #4
Member
- Join Date
- Feb 2009
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
java.lang.NullPointerException Error
By Manfizy in forum NetBeansReplies: 3Last Post: 02-24-2011, 06:27 AM -
Definitely new to Java "Main" Java.lang NullPointerException Error
By lilyumestar in forum New To JavaReplies: 4Last Post: 10-09-2008, 06:08 PM -
ERROR: nullPointerException in applet
By barney in forum Java AppletsReplies: 1Last Post: 08-07-2007, 07:11 AM -
ERROR: nullPointerException
By mathias in forum New To JavaReplies: 1Last Post: 08-05-2007, 06:54 AM -
Error Java.lang.NullPointerException in JBuilder2006
By Jack in forum Other IDEsReplies: 2Last Post: 07-02-2007, 02:29 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks