java Netbeans 中的方法必须调用 super() 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6560988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 16:22:34  来源:igfitidea点击:

method must call super() error in Netbeans

javaswingnetbeanschartssuperclass

提问by Andrew delgadillo

Recently I've made a Netbeans project and I am using SVN along with it. I am seeing duplicate class error, and in the console it says

最近我做了一个 Netbeans 项目,我正在使用 SVN。我看到重复的类错误,并在控制台中显示

java.lang.VerifyError: (class: pie/chart/explorer/PieChartExplorer, method: <init> signature: ()V) Constructor must call super() or this()
Could not find the main class: pie.chart.explorer.PieChartExplorer. Program will exit.
Exception in thread "main" Java Result: 1

java.lang.VerifyError: (class: pie/chart/explorer/PieChartExplorer, method: <init> signature: ()V) Constructor must call super() or this()
Could not find the main class: pie.chart.explorer.PieChartExplorer. Program will exit.
Exception in thread "main" Java Result: 1

Here is PieChartExplorer.java:

这是 PieChartExplorer.java:

package pie.chart.explorer;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class PieChartExplorer extends JFrame implements ActionListener {


    JTextField one = new JTextField(10);
     JTextField two = new JTextField(10);
      JTextField three = new JTextField(10);
    JButton sub = new JButton("Click to be amazed");


    public PieChartExplorer() {
        super("Pie Chart Explorer");
        setSize(300,100);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        FlowLayout flo = new FlowLayout();
        setLayout(flo);
        setVisible(true);
        add(one);
        add(two);
        add(three);
        sub.addActionListener(this);;
        add(sub);

    }

    public static void main(String[] args) {
        PieChartExplorer app = new PieChartExplorer();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();

        if(source == sub) {
            try {
            Pie show = new Pie(Float.parseFloat(one.getText()),Float.parseFloat(two.getText()),Float.parseFloat(three.getText()));
            } catch(Exception ex) {
                JOptionPane.showMessageDialog(this, "Please check entered data");

            }
        }
    }

}

I have tried:

我努力了:

  1. Clean and Rebuild project
  2. Making sure that I have called super in all constructors
  1. 清理和重建项目
  2. 确保我在所有构造函数中都调用了 super

How can this be fixed? Code for download.

如何解决这个问题?下载代码

采纳答案by Devon_C_Miller

I saw these symptoms just the other day.

前几天我看到了这些症状。

I had I file I had been editing and decided I wanted to split my changes into 2 commits. I went to the directory containing my file "x/y/Z.java", made a directory in "x/y" named "backup", moved "Z.java" there, and pulled a fresh copy from version control. Note all of this was done outside the IDE.

我有我一直在编辑的文件,并决定将我的更改分成 2 个提交。我转到包含我的文件“x/y/Z.java”的目录,在“x/y”中创建一个名为“backup”的目录,将“Z.java”移到那里,并从版本控制中提取一个新副本。请注意,所有这些都是在 IDE 之外完成的。

Back in the IDE I merged in the changes for the first commit and when I built I got the duplicate class message for "Z.java".

回到 IDE,我合并了第一次提交的更改,当我构建时,我收到了“Z.java”的重复类消息。

When I copied the source to "backup" I did it outside the IDE and it still had the original package "x.y" as did my newly edited "Z.java". NB would not compile the new "Z.java" because it could see it had already created "x.y.Z.class" (from "x/y/backup/Z.java").

当我将源代码复制到“备份”时,我是在 IDE 之外进行的,它仍然具有原始包“xy”,就像我新编辑的“Z.java”一样。NB 不会编译新的“Z.java”,因为它可以看到它已经创建了“xyZclass”(来自“x/y/backup/Z.java”)。

There are 2 ways to fix this:

有两种方法可以解决这个问题:

  1. Rename "x/y/backup/Z.java" to "x/y/backup/Z.java.backup". (Prevent the backup copy from being compiled.)
  2. Change the package in "x/y/backup/Z.java" from "x.y" to "x.y.backup". (Make the backup create a different class file.)
  1. 将“x/y/backup/Z.java”重命名为“x/y/backup/Z.java.backup”。(防止编译备份副本。)
  2. 将“x/y/backup/Z.java”中的包从“xy”更改为“xybackup”。(使备份创建一个不同的类文件。)

After making either of these changes, perform a "clean and build". Note:simply building will not fix the problem, you need to perform a clean to remove the rogue class file.

在进行任何这些更改后,执行“清理和构建”。注意:简单的构建并不能解决问题,您需要执行清理以删除流氓类文件。

Note:#1 was done by renaming Z.java from the command line, not within NB. NB will not let you change the file extension.

注意:#1 是通过从命令行重命名 Z.java 完成的,而不是在 NB 中。NB 不会让你更改文件扩展名。

回答by downeyt

I found that renaming the package did not work, the old package was still there.

我发现重命名包不起作用,旧包还在。

The problem for me started when I copied a package from another application into the current application, which already had a package with the same name. I was trying to add some missing classes to the package. After I did that, the error started.

当我将一个包从另一个应用程序复制到当前应用程序时,我的问题就开始了,该应用程序已经有一个同名的包。我试图向包中添加一些缺少的类。在我这样做之后,错误开始了。

To resolve it, I deleted the entire package from the target web app and did a clean and build. Then I copied the source package into the target application. No errors.

为了解决这个问题,我从目标 Web 应用程序中删除了整个包并进行了清理和构建。然后我将源包复制到目标应用程序中。没有错误。

回答by swapnil gandhi

Cleaning and Building solves the problem

清洁和建筑解决问题

回答by smoking_son

If you still have the problem, this is how I solved it..

如果你仍然有问题,这就是我解决它的方法..

In my case I changed the class with main method later and the initial class was still referenced in the proporties file.

在我的情况下,我稍后使用 main 方法更改了类,并且在 proporties 文件中仍然引用了初始类。

Change that setting, clean and build.. It worked for me...

更改该设置,清理并构建..它对我有用......

回答by MaVRoSCy

In my case, i had the same problem in a Web application after making an external copy of a POJO and manually editing it outside NETBEANS. The problem actually was what the others suggested in other answers about a conflict in the already compiled .classfiles.

就我而言,在制作 POJO 的外部副本并在 NETBEANS 之外手动编辑它后,我在 Web 应用程序中遇到了同样的问题。问题实际上是其他人在其他答案中提出的关于已编译.class文件中的冲突的建议。

What i did to overcome this was simply delete the folder webAppname/WEB-INF/classes(where compiled classes reside) and then do a Clean and Build

我为克服这个所做的只是删除文件夹webAppname/WEB-INF/classes(已编译的类所在的文件夹),然后执行Clean and Build

Hope this helps someone

希望这有助于某人