Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 NullPointerException
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
randint




PostPosted: Sun Aug 07, 2011 11:03 pm   Post subject: NullPointerException

For whatever reason, there is a NullPointerException in a GUI Program I made to open files:
code:

import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class ReadWriteFile extends JFrame implements ActionListener
{
        public JFrame f1 = new JFrame("Open/Save");
        public JFrame f2 = new JFrame("Choose file");
        public JPanel p1 = new JPanel();
        public JPanel p2 = new JPanel();
        public JButton b1 = new JButton ("Open");
        public JButton b2 = new JButton ("Save");
        public JFileChooser fc = new JFileChooser();
        public File f;
        public int select;
        public ReadWriteFile()
        {
                f1.setContentPane(p1);
                f1.setSize(300, 300);
                f1.setResizable(true);
                p1.add(b1);
                p1.add(b2);
                f2.setContentPane(p2);
                f2.setSize(500, 500);
                f2.setResizable(true);
                p2.add(fc);
                b1.addActionListener(this);
                b2.addActionListener(this);
                f1.setVisible(true);
        }
        public void actionPerformed(ActionEvent e)
        {
                if (e.getSource() == b1)
                {
                        f2.setVisible(true);
                        FileRead();
                }
        }
        public void FileRead()
        {
                if (select == fc.APPROVE_OPTION)
                {
                        f = fc.getSelectedFile();
                }
        }
        public static void main(String args[]) throws Exception
        {
                ReadWriteFile rwf = new ReadWriteFile();
        }
}

The java.lang.NullPointerException occurs when the user attempts to open a file, I do not know why this is.[/list]
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Sun Aug 07, 2011 11:47 pm   Post subject: RE:NullPointerException

Because, on the line where this exception is thrown, you are trying to call a method on NULL. Try to step backwards to figure out why that variable did not have any value assigned to it.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: