Results 1 to 18 of 18
  1. #1
    wigwom is offline Member
    Join Date
    Sep 2013
    Posts
    14
    Rep Power
    0

    Default Getting an illegal start of expression error

    I am getting an illegal start of expression error here:

    Java Code:
     @Override
         public void actionPerformed (ActionEvent e) {   
         
         public void saveTable()throws Exception   //<----illegal start of expression here
       {
         BufferedWriter bfw = new BufferedWriter(new FileWriter(tmp));
         for(int i = 0 ; i < table.getColumnCount() ; i++)
         {
         bfw.write(table.getColumnName(i));
         bfw.write("\t");
         } 
    
         for (int i = 0 ; i < table.getRowCount(); i++)
         {
         bfw.newLine();
         for(int j = 0 ; j < table.getColumnCount();j++)
         {
         bfw.write((String)(table.getValueAt(i,j)));
         bfw.write("\t");;
         }
       }
      bfw.close();
     
             
             
         }});
        
        }
    I have tried to fix the problem, but I just don't know what to do to fix it.

    Thanks for any help given.

  2. #2
    Join Date
    Sep 2013
    Posts
    1
    Rep Power
    0

    Default Re: Getting an illegal start of expression error

    you should not define method inside a method. Write saveTable() method outside of actionPerformed() method & inside this class, and call it. Then it will execute properly

  3. #3
    wigwom is offline Member
    Join Date
    Sep 2013
    Posts
    14
    Rep Power
    0

    Default Re: Getting an illegal start of expression error

    Quote Originally Posted by danduphalgunkumar View Post
    you should not define method inside a method. Write saveTable() method outside of actionPerformed() method & inside this class, and call it. Then it will execute properly
    Ok, I have made it this far:

    Java Code:
    package testaddress;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.Scanner;
    import javax.swing.table.DefaultTableModel;
    
    
    
    public class Testaddress extends JFrame {
       public static JTable table;
       public static DefaultTableModel model;
        
        public static void MyAddressBook(Container pane) throws FileNotFoundException, IOException{
            
            JLabel Name;
            pane.setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.fill = GridBagConstraints.HORIZONTAL;
            
            Name = new JLabel("Name:");
            gbc.weightx = 0.5;
            gbc.gridx = 0;
            gbc.gridy = 0; 
            gbc.anchor = GridBagConstraints.NORTH;       
            pane.add(Name, gbc);
            
            final JTextField textName = new JTextField();
            gbc.weightx = 0.5;
            gbc.gridx = 1;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.NORTH;
            pane.add(textName, gbc);
            
            JLabel HPhone = new JLabel("Home Phone #:");
            gbc.weightx = 0.5;
            gbc.gridx = 2;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.NORTH;
            pane.add(HPhone, gbc);
            
            final JTextField textHPhone = new JTextField();
            gbc.weightx = 0.5;
            gbc.gridx = 3;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.NORTH;
            pane.add(textHPhone, gbc);
           
           JLabel CellPhone = new JLabel("Cell Phone #:");
           gbc.weightx = 0.5;
           gbc.gridx = 0;
           gbc.gridy = 1;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(CellPhone, gbc);
           
           final JTextField textCPhone = new JTextField();
           gbc.weightx = 0.5;
           gbc.gridx = 1;
           gbc.gridy = 1;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(textCPhone, gbc);
           
           JLabel Address = new JLabel("Address");
           gbc.weightx = 0.5;
           gbc.gridx = 2;
           gbc.gridy = 1;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(Address, gbc);
           
           final JTextField textAddress = new JTextField();
           gbc.weightx = 0.5;
           gbc.gridx = 3;
           gbc.gridy = 1;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(textAddress, gbc);
           
           JLabel City = new JLabel("City:");
           gbc.weightx = 0.5;
           gbc.gridx = 0;
           gbc.gridy = 2;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(City, gbc);
           
           final JTextField textCity = new JTextField();
           gbc.weightx = 0.5;
           gbc.gridx = 1;
           gbc.gridy = 2;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(textCity, gbc);
           
           JLabel State = new JLabel("State:");
           gbc.weightx = 0.5;
           gbc.gridx = 2;
           gbc.gridy = 2;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(State, gbc);
           
           final JTextField textState = new JTextField();
           gbc.weightx = 0.5;
           gbc.gridx = 3;
           gbc.gridy = 2;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(textState, gbc);
           
           JLabel Zip = new JLabel("Zipcode:");
           gbc.weighty = 0.1;
           gbc.weightx = 0.5;
           gbc.gridx = 0;
           gbc.gridy = 3;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(Zip, gbc);
           
           final JTextField textZip = new JTextField();
           gbc.weighty = 0.1;
           gbc.weightx = 0.5;
           gbc.gridx = 1;
           gbc.gridy = 3;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(textZip, gbc);
           
           JLabel Country = new JLabel("Country:");
           gbc.weightx = 0.5;
           gbc.gridx = 2;
           gbc.gridy = 3;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(Country, gbc);
           
           final JTextField textCountry = new JTextField();
           gbc.weighty = 0.1;
           gbc.weightx = 0.5;
           gbc.gridx = 3;
           gbc.gridy = 3;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(textCountry, gbc);
           
           JButton update = new JButton("Update");
           gbc.weightx = 0.5;
           gbc.gridx = 1;
           gbc.gridy = 5;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(update, gbc);
           
           JButton exit = new JButton("Exit");
           gbc.weightx = 0.5;
           gbc.gridx = 3;
           gbc.gridy = 5;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(exit, gbc);
           
           exit.addActionListener(new ActionListener()
        {
        public void actionPerformed(ActionEvent e)
        {
        if (JOptionPane.showConfirmDialog(null, "Do you really want to quit?", "Quit",
        JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
        {
        System.exit(0);
        }
        }
        });
           
           JButton save = new JButton("Save");
           gbc.weightx = 0.5;
           gbc.gridx = 2;
           gbc.gridy = 5;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(save, gbc);
           
           JSeparator sep = new JSeparator();
           sep.setPreferredSize(new Dimension(5,1));
           gbc.weighty = 0.2;
           gbc.weightx = 0.5;
           gbc.gridx = 0;
           gbc.gridy = 7;
           gbc.gridwidth = 5;
           gbc.anchor = GridBagConstraints.CENTER;
           pane.add(sep, gbc);
         
        File fold = new File(System.getProperty("user.home") 
                + "/.MyAddressBook");
        if(!fold.exists()){       
        fold.mkdir();
        }
        final File tmp = new File(System.getProperty("user.home") + 
                "/.MyAddressBook/MyAddressBook.txt");
        if(!tmp.exists()){
        tmp.createNewFile();
        }
          
           
        table = new JTable();
        JScrollPane scroll = new JScrollPane(table);
        String[] colNames = { "Name:", "Home Phone #:", "Cell Phone #:", "Address",
              "City:", "State:", "Zip Code:", "Country:"};
        model = new DefaultTableModel(colNames, 0);
        final FileInputStream is;
        is = new FileInputStream(tmp); 
        InsertData(is);
        gbc.weighty = 1;
        gbc.weightx = 0.5;
        gbc.gridx = 0;  
        gbc.gridy = 8;  
        gbc.gridwidth = 4;
        gbc.gridheight = 9;
        gbc.anchor = GridBagConstraints.CENTER;
        pane.add(new JScrollPane(table), gbc);
        }
        
        public void saveTable()throws Exception
       {
         BufferedWriter bfw = new BufferedWriter(new FileWriter(System.getProperty("user.home") + 
                "/.MyAddressBook/MyAddressBook.txt"));
         for(int i = 0 ; i < table.getColumnCount() ; i++)
         {
         bfw.write(table.getColumnName(i));
         bfw.write("\t");
         } 
    
         for (int i = 0 ; i < table.getRowCount(); i++)
         {
         bfw.newLine();
         for(int j = 0 ; j < table.getColumnCount();j++)
         {
         bfw.write((String)(table.getValueAt(i,j)));
         bfw.write("\t");;
         }
       }
      bfw.close();
     
        
        update.addActionListener(new ActionListener() {  //<----cannot find symbol on update
                 
         
         @Override
         public void actionPerformed(ActionEvent e) {
    
             
             
             
         }});     
             
         }
    
           
     public static void InsertData(FileInputStream is){
        Scanner scan = new Scanner(is);
        String[] array;
        while (scan.hasNextLine()) {
            String line = scan.nextLine();
            if(line.indexOf(",")>-1)
                array = line.split(",");
            else
                array = line.split("\t");
            Object[] data = new Object[array.length];
            System.arraycopy(array, 0, data, 0, array.length);
            model.addRow(data);
        }
        table.setModel(model);
        
        
    }
    
    
        public static void main(String[] args) throws FileNotFoundException, IOException {
            JFrame frame = new Testaddress();
            frame.setTitle("My Address Book");
            frame.setSize(900, 600);
            frame.setResizable(false);
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            MyAddressBook(frame.getContentPane());
            frame.setVisible(true);
        }
    }

    But I am getting a cannot find symbol error. How do I call for the update button in the other method?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: Getting an illegal start of expression error

    getting a cannot find symbol error.
    Please copy the full text of the error message and paste it here. It shows the symbol and the source line.

    How do I call for the update button in the other method?
    If you want methods to be able to reference a variable, define the variable at the class level (not inside a method).
    Then all methods in the class will be able to reference it.
    Last edited by Norm; 09-29-2013 at 07:11 PM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    wigwom is offline Member
    Join Date
    Sep 2013
    Posts
    14
    Rep Power
    0

    Default Re: Getting an illegal start of expression error

    Quote Originally Posted by danduphalgunkumar View Post
    you should not define method inside a method. Write saveTable() method outside of actionPerformed() method & inside this class, and call it. Then it will execute properly
    ok, this is what I have now:

    Java Code:
     public static void saveTable()throws Exception
       {
         BufferedWriter bfw = new BufferedWriter(new FileWriter(System.getProperty("user.home") + 
                "/.MyAddressBook/MyAddressBook.txt"));
         for(int i = 0 ; i < table.getColumnCount() ; i++)
         {
         bfw.write(table.getColumnName(i));
         bfw.write("\t");
         } 
    
         for (int i = 0 ; i < table.getRowCount(); i++)
         {
         bfw.newLine();
         for(int j = 0 ; j < table.getColumnCount();j++)
         {
         bfw.write((String)(table.getValueAt(i,j)));
         bfw.write("\t");;
         }
       }
      bfw.close();
     
        
        update.addActionListener(new ActionListener() {
                 
         
         @Override
         public void actionPerformed(ActionEvent e) {
         
             
         }});     
         }
    To call it in the actionlistener couldn't I use saveTable.invoke(); if so, I have tried this but I keep getting:
    Java Code:
    cannot find symbol
    symbol: variable saveTable

  6. #6
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    732
    Rep Power
    2

    Default Re: Getting an illegal start of expression error

    Sounds like you forgot the brackets in the methodcall to saveTable().

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: Getting an illegal start of expression error

    Java Code:
    saveTable.invoke()
    Where is the variable: saveTable defined?

    Note; Having the same names for variables and methods is confusing. Here saveTable is defined as a method:
    Java Code:
    public static void saveTable()throws Exception
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    wigwom is offline Member
    Join Date
    Sep 2013
    Posts
    14
    Rep Power
    0

    Default Re: Getting an illegal start of expression error

    Quote Originally Posted by Norm View Post
    Java Code:
    saveTable.invoke()
    Where is the variable: saveTable defined?

    Note; Having the same names for variables and methods is confusing. Here saveTable is defined as a method:
    Java Code:
    public static void saveTable()throws Exception
    So am I suppose to make saveTable a variable instead of a method in order for the .invoke command to work?

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: Getting an illegal start of expression error

    You call a method that belongs to a class using a reference to an instance of the class.
    For example:
    Java Code:
     bfw.write("\t");
    The write() method is using btw which is a reference to an instance of the BufferedWriter class.

    Java Code:
    saveTable.invoke()
    What class in invoke() in? Is saveTable a reference to an instance of that class?

    It is confusing to have a method with the same name as the object reference: saveTable.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    wigwom is offline Member
    Join Date
    Sep 2013
    Posts
    14
    Rep Power
    0

    Default Re: Getting an illegal start of expression error

    ok, here how I have my code:

    Java Code:
    package testaddress;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.Scanner;
    import javax.swing.table.DefaultTableModel;
    
    
    
    public class Testaddress extends JFrame {
       public static JTable table;
       public static DefaultTableModel model;
       public static JButton update;
        
        public static void MyAddressBook(Container pane) throws FileNotFoundException, IOException{
            
            JLabel Name;
            pane.setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.fill = GridBagConstraints.HORIZONTAL;
            
            Name = new JLabel("Name:");
            gbc.weightx = 0.5;
            gbc.gridx = 0;
            gbc.gridy = 0; 
            gbc.anchor = GridBagConstraints.NORTH;       
            pane.add(Name, gbc);
            
            final JTextField textName = new JTextField();
            gbc.weightx = 0.5;
            gbc.gridx = 1;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.NORTH;
            pane.add(textName, gbc);
            
            JLabel HPhone = new JLabel("Home Phone #:");
            gbc.weightx = 0.5;
            gbc.gridx = 2;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.NORTH;
            pane.add(HPhone, gbc);
            
            final JTextField textHPhone = new JTextField();
            gbc.weightx = 0.5;
            gbc.gridx = 3;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.NORTH;
            pane.add(textHPhone, gbc);
           
           JLabel CellPhone = new JLabel("Cell Phone #:");
           gbc.weightx = 0.5;
           gbc.gridx = 0;
           gbc.gridy = 1;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(CellPhone, gbc);
           
           final JTextField textCPhone = new JTextField();
           gbc.weightx = 0.5;
           gbc.gridx = 1;
           gbc.gridy = 1;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(textCPhone, gbc);
           
           JLabel Address = new JLabel("Address");
           gbc.weightx = 0.5;
           gbc.gridx = 2;
           gbc.gridy = 1;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(Address, gbc);
           
           final JTextField textAddress = new JTextField();
           gbc.weightx = 0.5;
           gbc.gridx = 3;
           gbc.gridy = 1;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(textAddress, gbc);
           
           JLabel City = new JLabel("City:");
           gbc.weightx = 0.5;
           gbc.gridx = 0;
           gbc.gridy = 2;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(City, gbc);
           
           final JTextField textCity = new JTextField();
           gbc.weightx = 0.5;
           gbc.gridx = 1;
           gbc.gridy = 2;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(textCity, gbc);
           
           JLabel State = new JLabel("State:");
           gbc.weightx = 0.5;
           gbc.gridx = 2;
           gbc.gridy = 2;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(State, gbc);
           
           final JTextField textState = new JTextField();
           gbc.weightx = 0.5;
           gbc.gridx = 3;
           gbc.gridy = 2;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(textState, gbc);
           
           JLabel Zip = new JLabel("Zipcode:");
           gbc.weighty = 0.1;
           gbc.weightx = 0.5;
           gbc.gridx = 0;
           gbc.gridy = 3;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(Zip, gbc);
           
           final JTextField textZip = new JTextField();
           gbc.weighty = 0.1;
           gbc.weightx = 0.5;
           gbc.gridx = 1;
           gbc.gridy = 3;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(textZip, gbc);
           
           JLabel Country = new JLabel("Country:");
           gbc.weightx = 0.5;
           gbc.gridx = 2;
           gbc.gridy = 3;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(Country, gbc);
           
           final JTextField textCountry = new JTextField();
           gbc.weighty = 0.1;
           gbc.weightx = 0.5;
           gbc.gridx = 3;
           gbc.gridy = 3;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(textCountry, gbc);
           
           update = new JButton("Update");
           gbc.weightx = 0.5;
           gbc.gridx = 1;
           gbc.gridy = 5;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(update, gbc);
           
           JButton exit = new JButton("Exit");
           gbc.weightx = 0.5;
           gbc.gridx = 3;
           gbc.gridy = 5;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(exit, gbc);
           
           exit.addActionListener(new ActionListener()
        {
        public void actionPerformed(ActionEvent e)
        {
        if (JOptionPane.showConfirmDialog(null, "Do you really want to quit?", "Quit",
        JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
        {
        System.exit(0);
        }
        }
        });
           
           JButton save = new JButton("Save");
           gbc.weightx = 0.5;
           gbc.gridx = 2;
           gbc.gridy = 5;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(save, gbc);
           
           JSeparator sep = new JSeparator();
           sep.setPreferredSize(new Dimension(5,1));
           gbc.weighty = 0.2;
           gbc.weightx = 0.5;
           gbc.gridx = 0;
           gbc.gridy = 7;
           gbc.gridwidth = 5;
           gbc.anchor = GridBagConstraints.CENTER;
           pane.add(sep, gbc);
         
        File fold = new File(System.getProperty("user.home") 
                + "/.MyAddressBook");
        if(!fold.exists()){       
        fold.mkdir();
        }
        final File tmp = new File(System.getProperty("user.home") + 
                "/.MyAddressBook/MyAddressBook.txt");
        if(!tmp.exists()){
        tmp.createNewFile();
        }
          
           
        table = new JTable();
        JScrollPane scroll = new JScrollPane(table);
        String[] colNames = { "Name:", "Home Phone #:", "Cell Phone #:", "Address",
              "City:", "State:", "Zip Code:", "Country:"};
        model = new DefaultTableModel(colNames, 0);
        final FileInputStream is;
        is = new FileInputStream(tmp); 
        InsertData(is);
        gbc.weighty = 1;
        gbc.weightx = 0.5;
        gbc.gridx = 0;  
        gbc.gridy = 8;  
        gbc.gridwidth = 4;
        gbc.gridheight = 9;
        gbc.anchor = GridBagConstraints.CENTER;
        pane.add(new JScrollPane(table), gbc);
        }
        
        public static void saveTable()throws Exception
       {
         BufferedWriter bfw = new BufferedWriter(new FileWriter(System.getProperty("user.home") + 
                "/.MyAddressBook/MyAddressBook.txt"));
         for(int i = 0 ; i < table.getColumnCount() ; i++)
         {
         bfw.write(table.getColumnName(i));
         bfw.write("\t");
         } 
    
         for (int i = 0 ; i < table.getRowCount(); i++)
         {
         bfw.newLine();
         for(int j = 0 ; j < table.getColumnCount();j++)
         {
         bfw.write((String)(table.getValueAt(i,j)));
         bfw.write("\t");;
         }
       }
      bfw.close();
     
        
        update.addActionListener(new ActionListener() {
                 
         
         @Override
         public void actionPerformed(ActionEvent e) {
         saveTable.invoke();
             
         }});     
         }
    
           
     public static void InsertData(FileInputStream is){
        Scanner scan = new Scanner(is);
        String[] array;
        while (scan.hasNextLine()) {
            String line = scan.nextLine();
            if(line.indexOf(",")>-1)
                array = line.split(",");
            else
                array = line.split("\t");
            Object[] data = new Object[array.length];
            System.arraycopy(array, 0, data, 0, array.length);
            model.addRow(data);
        }
        table.setModel(model);
        
        
    }
    
    
    
        public static void main(String[] args) throws FileNotFoundException, IOException {
            JFrame frame = new Testaddress();
            frame.setTitle("My Address Book");
            frame.setSize(900, 600);
            frame.setResizable(false);
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            MyAddressBook(frame.getContentPane());
            frame.setVisible(true);
        }
    }
    As you can see I put the .invoke command in the actionlistener. I was thinking by doing this it would call on the saveTable method. But I think I got that wrong, if so how would I go about calling on the saveTable method in actionlistener?

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: Getting an illegal start of expression error

    how would I go about calling on the saveTable method
    I discussed how to call a method in post#9. Get a reference to the class the saveTable() method is in and use that to call it. If the code is in the same class as the saveTable() method, just code the call without an explicit reference (the this reference is added by the compiler).
    To call a static method use the classname where the reference variable would go:
    ClassName.staticMethod();
    Last edited by Norm; 10-01-2013 at 09:07 PM.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Posts
    100
    Rep Power
    0

    Default Re: Getting an illegal start of expression error

    you have a method inside of a method and AS far as i know you cannot do that, each method must be enclosed inside of a class, put your method like so

    public myclass{

    public void firstmethod(){

    }
    public void secondmethod(){

    }

    }

  13. #13
    wigwom is offline Member
    Join Date
    Sep 2013
    Posts
    14
    Rep Power
    0

    Default Re: Getting an illegal start of expression error

    Ok, I redid the whole thing to look like this:

    Java Code:
    public static void updateAction(){
        
        update.addActionListener(new ActionListener() {
                 
         
         @Override
         public void actionPerformed(ActionEvent e) {
         try{
         BufferedWriter bfw = new BufferedWriter(new FileWriter(tmp));
         for(int i = 0 ; i < table.getColumnCount() ; i++)
         {
         bfw.write(table.getColumnName(i));
         bfw.write("\t");
         }
         
         for (int i = 0 ; i < table.getRowCount(); i++)
         {
         bfw.newLine();
         for(int j = 0 ; j < table.getColumnCount();j++)
         {
         bfw.write((String)(table.getValueAt(i,j)));
         bfw.write("\t");;
         }
       }
      bfw.close();
            }  catch(IOException ea) {
    
         } 
         }});     
        }
    I get no errors now, but now when I try to use the save button after I enter the information in the fields it won't save. Also when I use the update button after correcting information it does not update the information.

    Java Code:
    package testaddress;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.Scanner;
    import javax.swing.table.DefaultTableModel;
    
    
    
    public class Testaddress extends JFrame {
       public static JTable table;
       public static DefaultTableModel model;
       public static JButton update;
       public static File tmp;
        
        public static void MyAddressBook(Container pane) throws FileNotFoundException, IOException{
            
            JLabel Name;
            pane.setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.fill = GridBagConstraints.HORIZONTAL;
            
            Name = new JLabel("Name:");
            gbc.weightx = 0.5;
            gbc.gridx = 0;
            gbc.gridy = 0; 
            gbc.anchor = GridBagConstraints.NORTH;       
            pane.add(Name, gbc);
            
            final JTextField textName = new JTextField();
            gbc.weightx = 0.5;
            gbc.gridx = 1;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.NORTH;
            pane.add(textName, gbc);
            
            JLabel HPhone = new JLabel("Home Phone #:");
            gbc.weightx = 0.5;
            gbc.gridx = 2;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.NORTH;
            pane.add(HPhone, gbc);
            
            final JTextField textHPhone = new JTextField();
            gbc.weightx = 0.5;
            gbc.gridx = 3;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.NORTH;
            pane.add(textHPhone, gbc);
           
           JLabel CellPhone = new JLabel("Cell Phone #:");
           gbc.weightx = 0.5;
           gbc.gridx = 0;
           gbc.gridy = 1;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(CellPhone, gbc);
           
           final JTextField textCPhone = new JTextField();
           gbc.weightx = 0.5;
           gbc.gridx = 1;
           gbc.gridy = 1;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(textCPhone, gbc);
           
           JLabel Address = new JLabel("Address");
           gbc.weightx = 0.5;
           gbc.gridx = 2;
           gbc.gridy = 1;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(Address, gbc);
           
           final JTextField textAddress = new JTextField();
           gbc.weightx = 0.5;
           gbc.gridx = 3;
           gbc.gridy = 1;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(textAddress, gbc);
           
           JLabel City = new JLabel("City:");
           gbc.weightx = 0.5;
           gbc.gridx = 0;
           gbc.gridy = 2;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(City, gbc);
           
           final JTextField textCity = new JTextField();
           gbc.weightx = 0.5;
           gbc.gridx = 1;
           gbc.gridy = 2;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(textCity, gbc);
           
           JLabel State = new JLabel("State:");
           gbc.weightx = 0.5;
           gbc.gridx = 2;
           gbc.gridy = 2;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(State, gbc);
           
           final JTextField textState = new JTextField();
           gbc.weightx = 0.5;
           gbc.gridx = 3;
           gbc.gridy = 2;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(textState, gbc);
           
           JLabel Zip = new JLabel("Zipcode:");
           gbc.weighty = 0.1;
           gbc.weightx = 0.5;
           gbc.gridx = 0;
           gbc.gridy = 3;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(Zip, gbc);
           
           final JTextField textZip = new JTextField();
           gbc.weighty = 0.1;
           gbc.weightx = 0.5;
           gbc.gridx = 1;
           gbc.gridy = 3;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(textZip, gbc);
           
           JLabel Country = new JLabel("Country:");
           gbc.weightx = 0.5;
           gbc.gridx = 2;
           gbc.gridy = 3;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(Country, gbc);
           
           final JTextField textCountry = new JTextField();
           gbc.weighty = 0.1;
           gbc.weightx = 0.5;
           gbc.gridx = 3;
           gbc.gridy = 3;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(textCountry, gbc);
           
           update = new JButton("Update");
           gbc.weightx = 0.5;
           gbc.gridx = 1;
           gbc.gridy = 5;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(update, gbc);
           
           JButton exit = new JButton("Exit");
           gbc.weightx = 0.5;
           gbc.gridx = 3;
           gbc.gridy = 5;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(exit, gbc);
           
           exit.addActionListener(new ActionListener()
        {
        public void actionPerformed(ActionEvent e)
        {
        if (JOptionPane.showConfirmDialog(null, "Do you really want to quit?", "Quit",
        JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
        {
        System.exit(0);
        }
        }
        });
           
           JButton save = new JButton("Save");
           gbc.weightx = 0.5;
           gbc.gridx = 2;
           gbc.gridy = 5;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(save, gbc);
           
           JSeparator sep = new JSeparator();
           sep.setPreferredSize(new Dimension(5,1));
           gbc.weighty = 0.2;
           gbc.weightx = 0.5;
           gbc.gridx = 0;
           gbc.gridy = 7;
           gbc.gridwidth = 5;
           gbc.anchor = GridBagConstraints.CENTER;
           pane.add(sep, gbc);
         
        File fold = new File(System.getProperty("user.home") 
                + "/.MyAddressBook");
        if(!fold.exists()){       
        fold.mkdir();
        }
        tmp = new File(System.getProperty("user.home") + 
                "/.MyAddressBook/MyAddressBook.txt");
        if(!tmp.exists()){
        tmp.createNewFile();
        }
          
           
        table = new JTable();
        JScrollPane scroll = new JScrollPane(table);
        String[] colNames = { "Name:", "Home Phone #:", "Cell Phone #:", "Address",
              "City:", "State:", "Zip Code:", "Country:"};
        model = new DefaultTableModel(colNames, 0);
        final FileInputStream is;
        is = new FileInputStream(tmp); 
        InsertData(is);
        gbc.weighty = 1;
        gbc.weightx = 0.5;
        gbc.gridx = 0;  
        gbc.gridy = 8;  
        gbc.gridwidth = 4;
        gbc.gridheight = 9;
        gbc.anchor = GridBagConstraints.CENTER;
        pane.add(new JScrollPane(table), gbc);
        }
        
        public static void updateAction(){
        
        update.addActionListener(new ActionListener() {
                 
         
         @Override
         public void actionPerformed(ActionEvent e) {
         try{
         BufferedWriter bfw = new BufferedWriter(new FileWriter(tmp));
         for(int i = 0 ; i < table.getColumnCount() ; i++)
         {
         bfw.write(table.getColumnName(i));
         bfw.write("\t");
         }
         
         for (int i = 0 ; i < table.getRowCount(); i++)
         {
         bfw.newLine();
         for(int j = 0 ; j < table.getColumnCount();j++)
         {
         bfw.write((String)(table.getValueAt(i,j)));
         bfw.write("\t");;
         }
       }
      bfw.close();
            } catch(IOException ea) {
    
         } 
         }});     
        }
    
           
     public static void InsertData(FileInputStream is){
        Scanner scan = new Scanner(is);
        String[] array;
        while (scan.hasNextLine()) {
            String line = scan.nextLine();
            if(line.indexOf(",")>-1)
                array = line.split(",");
            else
                array = line.split("\t");
            Object[] data = new Object[array.length];
            System.arraycopy(array, 0, data, 0, array.length);
            model.addRow(data);
        }
        table.setModel(model);
        
        
    }
    
    
    
        public static void main(String[] args) throws FileNotFoundException, IOException {
            JFrame frame = new Testaddress();
            frame.setTitle("My Address Book");
            frame.setSize(900, 600);
            frame.setResizable(false);
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            MyAddressBook(frame.getContentPane());
            frame.setVisible(true);
        }
    }

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: Getting an illegal start of expression error

    use the save button after I enter the information in the fields it won't save
    Where is the action listener that should be called when the save button is pressed?
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    wigwom is offline Member
    Join Date
    Sep 2013
    Posts
    14
    Rep Power
    0

    Default Re: Getting an illegal start of expression error

    Quote Originally Posted by Norm View Post
    Where is the action listener that should be called when the save button is pressed?
    That is a great question... I must have somehow deleted it somewhere along the way O.o It's a good thing I keep a backup of my work. Anyway here's the code with the save button actionlistner:

    Java Code:
    package testaddress;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.Scanner;
    import javax.swing.table.DefaultTableModel;
    
    
    
    public class Testaddress extends JFrame {
       public static JTable table;
       public static DefaultTableModel model;
       public static JButton update;
       public static File tmp;
        
        public static void MyAddressBook(Container pane) throws FileNotFoundException, IOException{
            
            JLabel Name;
            pane.setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.fill = GridBagConstraints.HORIZONTAL;
            
            Name = new JLabel("Name:");
            gbc.weightx = 0.5;
            gbc.gridx = 0;
            gbc.gridy = 0; 
            gbc.anchor = GridBagConstraints.NORTH;       
            pane.add(Name, gbc);
            
            final JTextField textName = new JTextField();
            gbc.weightx = 0.5;
            gbc.gridx = 1;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.NORTH;
            pane.add(textName, gbc);
            
            JLabel HPhone = new JLabel("Home Phone #:");
            gbc.weightx = 0.5;
            gbc.gridx = 2;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.NORTH;
            pane.add(HPhone, gbc);
            
            final JTextField textHPhone = new JTextField();
            gbc.weightx = 0.5;
            gbc.gridx = 3;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.NORTH;
            pane.add(textHPhone, gbc);
           
           JLabel CellPhone = new JLabel("Cell Phone #:");
           gbc.weightx = 0.5;
           gbc.gridx = 0;
           gbc.gridy = 1;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(CellPhone, gbc);
           
           final JTextField textCPhone = new JTextField();
           gbc.weightx = 0.5;
           gbc.gridx = 1;
           gbc.gridy = 1;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(textCPhone, gbc);
           
           JLabel Address = new JLabel("Address");
           gbc.weightx = 0.5;
           gbc.gridx = 2;
           gbc.gridy = 1;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(Address, gbc);
           
           final JTextField textAddress = new JTextField();
           gbc.weightx = 0.5;
           gbc.gridx = 3;
           gbc.gridy = 1;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(textAddress, gbc);
           
           JLabel City = new JLabel("City:");
           gbc.weightx = 0.5;
           gbc.gridx = 0;
           gbc.gridy = 2;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(City, gbc);
           
           final JTextField textCity = new JTextField();
           gbc.weightx = 0.5;
           gbc.gridx = 1;
           gbc.gridy = 2;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(textCity, gbc);
           
           JLabel State = new JLabel("State:");
           gbc.weightx = 0.5;
           gbc.gridx = 2;
           gbc.gridy = 2;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(State, gbc);
           
           final JTextField textState = new JTextField();
           gbc.weightx = 0.5;
           gbc.gridx = 3;
           gbc.gridy = 2;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(textState, gbc);
           
           JLabel Zip = new JLabel("Zipcode:");
           gbc.weighty = 0.1;
           gbc.weightx = 0.5;
           gbc.gridx = 0;
           gbc.gridy = 3;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(Zip, gbc);
           
           final JTextField textZip = new JTextField();
           gbc.weighty = 0.1;
           gbc.weightx = 0.5;
           gbc.gridx = 1;
           gbc.gridy = 3;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(textZip, gbc);
           
           JLabel Country = new JLabel("Country:");
           gbc.weightx = 0.5;
           gbc.gridx = 2;
           gbc.gridy = 3;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(Country, gbc);
           
           final JTextField textCountry = new JTextField();
           gbc.weighty = 0.1;
           gbc.weightx = 0.5;
           gbc.gridx = 3;
           gbc.gridy = 3;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(textCountry, gbc);
           
           update = new JButton("Update");
           gbc.weightx = 0.5;
           gbc.gridx = 1;
           gbc.gridy = 5;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(update, gbc);
           
           JButton exit = new JButton("Exit");
           gbc.weightx = 0.5;
           gbc.gridx = 3;
           gbc.gridy = 5;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(exit, gbc);
           
           exit.addActionListener(new ActionListener()
        {
        public void actionPerformed(ActionEvent e)
        {
        if (JOptionPane.showConfirmDialog(null, "Do you really want to quit?", "Quit",
        JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
        {
        System.exit(0);
        }
        }
        });
           
           JButton save = new JButton("Save");
           gbc.weightx = 0.5;
           gbc.gridx = 2;
           gbc.gridy = 5;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(save, gbc);
           
           JSeparator sep = new JSeparator();
           sep.setPreferredSize(new Dimension(5,1));
           gbc.weighty = 0.2;
           gbc.weightx = 0.5;
           gbc.gridx = 0;
           gbc.gridy = 7;
           gbc.gridwidth = 5;
           gbc.anchor = GridBagConstraints.CENTER;
           pane.add(sep, gbc);
         
        File fold = new File(System.getProperty("user.home") 
                + "/.MyAddressBook");
        if(!fold.exists()){       
        fold.mkdir();
        }
        tmp = new File(System.getProperty("user.home") + 
                "/.MyAddressBook/MyAddressBook.txt");
        if(!tmp.exists()){
        tmp.createNewFile();
        }
          
           
        table = new JTable();
        JScrollPane scroll = new JScrollPane(table);
        String[] colNames = { "Name:", "Home Phone #:", "Cell Phone #:", "Address",
              "City:", "State:", "Zip Code:", "Country:"};
        model = new DefaultTableModel(colNames, 0);
        final FileInputStream is;
        is = new FileInputStream(tmp); 
        InsertData(is);
        gbc.weighty = 1;
        gbc.weightx = 0.5;
        gbc.gridx = 0;  
        gbc.gridy = 8;  
        gbc.gridwidth = 4;
        gbc.gridheight = 9;
        gbc.anchor = GridBagConstraints.CENTER;
        pane.add(new JScrollPane(table), gbc);
        
        save.addActionListener(new ActionListener() {
                 
        @Override
        public void actionPerformed(ActionEvent e) {
        BufferedWriter writer;
        try {
        writer = new BufferedWriter(new FileWriter(tmp,true));
        writer.write(textName.getText() + "\t" + textHPhone.getText() 
        + "\t" + textCPhone.getText() + "\t" + textAddress.getText() + "\t"
        + textCity.getText() + "\t" + textState.getText() + "\t" + 
        textZip.getText() + "\t" + textCountry.getText());
        writer.newLine();
        writer.close();
        } catch(FileNotFoundException ex) {
        } catch (IOException ex) {
        }
        Scanner scan = new Scanner(is);
        String[] array;
        while (scan.hasNextLine()) {
            String line = scan.nextLine();
            if(line.indexOf(",")>-1)
                array = line.split(",");
            else
                array = line.split("\t");
            Object[] data = new Object[array.length];
            for (int i = 0; i < array.length; i++)
                data[i] = array[i];
            model.addRow(data);
        }
        table.setModel(model);
        }});    
        }
        
        
        
        public static void updateAction(){
        
        update.addActionListener(new ActionListener() {
                 
         
         @Override
         public void actionPerformed(ActionEvent e) {
         try{
         BufferedWriter bfw = new BufferedWriter(new FileWriter(tmp));
         for(int i = 0 ; i < table.getColumnCount() ; i++)
         {
         bfw.write(table.getColumnName(i));
         bfw.write("\t");
         }
         
         for (int i = 0 ; i < table.getRowCount(); i++)
         {
         bfw.newLine();
         for(int j = 0 ; j < table.getColumnCount();j++)
         {
         bfw.write((String)(table.getValueAt(i,j)));
         bfw.write("\t");;
         }
       }
      bfw.close();
            } catch(IOException ea) {
    
         } 
         }});     
        }
    
           
     public static void InsertData(FileInputStream is){
        Scanner scan = new Scanner(is);
        String[] array;
        while (scan.hasNextLine()) {
            String line = scan.nextLine();
            if(line.indexOf(",")>-1)
                array = line.split(",");
            else
                array = line.split("\t");
            Object[] data = new Object[array.length];
            System.arraycopy(array, 0, data, 0, array.length);
            model.addRow(data);
        }
        table.setModel(model);
        
        
    }
    
    
    
        public static void main(String[] args) throws FileNotFoundException, IOException {
            JFrame frame = new Testaddress();
            frame.setTitle("My Address Book");
            frame.setSize(900, 600);
            frame.setResizable(false);
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            MyAddressBook(frame.getContentPane());
            frame.setVisible(true);
        }
    }
    I am still having trouble with the update button, it will not save the information after I correct it.

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: Getting an illegal start of expression error

    The code for the update button's action listener is poorly formatted. Statements should be indented to show logic nesting.

    update button, it will not save the information after I correct it.
    What does the code do when you press the update button? What statements in the action listener method are executed? Add some println statements so you see what the code is doing when it executes. Print out messages showing the contents of variables as they are changed and used.

    One BIG problem is the empty catch block. You should ALWAYS call the printStackTrace() method in catch blocks when debugging code.
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    wigwom is offline Member
    Join Date
    Sep 2013
    Posts
    14
    Rep Power
    0

    Default Re: Getting an illegal start of expression error

    After reading your post I went back and decided to try to debug the code line by line. It turns out that it wasn't even reading the code, so I took it out of the method I have it in and placed it in the main MyAddressBook method. Once I did this it will update once, I can go in and change something in the table and hit update. I can update as long as I don't exit the program, once I run the program again I notice that it saved the cell I changed but it also adds a blank line of cells. I tried to update some changes again it errors on me:

    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at java.io.Writer.write(Writer.java:157)
    	at testaddress.Testaddress$3.actionPerformed(Testaddress.java:261)
    	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    	at java.awt.Component.processMouseEvent(Component.java:6505)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3312)
    	at java.awt.Component.processEvent(Component.java:6270)
    	at java.awt.Container.processEvent(Container.java:2229)
    	at java.awt.Component.dispatchEventImpl(Component.java:4861)
    	at java.awt.Container.dispatchEventImpl(Container.java:2287)
    	at java.awt.Component.dispatchEvent(Component.java:4687)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    	at java.awt.Container.dispatchEventImpl(Container.java:2273)
    	at java.awt.Window.dispatchEventImpl(Window.java:2719)
    	at java.awt.Component.dispatchEvent(Component.java:4687)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
    	at java.awt.EventQueue.access$200(EventQueue.java:103)
    	at java.awt.EventQueue$3.run(EventQueue.java:694)
    	at java.awt.EventQueue$3.run(EventQueue.java:692)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    	at java.awt.EventQueue$4.run(EventQueue.java:708)
    	at java.awt.EventQueue$4.run(EventQueue.java:706)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
    It also wipes my whole file of information, leaving a blank txt file.
    Heres the updated code:

    Java Code:
    package testaddress;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.Scanner;
    import javax.swing.table.DefaultTableModel;
    
    
    
    public class Testaddress extends JFrame {
       public static JTable table;
       public static DefaultTableModel model;
       public static JButton update;
       public static File tmp;
        
        public static void MyAddressBook(Container pane) throws FileNotFoundException, IOException{
            
            JLabel Name;
            pane.setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.fill = GridBagConstraints.HORIZONTAL;
            
            Name = new JLabel("Name:");
            gbc.weightx = 0.5;
            gbc.gridx = 0;
            gbc.gridy = 0; 
            gbc.anchor = GridBagConstraints.NORTH;       
            pane.add(Name, gbc);
            
            final JTextField textName = new JTextField();
            gbc.weightx = 0.5;
            gbc.gridx = 1;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.NORTH;
            pane.add(textName, gbc);
            
            JLabel HPhone = new JLabel("Home Phone #:");
            gbc.weightx = 0.5;
            gbc.gridx = 2;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.NORTH;
            pane.add(HPhone, gbc);
            
            final JTextField textHPhone = new JTextField();
            gbc.weightx = 0.5;
            gbc.gridx = 3;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.NORTH;
            pane.add(textHPhone, gbc);
           
           JLabel CellPhone = new JLabel("Cell Phone #:");
           gbc.weightx = 0.5;
           gbc.gridx = 0;
           gbc.gridy = 1;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(CellPhone, gbc);
           
           final JTextField textCPhone = new JTextField();
           gbc.weightx = 0.5;
           gbc.gridx = 1;
           gbc.gridy = 1;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(textCPhone, gbc);
           
           JLabel Address = new JLabel("Address");
           gbc.weightx = 0.5;
           gbc.gridx = 2;
           gbc.gridy = 1;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(Address, gbc);
           
           final JTextField textAddress = new JTextField();
           gbc.weightx = 0.5;
           gbc.gridx = 3;
           gbc.gridy = 1;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(textAddress, gbc);
           
           JLabel City = new JLabel("City:");
           gbc.weightx = 0.5;
           gbc.gridx = 0;
           gbc.gridy = 2;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(City, gbc);
           
           final JTextField textCity = new JTextField();
           gbc.weightx = 0.5;
           gbc.gridx = 1;
           gbc.gridy = 2;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(textCity, gbc);
           
           JLabel State = new JLabel("State:");
           gbc.weightx = 0.5;
           gbc.gridx = 2;
           gbc.gridy = 2;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(State, gbc);
           
           final JTextField textState = new JTextField();
           gbc.weightx = 0.5;
           gbc.gridx = 3;
           gbc.gridy = 2;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(textState, gbc);
           
           JLabel Zip = new JLabel("Zipcode:");
           gbc.weighty = 0.1;
           gbc.weightx = 0.5;
           gbc.gridx = 0;
           gbc.gridy = 3;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(Zip, gbc);
           
           final JTextField textZip = new JTextField();
           gbc.weighty = 0.1;
           gbc.weightx = 0.5;
           gbc.gridx = 1;
           gbc.gridy = 3;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(textZip, gbc);
           
           JLabel Country = new JLabel("Country:");
           gbc.weightx = 0.5;
           gbc.gridx = 2;
           gbc.gridy = 3;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(Country, gbc);
           
           final JTextField textCountry = new JTextField();
           gbc.weighty = 0.1;
           gbc.weightx = 0.5;
           gbc.gridx = 3;
           gbc.gridy = 3;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(textCountry, gbc);
           
           update = new JButton("Update");
           gbc.weightx = 0.5;
           gbc.gridx = 1;
           gbc.gridy = 5;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(update, gbc);
           
           JButton exit = new JButton("Exit");
           gbc.weightx = 0.5;
           gbc.gridx = 3;
           gbc.gridy = 5;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(exit, gbc);
           
           exit.addActionListener(new ActionListener()
        {
        public void actionPerformed(ActionEvent e)
        {
        if (JOptionPane.showConfirmDialog(null, "Do you really want to quit?", "Quit",
        JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
        {
        System.exit(0);
        }
        }
        });
           
           JButton save = new JButton("Save");
           gbc.weightx = 0.5;
           gbc.gridx = 2;
           gbc.gridy = 5;
           gbc.anchor = GridBagConstraints.NORTH;
           pane.add(save, gbc);
           
           JSeparator sep = new JSeparator();
           sep.setPreferredSize(new Dimension(5,1));
           gbc.weighty = 0.2;
           gbc.weightx = 0.5;
           gbc.gridx = 0;
           gbc.gridy = 7;
           gbc.gridwidth = 5;
           gbc.anchor = GridBagConstraints.CENTER;
           pane.add(sep, gbc);
         
        File fold = new File(System.getProperty("user.home") 
                + "/.MyAddressBook");
        if(!fold.exists()){       
        fold.mkdir();
        }
        tmp = new File(System.getProperty("user.home") + 
                "/.MyAddressBook/MyAddressBook.txt");
        if(!tmp.exists()){
        tmp.createNewFile();
        }
          
           
        table = new JTable();
        JScrollPane scroll = new JScrollPane(table);
        String[] colNames = { "Name:", "Home Phone #:", "Cell Phone #:", "Address",
              "City:", "State:", "Zip Code:", "Country:"};
        model = new DefaultTableModel(colNames, 0);
        final FileInputStream is;
        is = new FileInputStream(tmp); 
        InsertData(is);
        gbc.weighty = 1;
        gbc.weightx = 0.5;
        gbc.gridx = 0;  
        gbc.gridy = 8;  
        gbc.gridwidth = 4;
        gbc.gridheight = 9;
        gbc.anchor = GridBagConstraints.CENTER;
        pane.add(new JScrollPane(table), gbc);
        
        save.addActionListener(new ActionListener() {
                 
        @Override
        public void actionPerformed(ActionEvent e) {
        BufferedWriter writer;
        try {
        writer = new BufferedWriter(new FileWriter(tmp,true));
        writer.write(textName.getText() + "\t" + textHPhone.getText() 
        + "\t" + textCPhone.getText() + "\t" + textAddress.getText() + "\t"
        + textCity.getText() + "\t" + textState.getText() + "\t" + 
        textZip.getText() + "\t" + textCountry.getText());
        writer.newLine();
        writer.close();
        } catch(FileNotFoundException ex) {
        } catch (IOException ex) {
        }
        Scanner scan = new Scanner(is);
        String[] array;
        while (scan.hasNextLine()) {
            String line = scan.nextLine();
            if(line.indexOf("\t")>-1)
                array = line.split("\t");
            else
                array = line.split("\t");
            Object[] data = new Object[array.length];
            for (int i = 0; i < array.length; i++)
                data[i] = array[i];
            model.addRow(data);
        }
        table.setModel(model);
        }});    
        
        update.addActionListener(new ActionListener() {
                 
         
         @Override
         public void actionPerformed(ActionEvent e) {
         try{
         BufferedWriter bfw = new BufferedWriter(new FileWriter(tmp));
         for (int i = 0 ; i < table.getRowCount(); i++)
         {
         bfw.newLine();
         for(int j = 0 ; j < table.getColumnCount();j++)
         {
         bfw.write((String)(table.getValueAt(i,j)));
         bfw.write("\t");;
         }
       }
      bfw.close();
            } catch(IOException ea) {
           System.out.println(
           "Input/Output Exception occurred: " +
           ea.getMessage()); 
         } 
         }});   
        
        }
    
           
     public static void InsertData(FileInputStream is){
        Scanner scan = new Scanner(is);
        String[] array;
        while (scan.hasNextLine()) {
            String line = scan.nextLine();
            if(line.indexOf(",")>-1)
                array = line.split(",");
            else
                array = line.split("\t");
            Object[] data = new Object[array.length];
            System.arraycopy(array, 0, data, 0, array.length);
            model.addRow(data);
        }
        table.setModel(model);
        
        
    }
    
    
    
        public static void main(String[] args) throws FileNotFoundException, IOException {
            JFrame frame = new Testaddress();
            frame.setTitle("My Address Book");
            frame.setSize(900, 600);
            frame.setResizable(false);
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            MyAddressBook(frame.getContentPane());
            frame.setVisible(true);
        }
    }
    I know I am probably a pain in the butt, but thanks so much for helping me.

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: Getting an illegal start of expression error

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at java.io.Writer.write(Writer.java:157)
    at testaddress.Testaddress$3.actionPerformed(Testaddr ess.java:261)
    Look at the code on line 157 and see what variable has a null value when it is executed. Then backtrack to see why that variable does not have a valid non-null value.

    BTW The code should NOT make everything static. Only the main() method should be static. Everything else should be instance variables and methods ie only exist when an instance is created.


    Also you need to fix the formatting of the code. Statements need to be indented to show nested logic.
    For example:
    Java Code:
        public void actionPerformed(ActionEvent e) {
        BufferedWriter writer;
        try {
        writer = new BufferedWriter(new FileWriter(tmp,true));
        writer.write(textName.getText() + "\t" + textHPhone.getText() 
        + "\t" + textCPhone.getText() + "\t" + textAddress.getText() + "\t"
        + textCity.getText() + "\t" + textState.getText() + "\t" + 
        textZip.getText() + "\t" + textCountry.getText());
        writer.newLine();
        writer.close();
        } catch(FileNotFoundException ex) {
        } catch (IOException ex) {
        }
    should be:
    Java Code:
         public void actionPerformed(ActionEvent e) {
           BufferedWriter writer;
           try {
              writer = new BufferedWriter(new FileWriter(tmp,true));
              writer.write(textName.getText() + "\t" + textHPhone.getText() 
                   + "\t" + textCPhone.getText() + "\t" + textAddress.getText() + "\t"
                   + textCity.getText() + "\t" + textState.getText() + "\t" + 
                   textZip.getText() + "\t" + textCountry.getText());
              writer.newLine();
              writer.close();
           } catch(FileNotFoundException ex) {
           } catch (IOException ex) {
           }
    And the catch blocks should NOT be empty. Add calls to the printStackTrace() method.
    Last edited by Norm; 10-06-2013 at 09:55 PM.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. For Loop illegal start of expression error
    By Syed Rizvi in forum New To Java
    Replies: 11
    Last Post: 07-18-2013, 03:56 PM
  2. Error: illegal start of expression
    By iswan in forum JDBC
    Replies: 2
    Last Post: 09-28-2011, 09:29 AM
  3. Illegal start of expression error pls help me!
    By Gayethiri_86 in forum New To Java
    Replies: 12
    Last Post: 05-12-2010, 03:06 PM
  4. Illegal start of expression error
    By lukermsdn111 in forum New To Java
    Replies: 9
    Last Post: 03-22-2010, 01:31 PM
  5. Servlet Error -illegal start of expression
    By raghu9198 in forum Java Servlet
    Replies: 2
    Last Post: 04-21-2009, 11:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •