java - 如何通过 Main - NullpointerException 错误更改进度条的值

标签 java swing nullpointerexception jframe progress-bar

我正在尝试添加一个值并将其显示在我通过 main 在 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   
}

除此之外,我尝试创建一个按钮,我添加了这两行,效果很好
(按钮的代码不在这里)

注意1:我手动将进度条设置为静态,因为如果没有,我会收到错误

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

最佳答案

不要将 JProgressBar 静态化,而是添加一个 getter,以便您可以在 main.c 中获取它。不过,由于您的设计,您将面临其他问题。

public JProgressBar getProgressBar()
{
    return p1hp;
}

然后在你的 main 中:

UI ui = new UI();
final JProgressBar progressBar = ui.getProgressBar();

在您的主要用途中设置值:

public static void main(String[] args) throws IOException {

    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            UI ui = new UI();
            ui.setVisible(true);
            ui.getProgressBar().setStringPainted(true);
            ui.getProgressBar().setValue(12);
        }
    });

关于java - 如何通过 Main - NullpointerException 错误更改进度条的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44313758/

相关文章:

android - 将 Mopub 广告集成到 Cocos2dx 2.1

java - Android ComponentInfo RuntimeException 和 NullPointerException

java - jsoup 代码可以在 Java 中运行,但不能在 Android 中运行 - NullpointerException

java - Android-无法解决java.lang.StringIndexOutOfBoundsException

java -version 返回错误,但 bash 配置文件正确

java - RxJava 去抖动

java - Java中获取当前显示尺寸

java - 如何在 Java Swing 中实现语言按钮?

java - 如何使用给定的 LdapContext 在 ldap whith java 中检查用户密码?

java - 如何在 Swing 中制作一个合适的表格,并使用合适的布局将所有元素放在各自的位置?