我想添加一個值,並通過進度條我有一個JFrame創建上顯示我的主要(因爲我有方法左側和右側,我想,值從論文1),但是當我運行它,它給了我如何通過你的主要變化的進度條的值 - 空指針異常錯誤
Exception in thread "main" java.lang.NullPointerException
我明白我必須初始化「VAR」所以我不會被收到這個錯誤,但我真的不知道該怎麼。 (我是一種新的Java)
這是我的代碼(這裏有可能是不必要的東西,但他們是通過設計自動創建)
import java.io.IOException;
public class UI extends javax.swing.JFrame {
/**
* Creates new form UI
*/
public UI() {
initComponents();
}
/**
* 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.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
p1hp = new javax.swing.JProgressBar();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(p1hp, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(768, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(469, Short.MAX_VALUE)
.addComponent(p1hp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(100, 100, 100))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
* @throws java.io.IOException
*/
public static void main(String[] args) throws IOException {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new UI().setVisible(true);
}
});
// THIS IS WHAT I AM TRYING TO DO
p1hp.setStringPainted(true);
p1hp.setValue(12);
}
// Variables declaration - do not modify
public static javax.swing.JProgressBar p1hp;
// End of variables declaration
}
一邊,雖然,我嘗試創建一個按鈕,我放入系統中添加2條線,它能正常工作
(代碼爲按鈕心不是在這裏)
注1:我手動maked進度條靜態bacause如果沒有,我會得到錯誤
non-static variable p1hp cannot be referenced from a static context
注2:我看了這樣的回答: What is a NullPointerException, and how do I fix it?
仍然不知道如何解決它
注3:我使用NetBeans 8.2
要從'NullPonterException'預防? – Blasanka
是的,正如我所說,做一個按鈕更具體,它工作得很好,我想從我的主要結果相同,因爲我有一個按鈕 –