我在线程“AWT-EventQueue-0”中收到错误异常java.lang.NullPointerException
public class NetworkAnalyzerWindow extends javax.swing.JFrame {
String host = hostName.getText();
/**
* Creates new form NetworkAnalyzerwindow
* @param title
* @throws java.io.IOException
* @throws java.lang.InterruptedException
*/
public NetworkAnalyzerWindow(final String title) throws IOException, InterruptedException {
super(title);
XYDataset dataset = createDataset();
JFreeChart chart = createChart(dataset);
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setSize(pingPanel.getSize());
pingPanel.add(chartPanel);
pingPanel.getParent().validate();
setContentPane(chartPanel);
}
//create dataset
private XYDataset createDataset() throws IOException, InterruptedException
{
XYSeries series1 = new XYSeries("series");
for(int i = 0; i <10; i++){
int ping = PingGraphData.getPing(host);
series1.add(i, ping);
Thread.sleep(1000);
}
XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(series1);
return dataset;
}
//create chart
private JFreeChart createChart(XYDataset dataset){
JFreeChart pingChart = ChartFactory.createXYLineChart(
"Your Ping", //title
"Time", //x axis
"Ping", //y axis
dataset, //data
PlotOrientation.VERTICAL,
false, //include legend
false, //tooltips
false //urls
);
final XYPlot plot = pingChart.getXYPlot();
ValueAxis axis = plot.getDomainAxis();
axis.setAutoRange(true);
axis.setFixedAutoRange(60000.0);
axis = plot.getRangeAxis();
axis.setAutoRange(true);
return pingChart;
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
hostName = new javax.swing.JTextField();
startButton = new javax.swing.JButton();
maxPing = new javax.swing.JLabel();
minPing = new javax.swing.JLabel();
avgPing = new javax.swing.JLabel();
pingPanel = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setAutoRequestFocus(false);
setBackground(new java.awt.Color(255, 255, 255));
setForeground(java.awt.Color.white);
hostName.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
hostName.setText("Enter domain");
hostName.setToolTipText("");
startButton.setBackground(new java.awt.Color(255, 255, 255));
startButton.setText("Start");
startButton.setToolTipText("Click here to test ping");
startButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
startButtonMouseClicked(evt);
}
});
maxPing.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
maxPing.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
maxPing.setText("Max: ms");
maxPing.setToolTipText("Ping");
minPing.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
minPing.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
minPing.setText("Min: ms");
avgPing.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
avgPing.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
avgPing.setText("Avg: ms");
pingPanel.setBackground(new java.awt.Color(255, 255, 255));
javax.swing.GroupLayout pingPanelLayout = new javax.swing.GroupLayout(pingPanel);
pingPanel.setLayout(pingPanelLayout);
pingPanelLayout.setHorizontalGroup(
pingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 504, Short.MAX_VALUE)
);
pingPanelLayout.setVerticalGroup(
pingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 346, Short.MAX_VALUE)
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(23, 23, 23)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(hostName, javax.swing.GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE)
.addComponent(maxPing, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(minPing, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(avgPing, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(startButton, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(pingPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(51, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(41, 41, 41)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(hostName, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(startButton, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(maxPing, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(minPing, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(avgPing, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(pingPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(50, Short.MAX_VALUE))
);
hostName.getAccessibleContext().setAccessibleName("");
pack();
}// </editor-fold>
private void startButtonMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
exec.scheduleAtFixedRate(new Runnable(){
@Override
public void run(){
try {
NetworkAnalyzer.pingCheck(host);
} catch (IOException ex) {
Logger.getLogger(NetworkAnalyzerWindow.class.getName()).log(Level.SEVERE, null, ex);
}
}
}, 0, 1, TimeUnit.SECONDS);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NetworkAnalyzerWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NetworkAnalyzerWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NetworkAnalyzerWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NetworkAnalyzerWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
new NetworkAnalyzerWindow("").setVisible(true);
} catch (IOException ex) {
Logger.getLogger(NetworkAnalyzerWindow.class.getName()).log(Level.SEVERE, null, ex);
} catch (InterruptedException ex) {
Logger.getLogger(NetworkAnalyzerWindow.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
// Variables declaration - do not modify
public static javax.swing.JLabel avgPing;
public static javax.swing.JTextField hostName;
public static javax.swing.JLabel maxPing;
public static javax.swing.JLabel minPing;
public static javax.swing.JPanel pingPanel;
private javax.swing.JButton startButton;
// End of variables declaration
}
`
从错误消息中,似乎错误来自这两行:
String host = hostName.getText();
和
new NetworkAnalyzerWindow("").setVisible(true);
我试图谷歌,但我不明白我做错了什么。可能是因为我仍然是一个菜鸟,但如果有人可以帮我指出正确的方向,那就太好了