简体   繁体   English

Eclipse说我没有编译错误-如何解决?

[英]Eclipse says I have a compilation error when I don't - How to fix?

I'm writing a java project and suddenly, out of nowhere eclipse is reporting that I have a problem with my code. 我在写一个Java项目,突然间,eclipse突然报告我的代码有问题。 It's specific to one file, but others are failing as well as they use a couple functions from the class in question, which they can't seem to find, the functions that is. 它特定于一个文件,但其他文件却失败了,因为它们使用了相关类中的几个函数,这些函数似乎无法找到。 I've tried almost everything I can think of to make this error go away. 我已尽我所能想到的一切方法来使此错误消失。

  • Refreshing project 刷新项目
  • Clean project 清洁项目
  • Remove project and re-import 删除项目并重新导入
  • Removing file, make a new, paste content 删除文件,制作新的粘贴内容
  • Removed errors and cleaned project 删除错误并清理项目

To be specific the first error is: 具体来说,第一个错误是:

Syntax error, insert "}" to complete ClassBody

Just to illustrate, here's the code: 只是为了说明,下面是代码:

package view;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;

import model.HolderCompany;
import model.Row;
import viewmodel.App;

public class PanelMain extends JPanel {

    private PanelTable table;
    private DataDialog editDialog;
    private DataDialog addDialog;
    private ManageCompaniesDialog mcDialog;
    private PanelConsole consolePane;
    private JFrame frame;
    private ArrayList<Row> data;
    private ArrayList<HolderCompany> companies;

    private static String[] labelHeaders = {
        "ID",
        "Deployment Date",
        "IMEI",
        "Name",
        "Model",
        "Software Version",
        "A51 Device",
        "Holder Company",
        "Company E-mail",
        "Company Phone"
    }; //Here is the so called Syntax Error

