我想添加一个值,并通过进度条我有一个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
是的,正如我所说,做一个按钮更具体,它工作得很好,我想从我的主要结果相同,因为我有一个按钮 –