gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:48:00 27 4
gpt4 key购买 nike

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com