init:
deps-jar:
Compiling 1 source file to F:\JavaProject\DrawPad\build\classes
F:\JavaProject\DrawPad\src\DrawPad\DrawPad.java:535: 无法访问的语句
    File fileName = fileChooser.getSelectedFile();
1 错误
生成失败(总时间:0 秒)
这个是什么问题啊 谢谢!!
附源码:public void saveFile()
{
   fileChooser=new JFileChooser();
   fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
   int result=fileChooser.showSaveDialog(this);
   if(result==JFileChooser.CANCEL_OPTION);
        return;
    File fileName = fileChooser.getSelectedFile();        fileName.canWrite();
        if(fileName==null||fileName.getName().equals(""))
             JOptionPane.showMessageDialog(fileChooser,"Invalid File Name",
                     "Invalid File Name",JOptionPane.ERROR_MESSAGE);
         else{
             try{
                 fileName.delete();
                 FileOutputStream fos=new FileOutputStream(fileName);
                 output=new ObjectOutputStream(fos);
                 drawings record;
                 output.writeInt(index);
                 
                 for(int i=0;i<index;i++)
                  {
                     drawings p=itemList[i];
                     output.writeObject(p);
                     output.flush();
                   }
                 output.close();
                 fos.close();
             }
             catch(IOException ioe)
             {
                ioe.printStackTrace();
                
             }
         }
   
}

解决方案 »

  1.   

     if(result==JFileChooser.CANCEL_OPTION);  //这里多了个分号,把他去掉
        return; 
     File   fileName   =   fileChooser.getSelectedFile();           
     fileName.canWrite(); 
      

  2.   

    程序执行到return之后就会向上一级返回,也就是说会返回到调用saveFile() 的那个方法去...这样看saveFile()方法中有永远也执行不到的语句...
      

  3.   


    老兄有几处错误哦,你用我的与你自己的比较一下吧:    public void saveFile()
        {
            fileChooser = new JFileChooser();
            fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            int result = fileChooser.showSaveDialog(this);
            if (result == JFileChooser.CANCEL_OPTION) {
                return;
            }
            else {

                File fileName = fileChooser.getSelectedFile();
                fileName.canWrite();
                if (fileName == null || fileName.getName().equals("")) {
                    JOptionPane.showMessageDialog(fileChooser, "Invalid   File   Name",
                                                  "Invalid   File   Name", JOptionPane.ERROR_MESSAGE);
                }

                else {
                    try {
                        fileName.delete();
                        FileOutputStream fos = new FileOutputStream(fileName);
                        output = new ObjectOutputStream(fos);
                        drawings record;
                        output.writeInt(index);                    for (int i = 0; i < index; i++) {
                            drawings p = itemList[i];
                            output.writeObject(p);
                            output.flush();
                        }
                        output.close();
                        fos.close();
                    }
                    catch (IOException ioe) {
                        ioe.printStackTrace();                }
                }
            }
        }
    好好看看吧,你怎么if后面都不加这个呀{},写java代码的大忌哦!
      

  4.   

        public void saveFile()
        {
            fileChooser = new JFileChooser();
            fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            int result = fileChooser.showSaveDialog(this);
            if (result == JFileChooser.CANCEL_OPTION) {
                return;
            }
            else {
                File fileName = fileChooser.getSelectedFile();
                fileName.canWrite();
                if (fileName == null || fileName.getName().equals("")) {
                    JOptionPane.showMessageDialog(fileChooser, "Invalid   File   Name",
                                                  "Invalid   File   Name", JOptionPane.ERROR_MESSAGE);
                }
                else {
                    try {
                        fileName.delete();
                        FileOutputStream fos = new FileOutputStream(fileName);
                        output = new ObjectOutputStream(fos);
                        drawings record;
                        output.writeInt(index);                    for (int i = 0; i < index; i++) {
                            drawings p = itemList[i];
                            output.writeObject(p);
                            output.flush();
                        }
                        output.close();
                        fos.close();
                    }
                    catch (IOException ioe) {
                        ioe.printStackTrace();                }
                }
            }
        }