2013-09-30 88 views
0

誰能幫我用下面的錯誤報告(該代碼在底部):構造函數必須調用超()或()這個

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) 

其中涉及以下3行代碼:

ODESolver.java:81

JFreeChart chart = ChartFactory.createXYLineChart(

ODESolver.java:35

createGraph(); 

ODESolver.java:105

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

整個程序:

/* 
* 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 
     } 
     }); 
    } 
} 

也許問題是,有在ODESolver,SRC和lib與錯誤的文件,,因爲NetBeans報告(見下面的截圖) 。我不知道哪些文件是錯誤的,儘管它們都沒有感嘆號,因爲它們通常會有錯誤。

netbeans error

+0

我只是想該文件夾解決,但是仍然得到同樣的錯誤 –

+0

添加超()來構造不會有任何效果。當沒有明確指定superconstructor時,編譯器實際上會將默認的super()作爲構造函數的第一行插入。 – Ron

+0

@SubhrajyotiMajumder AFAIK沒有必要明確指定。編譯器會爲你做這件事,即在構造函數的開頭放置一個默認的super()沒有明確指定。 –

回答

0

問題通過添加jar文件到類路徑,而不是包含它們

4

你似乎是運行老版本的JFreeChart產生這個錯誤。升級到1.0.13版本發現here

+0

實際上,我運行的是版本16,不過謝謝。 –

+1

冉對13和工作正常,讓我檢查16 ... – Reimeus

+0

假設你的工作正常與V 16,我想知道你在做什麼,我不是或反之亦然。順便說一句,我正在運行舊版本的JCommon(v8而不是v20):切換修復了super()錯誤,儘管現在有一些不同的錯誤。 –

-1

JFrame各種構造做重要工作的初始化,任何JFrame需要。因此,創建的每個JFrame都必須具有其中一個調用的構造函數。但是因爲ODESolver也是JFrame,它也適用於ODESolver對象。

幸運的是,Java語言強制執行此操作。如果沒有調用JFrame構造函數之一,我們不能創建ODESolver。它強制執行的方式是要求將每個ODESolver構造函數映射到JFrame構造函數。

當我們創建一個ODESolver時,其中一個ODESolver構造函數會被調用。但是該構造函數必須指定將調用哪個構造函數。它的做法是通過執行以下任一操作。

  • 明確指定使用哪個JFrame構造,通過調用super(),具有或不具有一些參數;
  • 調用另一個ODESolver構造函數,通過調用this(),有或沒有一些參數。

在任一情況下,呼叫到super()this()必須是ODESolver構造函數的第一行。

+0

我試着把super()作爲第一行:它沒有解決它。什麼消除了super()錯誤(但讓我有許多其他錯誤)是改變我使用的JCommon版本從8到20。 –

相關問題