Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: NullPointerException with java.io using a gui

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default NullPointerException with java.io using a gui

    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();
                }
            });
        }
    }
    Last edited by killzone; July 18th, 2011 at 04:44 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,319
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: NullPointerException with java.io using a gui

    I get a null pointer exception
    It would help if you copy and pasted the full text of the error message here

    Also please wrap your code in code tags. See the # icon above box.

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException with java.io using a gui

    Anyone? Please.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,319
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: NullPointerException with java.io using a gui

    at java.lang.Double.parseDouble(Double.java:510)
    at primeGui.actionPerformed(primeGui.java:65)

    Looking thru the stack trace you see this reference to a line of your code on the bottom and above it a reference to the method that was called at that line.
    What variable on line 65 is null? Then determine why it is null and change the logic so that it has a valid value.

  5. The Following User Says Thank You to Norm For This Useful Post:

    killzone (July 18th, 2011)

  6. #5
    Junior Member
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException with java.io using a gui

    Thanks. That helped. Now I have some other minor problems to work around, but they look simple so far.

Similar Threads

  1. [SOLVED] Java run time error: 'java.lang.NullPointerException"
    By ChandanSharma in forum What's Wrong With My Code?
    Replies: 9
    Last Post: June 20th, 2011, 12:53 PM
  2. java.lang.nullPointerException
    By ridg18 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 25th, 2010, 03:52 PM
  3. problem in java.lang.NullPointerException
    By jianghuzai in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 28th, 2010, 11:24 AM
  4. java.lang.NullPointerException - Help
    By mds1256 in forum Exceptions
    Replies: 5
    Last Post: November 30th, 2009, 06:31 PM
  5. Java NullPointer Exception in Server chat program
    By Valtros in forum Exceptions
    Replies: 1
    Last Post: May 8th, 2009, 06:06 AM