java - 当我尝试更新图表时,线程 "Thread-1"java.lang.NullPointerException 中发生异常

标签 java multithreading exception io

我编辑了 JFreeChart 实时图表,现在当我尝试运行它时出现此错误。当我运行它时,GUI 将弹出,只是图表中没有随机数据流。 任何想法都会有很大的帮助。

public class Therm extends ApplicationFrame implements ActionListener{

private static TimeSeries ts;
JTextArea text = new JTextArea("25");
JTextArea degreeC = new JTextArea("degrees C");
JTextArea degreeF = new JTextArea("degrees F");


public Therm(final String title){
super(title);


ts = new TimeSeries("Data", Millisecond.class);
TimeSeriesCollection data = new TimeSeriesCollection(ts);

JFreeChart chart = create(data);
JFrame frame = new JFrame("GraphTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ChartPanel label = new ChartPanel(chart);
frame.getContentPane().add(label);  


ChartPanel chartPanel = new ChartPanel(chart);
JButton buttonC = new JButton("Celsius");
JButton buttonF = new JButton("Farenheit");
JButton button1 = new JButton("Toggle Extension (60 and 300 seconds)");
JButton buttonOff = new JButton("Turn Box");
buttonC.setActionCommand("C");
buttonC.addActionListener(this);
buttonF.setActionCommand("F");
buttonF.addActionListener(this);
button1.setActionCommand("ADD");
button1.addActionListener(this);
buttonOff.setActionCommand("off");
buttonOff.addActionListener(this);



Font font = new Font ("Verdana", Font.BOLD, 26);
text.setFont(font);
degreeC.setFont(font);
degreeF.setFont(font);


JPanel graph = new JPanel(new BorderLayout());
graph.add(chartPanel);
chartPanel.setPreferredSize(new java.awt.Dimension(700,300));
setContentPane(graph);


JPanel content = new JPanel(new GridLayout(3,2));
content.setLayout(new GridLayout(3,2));
content.add(text);
content.add(degreeC);
content.add(buttonC);
content.add(buttonF);
content.add(button1);
content.add(buttonOff);
add(content,BorderLayout.SOUTH);
chartPanel.setPreferredSize(new java.awt.Dimension(700,300));

} 

private JFreeChart create(final XYDataset data){
    JFreeChart chart = ChartFactory.createTimeSeriesChart(
        "Thermometer Reading",
        "Time",
        "Degrees",
        data,
        true,
        true,
        false
    );

    final XYPlot plot = chart.getXYPlot();
    ValueAxis axisy = plot.getDomainAxis();
    axisy.setFixedAutoRange(60000.0);
    axisy = plot.getRangeAxis();
    axisy.setRange(10.0,50.0);
    return chart;
    }



public void actionPerformed(final ActionEvent e) {
    if (e.getActionCommand().equals("C")) {  
        degreeC.setText("degrees C");
    }
    if (e.getActionCommand().equals("F")){
        degreeC.setText("degrees F");
    }
    if (e.getActionCommand().equals("off")){

    }
}


public static void main(String[] args) throws IOException{
    gen myGen = new gen();
    new Thread(myGen).start();



    final Therm prog = new Therm("Thermometer Graph");
    prog.pack();
    prog.setVisible(true);
}



static class gen implements Runnable{
    public void run() {
        while(true) {
            int num = 25 + (int)(Math.random() * ((40 - 30) + 1));
            System.out.println(num);
            ts.addOrUpdate(new Millisecond(), num);
            System.out.println("HI");
            try {
                Thread.sleep(20);
            }
            catch (InterruptedException ex) {
                System.out.println(ex);
            }

        }
    }
}
}

编辑:这里是接收错误“”语法错误,在“gen”处插入“Classbody”以完成类声明”的新代码

static class gen{
    while(true) {
        // don't forget to make num final!
        final int num = 25 + (int)(Math.random() * ((40 - 30) + 1));
        System.out.println(num);
        SwingUtilities.invokeLater(new Runnable(){
          public void run() {
            ts.addOrUpdate(new Millisecond(), num);                
          }
        });
        // ts.addOrUpdate(new Millisecond(), num);
        System.out.println("HI");
        try {
            Thread.sleep(20);
        }
        catch (InterruptedException ex) {
            System.out.println(ex);
        }
    }
}

最佳答案

TimeSeries 是一个 Swing GUI 模型,可能只应在 Swing 事件线程上更新。

如果您像这样将更新添加到事件队列中,会发生什么:

    while(true) {
        // don't forget to make num final!
        final int num = 25 + (int)(Math.random() * ((40 - 30) + 1));
        System.out.println(num);
        SwingUtilities.invokeLater(new Runnable(){
          public void run() {
            ts.addOrUpdate(new Millisecond(), num);                
          }
        });
        // ts.addOrUpdate(new Millisecond(), num);
        System.out.println("HI");
        try {
            Thread.sleep(20);
        }
        catch (InterruptedException ex) {
            System.out.println(ex);
        }
    }

如果这没有帮助,请打印异常的整个堆栈跟踪,并指出堆栈跟踪引用的类中的任何行。

关于java - 当我尝试更新图表时,线程 "Thread-1"java.lang.NullPointerException 中发生异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12791601/

相关文章:

java - Android/Libgdx 如何解决这个问题?

multithreading - 同步3个线程以打印顺序输出

c++ 理解多线程中的 lock_guard 和 mutex

c# - 如何并行运行两个线程?

exception - 蛋糕PHP 2 : new exceptions

objective-c - NSException 引发 :format: as the last statement in a method

java - Apache Camel - 从文件读取 JDBC 数据源属性

java - 正则表达式删除方括号内的逗号和空格

java - 找不到要处理为 'ajaxSingle' 的组件

java - 如何处理渲染 View 时抛出的异常