-4
    public FileWindow(JMenuItem menuItem,final JTextArea text, JMenuItem save){

    //Open
    menuItem.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent event){
            openFile();

            if(fileToOpen == JFileChooser.APPROVE_OPTION){
                text.setText("");
                try{
                    Scanner scan = new Scanner(new FileReader(Open.getSelectedFile().getPath()));
                    while(scan.hasNext()){
                        text.append(scan.nextLine() + "\n");
                    }
                }catch(Exception e){
                    System.out.println(e.getMessage());
                }
            }
        }
    });

    //Save
    save.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent event){
            saveFile();
            if(fileToSave == JFileChooser.APPROVE_OPTION){
                try{
                    BufferedWriter out = new BufferedWriter(new FileWriter(Save.getSelectedFile().getPath() + ".txt"));
                    out.write(text.getText());
                    out.flush();
                    out.close();

                }catch(Exception e){
                    System.out.println(e.getMessage());
                }
            }
        }
    });
}

I'm getting an error on this line --> "menuItem.addActionListener(new ActionListener){" I tried initiating the variables but that didn't work. Any help would be very appreciated!

jjtraversy
  • 11
  • 4
  • the problem isn't that I don't know what a Null Pointer Exception is, its that I can't seem to find it. My IDE says its on the line that I mentioned earlier. – jjtraversy Apr 24 '14 at 23:59
  • `I don't know what a Null Pointer Exception is,` well a NullPointerException is probably the common Exception you will ever get so you need to learn how to debug the problem. It basically means you have allocated memory to the variable because you didn't invoke the "new" method for that variable. For example `JMenuItem mi = new JMenuItem("testing");`. This is easy to check you simply add a `System.out.println(menuItem);` statement in your method. Then if the output is "null" you check where you invoke the method to see why the variable is "null". – camickr Apr 25 '14 at 00:51

2 Answers2

0

you're most probably getting a NullPointerException because menuitem is null. make sure menuitem is initialized before you call this constructor.

lewmpk
  • 13
  • 3
0

menuItem is null when you run menuItem.addActionListener()

Make sure it isn't null but adding:

if (menuItem == null)
    System.out.println("menuItem is null!");
Anubian Noob
  • 12,897
  • 5
  • 47
  • 69