Hello, this summer has become boring, so I decided to learn programming. I've had no problems until now. I'm learning the java.io class, and decided to use it in a gui. In the program I'm trying to test a number to see if it's prime then save it to a file if it is the largest prime tested.
I get a null pointer exception. Any help would be appreciated.
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:989)
at java.lang.Double.parseDouble(Double.java:510)
at primeGui.actionPerformed(primeGui.java:65)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.jav a:6288)
at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
at java.awt.Component.processEvent(Component.java:605 3)
at java.awt.Container.processEvent(Container.java:204 1)
at java.awt.Component.dispatchEventImpl(Component.jav a:4651)
at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
at java.awt.Component.dispatchEvent(Component.java:44 81)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4168)
at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478 )
at java.awt.Component.dispatchEvent(Component.java:44 81)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:643)
at java.awt.EventQueue.access$000(EventQueue.java:84)
at java.awt.EventQueue$1.run(EventQueue.java:602)
at java.awt.EventQueue$1.run(EventQueue.java:600)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:616)
at java.awt.EventQueue$2.run(EventQueue.java:614)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 613)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
import java.awt.event.*; import javax.swing.*; import java.awt.*; import java.io.*; public class primeGui implements ActionListener { JFrame frame; JPanel panel; JButton button; JLabel label, answer, highPrime; JTextField enterInfo; public primeGui() { frame = new JFrame("Prime Number Check"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); panel = new JPanel(); panel.setLayout(new GridLayout(0,2,10,5)); panel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); label = new JLabel("Enter an integer: "); panel.add(label); enterInfo = new JTextField(10); panel.add(enterInfo); answer = new JLabel(" "); panel.add(answer); button = new JButton("check"); button.setActionCommand("check"); button.addActionListener(this); panel.add(button); highPrime = new JLabel(" "); panel.add(highPrime); frame.setContentPane(panel); frame.pack(); frame.setVisible(true); } public void actionPerformed(ActionEvent event) { String eventName = event.getActionCommand(); int counter=2; double number = 0; String prime = enterInfo.getText(); String numInFile; if (eventName.equals("check")) { number = Double.parseDouble(enterInfo.getText()); do { counter++; } while (number%counter!= 0); if (counter == Double.parseDouble(prime)) { answer.setText("The number IS prime."); }else{ answer.setText("The number is NOT prime."); } } File prime1 = new File("prime.txt"); if(!prime1.exists()) { try { prime1.createNewFile(); }catch (IOException e) { highPrime.setText("Problem creating file."); } } try { FileReader in = new FileReader(prime1); BufferedReader readFile = new BufferedReader(in); FileWriter out = new FileWriter(prime1); BufferedWriter writeFile = new BufferedWriter(out); if ((numInFile = readFile.readLine()) == null) { writeFile.write(String.valueOf(number)); }else{ numInFile = readFile.readLine(); if (number > Double.parseDouble(numInFile)) { writeFile.write(String.valueOf(number)); } } highPrime.setText("The highest prime ever tested is " + Double.parseDouble(readFile.readLine())); writeFile.close();out.close();readFile.close();in.close(); }catch (FileNotFoundException e) { highPrime.setText("File not found."); }catch (IOException e) { highPrime.setText("Problem setting data."); } } private static void runGui() { primeGui gui = new primeGui(); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { runGui(); } }); } }