    public PanelMain(){
        frame = new JFrame("Mobile Sensor Manager");

        setLayout(new BorderLayout());
        JPanel pane = new JPanel(new BorderLayout());

        consolePane = new PanelConsole();

        //Button Pane
        JButton addRow = new JButton("Add Row");
        addRow.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                addDialog = openDialog("Add Row");
                //Wait for return here
                Object[] data = addDialog.getData();
                if(data != null){
                    table.addRowToTable(data);
                    consolePane.write("Added row: " + table.dataToString(data), null);
                }
            }
        });

        JButton editRow = new JButton("Edit Row");
        editRow.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int index = table.getSelectedRow();
                if(index == -1){
                    //Write error message
                    consolePane.write("No row is selected!", null);
                } else {
                    Object[] d1 = table.getData(index);
                    editDialog = openDialog("Edit Row", data);
                    Row data =     App.getSharedResources().getData().get(index); 
                    editDialog = openDialog("Edit Row", data);
                    //Wait for return here
                    Object[] newData = editDialog.getData();
                    Row newRow = editDialog.getRow();
                    if(newData != null){
                        table.changeRowInTable(index, newData);
                        App.getSharedResources().changeRow(index, newRow);
                        consolePane.write("Changed row " + index + " to " + table.dataToString(newData), null);
                    }
                }
            }
        });

        JButton deleteRow = new JButton("Delete Row");
        deleteRow.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int index = table.getSelectedRow();
                if(index == -1){
                    //Write error message
                    consolePane.write("No row is selected!", null);
                } else {
                    consolePane.write("Removed row: " + table.tableIndexToString(index), null);
                    table.deleteRowFromTable(index);
                    App.getSharedResources().removeRow(index);
                }
            }
        });

        JButton manageCompanies = new JButton("Manage Companies");
        manageCompanies.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                mcDialog = openDialog();
            }
        });

        JButton clearDB = new JButton("Clear Database");
        JButton deleteDB = new JButton("Delete Database");

        JPanel buttonPane = new JPanel(new GridLayout(0, 1));
        TitledBorder buttonBorder = new TitledBorder("Buttons");
        buttonPane.setBorder(buttonBorder);

        buttonPane.add(addRow);
        buttonPane.add(editRow);
        buttonPane.add(deleteRow);
        buttonPane.add(new JSeparator());
        buttonPane.add(manageCompanies);
        buttonPane.add(new JSeparator());
        buttonPane.add(clearDB);
        buttonPane.add(deleteDB);

        //Table Pane
        table = new PanelTable();

        JPanel tablePane = new JPanel(new BorderLayout());
        TitledBorder dbContentBorder = new TitledBorder("Database Content");
        tablePane.setBorder(dbContentBorder);
        tablePane.add(table, BorderLayout.CENTER);

        pane.add(buttonPane, BorderLayout.LINE_END);
        pane.add(tablePane, BorderLayout.CENTER);
        pane.add(consolePane, BorderLayout.PAGE_END);

        add(pane, BorderLayout.CENTER);
    }

    public DataDialog openDialog(String name){
        Window win = SwingUtilities.getWindowAncestor(this);
        return new DataDialog(win, new DataTemplate(), name);
    }

    public DataDialog openDialog(String name, Object[] data){
        Window win = SwingUtilities.getWindowAncestor(this);
        return new DataDialog(win, new DataTemplate(), name, data);
    }

    public DataDialog openDialog(String name, Row data){
        Window win = SwingUtilities.getWindowAncestor(this);
        return new DataDialog(win, new DataTemplate(), name, data);
    }

    public ManageCompaniesDialog openDialog() {
        Window win = SwingUtilities.getWindowAncestor(this);
        return new ManageCompaniesDialog(win, new     ManageCompaniesTemplate(companies));
    }

    public void injectCompanies(ArrayList<HolderCompany> companies) {
        this.companies = companies;
    }

    public void injectData(ArrayList<Row> data) {
        this.data = data;
        table.injectDataToTable(data);
    }

    public void addMainToFrame(PanelMain main) {
        frame.add(main);
    }

    public void createAndShowGUI(){
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        //frame.setResizable(false);
        frame.pack();
        frame.setVisible(true);
    }

    public static String[] getHeaders() {
        return labelHeaders;
    }
}

EDIT 编辑

Is it just me or does anyone else get this error? 是我还是其他任何人都会收到此错误?

Any suggestions on how to fix it are welcome! 任何有关如何修复它的建议都欢迎!

Okay. 好的。 I finally managed to fix the error. 我终于设法解决了该错误。

As mentioned in the comments in the top post I could get it to compile if I cleared the constructor. 如顶部文章中的评论所述,如果清除了构造函数,我可以将其编译。 Once I got it to compile I slowly copy pasted the code line by line into my project. 编译完成后,我慢慢将代码逐行复制到项目中。 I left out the ActionListeners and it compiled just fine. 我省略了ActionListeners,它编译得很好。 After this I slowly imported the ActionListener methods one by one and I found a line that resulted in the error. 之后,我慢慢地一步一步导入了ActionListener方法,并发现了导致错误的一行。 It was the line: 一行:

App.getSharedResources().changeRow(index, newRow); //line 106

This method was not yet implemented and it gave me that as an error after the above explained process. 该方法尚未实现,在上述过程之后,给了我一个错误。 I could create the method and the rest of my code compiles as intended now. 我可以创建该方法,其余代码现在可以按预期进行编译。 This was a really weird error. 这是一个非常奇怪的错误。

I have also faced similar issue sometime in past, could you please try to delete the error from 'Problems' view and perform a clean project afterwards, and see if it disappears. 我过去也曾遇到过类似的问题,请您尝试从“问题”视图中删除该错误,然后再执行一个干净的项目,然后查看它是否消失。

If it does not help try: looking into the .log file created by eclipse in your workspace directory. 如果没有帮助,请尝试:在工作区目录中查找eclipse创建的.log文件。 That may hold a clue how to fix this. 这可能会提示如何解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM