简体   繁体   English

构造函数必须调用super()或this()

[英]Constructor must call super() or this()

Can anyone help me with the following error report (for the code at the bottom): 谁能为我提供以下错误报告(针对底部的代码):

Exception in thread "AWT-EventQueue-0" java.lang.VerifyError: Constructor must call super() or this() before return in method org.jfree.ui.RectangleInsets.<init>()V at offset 0
at org.jfree.chart.axis.Axis.<clinit>(Axis.java:153)
at org.jfree.chart.StandardChartTheme.<init>(StandardChartTheme.java:233)
at org.jfree.chart.StandardChartTheme.<init>(StandardChartTheme.java:319)
at org.jfree.chart.ChartFactory.<clinit>(ChartFactory.java:231)
at odesolver.ODESolver.createGraph(ODESolver.java:81)
at odesolver.ODESolver.<init>(ODESolver.java:35)
at odesolver.ODESolver$2.run(ODESolver.java:105)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
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.awt.EventQueue.dispatchEvent(EventQueue.java:703)
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)
BUILD SUCCESSFUL (total time: 2 seconds)  

which relates to the following 3 lines of the code: 与以下三行代码有关:

ODESolver.java:81 ODESolver.java:81

JFreeChart chart = ChartFactory.createXYLineChart(

ODESolver.java:35 ODESolver.java:35

createGraph();

ODESolver.java:105 ODESolver.java:105

new ODESolver(); // Let the constructor do the job

Whole program: 整个程序:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package odesolver;

/**
 *
 * @author User
 */
import java.awt.*;       // Using AWT containers and components
import java.awt.event.*; // Using AWT events and listener interfaces
import javax.swing.*;    // Using Swing components and containers
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.ChartPanel;
import org.jfree.data.general.Series;

// A Swing GUI application inherits the top-level container javax.swing.JFrame
public class ODESolver extends JFrame {
   private JTextField tfInput, tfOutput;
   private int numberIn;   // input number
   private int sum = 0;    // accumulated sum, init to 0

   /** Constructor to setup the GUI */
   public ODESolver() {
      // Retrieve the content-pane of the top-level container JFrame
      // All operations done on the content-pane
      Container cp = getContentPane();
      cp.setLayout(new GridLayout(2, 2, 5, 5));

      createGraph();


      add(new JLabel("Enter an Integer: "));
      tfInput = new JTextField(10);
      add(tfInput);
      add(new JLabel("The Accumulated Sum is: "));
      tfOutput = new JTextField(10);
      tfOutput.setEditable(false);  // read-only
      add(tfOutput);

      // Allocate an anonymous instance of an anonymous inner class that
      //  implements ActionListener as ActionEvent listener
      tfInput.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent e) {
            // Get the String entered into the input TextField, convert to int
            numberIn = Integer.parseInt(tfInput.getText());
            sum += numberIn;      // accumulate numbers entered into sum
            tfInput.setText("");  // clear input TextField
            tfOutput.setText(sum + ""); // display sum on the output TextField
         }
      });

      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  // Exit program if close-window button clicked
      setTitle("ODE Accumulator"); // "this" Frame sets title
      setSize(350, 120);  // "this" Frame sets initial size
      setVisible(true);   // "this" Frame shows


   }

   private JPanel createGraph() {

        JPanel panel = new JPanel();
        XYSeries series = new XYSeries("MyGraph");
        series.add(0, 1);
        series.add(1, 2);
        series.add(2, 5);
        series.add(7, 8);
        series.add(9, 10);


        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(series);

        JFreeChart chart = ChartFactory.createXYLineChart(
                "XY Chart",
                "x-axis",
                "y-axis",
                dataset, 
                PlotOrientation.VERTICAL,
                true,
                true,
                false
                );
        ChartPanel chartPanel = new ChartPanel(chart);


        panel.add(chartPanel);

        return panel;
    }

   /** The entry main() method */
   public static void main(String[] args) {
      // Run the GUI construction in the Event-Dispatching thread for thread-safety
      SwingUtilities.invokeLater(new Runnable() {
         @Override
         public void run() {
            new ODESolver(); // Let the constructor do the job
         }
      });
   }
}

Maybe the problem is that there are files in ODESolver, src and lib with errors, as netbeans reports (see screenshot below). 可能的问题是,如netbeans报告的那样,ODESolver,src和lib中存在文件错误(请参见下面的屏幕截图)。 I don't know which files are upposed to have errors though, as none of then have an exclamation mark on them, as they usually do of they have errors. 我不知道哪些文件被认为有错误,因为它们通常都带有错误,因此没有一个带有感叹号。

netbeans错误

You appear to be running an old version of JFreeChart which produces this error. 您似乎正在运行旧版本的JFreeChart ,它将产生此错误。 Upgrade to version 1.0.13 as found here 升级到版本1.0.13 (在此处找到)

通过将jar文件添加到类路径(而不是包含它们的文件夹)解决了问题

The various constructors of JFrame do important initialisation work, that any JFrame requires. JFrame的各种构造函数执行任何JFrame所需的重要初始化工作。 Therefore, every JFrame that is ever created must have one of those constructors called. 因此,曾经创建的每个JFrame必须具有被调用的那些构造函数之一。 But because an ODESolver is also a JFrame , that applies to ODESolver objects too. 但是因为ODESolver也是JFrame ,所以它也适用于ODESolver对象。

Fortunately, the Java language enforces this. 幸运的是,Java语言实现了这一点。 We can't create an ODESolver , without one of the JFrame constructors getting called. 没有一个JFrame构造函数被调用,我们就无法创建ODESolver The way it enforces it is by requiring every ODESolver constructor to be mapped to a JFrame constructor. 它强制执行的方式是要求将每个ODESolver构造函数映射到JFrame构造函数。

When we create an ODESolver , one of the ODESolver constructors will get called. 当我们创建ODESolver ,将调用ODESolver构造函数之一。 But that constructor must specify which JFrame constructor will get called. 但是该构造函数必须指定将调用哪个JFrame构造函数。 The way it does that is by doing one of the following. 它的操作方式是执行以下任一操作。

  • specifying explicitly which JFrame constructor to use, via a call to super() , with or without some arguments; 通过调用super()来明确指定要使用的JFrame构造函数,带有或不带有某些参数;
  • calling another ODESolver constructor, via a call to this() , with or without some arguments. 通过调用this()来调用另一个ODESolver构造函数,带有或不带有某些参数。

In either case, the call to super() or this() must be the first line of the ODESolver constructor. 无论哪种情况,对super()this()的调用都必须是ODESolver构造函数的第一行。

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

相关问题 构造函数调用必须是 super() 构造函数中的第一条语句 - Constructor call must be the first statement in a constructor in super() 对super()的调用必须是构造函数体中的第一个语句 - call to super() must be the first statement in constructor body 构造函数必须在返回方法之前调用super()或this() - Constructor must call super() or this() before return in method 何时必须使用对超级构造函数的显式调用? - When must an explicit call to super constructor be used? 对 super 的调用必须是构造函数中的第一条语句,但它是 - Call to super must be first statement in the constructor, but it is 使用super()时,构造函数调用必须是构造函数中的第一条语句; - Constructor Call must be the first statement in a constructor while using super(); Zebra.java:3:错误:对super的调用必须是构造函数中的第一条语句 - Zebra.java:3: error: call to super must be first statement in constructor 继承的构造函数java:对super()的调用必须是第一条语句 - Inherited constructor java : Call to super() must be first statement java.lang.VerifyError:构造函数必须在返回之前调用super()或this() - java.lang.VerifyError: Constructor must call super() or this() before return Mockito.spy VerifyError:构造函数必须调用super()或this() - Mockito.spy VerifyError: Constructor must call super() or this()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM