- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我一直在编写我的程序,第一次在java中使用GUI。我认为我已经拥有了应有的一切,但给了我一个错误:
Exception in thread "main" java.lang.NullPointerException
at GUIProgram.<init>(GUIProgram.java:41)
at Inventory.main(Inventory.java:12)
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)
我不知道我到底要做什么,我一直在尝试解决它,但我做不到。
如果您需要更多信息来找出问题所在,请告诉我。
编辑(从评论复制)
Product[] array = new Product[table.length];
float total = 0; float fee = 0;
for(Product p: array) {
total += p.getTotal();
fee += p.getRestockingFee();
}
这是我正在构建的 GUIProgram 类。
import javax.swing.*;
import java.awt.*;
public class GUIProgram extends JFrame
{
public GUIProgram()
{
super("Welcome to the Inventory Program");
setLayout(new FlowLayout());
String[] columnNames = {"Item", "Item No.", "Unit", "Price"};
Product[] table = new Product[5];
table[0] = new Product("chocolate", 1023, 124, 1.50f);
table[1] = new Product("ice cream", 1543, 170, 3.35f);
table[2] = new Product("milk", 1265, 230, 2.40f);
table[3] = new Product("orange juice", 1653, 199, 0.60f);
table[4] = new Product("cereal", 1534, 176, 3.50f);
for (int i = 0; i < table.length; i++)
{
JTextField textfield1 = new JTextField(table.length);
add(textfield1);
float total = 0;
float fee = 0;
for(Product p: table)
{
total += p.getTotal();
fee += p.getRestockingFee();
}
JTextField textfield2 = new JTextField(table.length);
textfield2 = new JTextField(String.format("The total value of the Fruits Inventory is: %.2f", total));
add(textfield2);
JTextField textfield3 = new JTextField(table.length);
textfield3 = new JTextField(String.format("The total restocking fee is: %.2f", fee));
add(textfield3);
}
}
}
我只是想为这个数组构建一个 GUI。
最佳答案
它说有一个名为GUIProgram.java
的源文件,其第41行有一些对null
引用的方法调用
由
Product[] array = new Product[table.length];
您声明您将拥有一个 Product 数组,并且数组中现在有空间用于 table.lengh Product 引用
所有这些引用仍然是null
,您需要初始化每个产品
并且这样做
for(Product p: array) {
// initialization
p = new Product();
total += p.getTotal();
fee += p.getRestockingFee();
}
关于java - 这个错误试图在 NetBeans 中告诉我什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18038791/
我是 C++ 的新手,我在使用这段代码时遇到了问题: string output_date(int day, int month, int year){ string date; if
所以我这样做了 tar cvzf test.zip FP 为了创建目录 FP 的 zip 但是,它会列出 zip 中的目录 FP/ FP/php/ FP/php/pdf/ FP/php/docs/ F
我正在尝试在 Swift、Xcode 7.3(所以是 Swift 2.2)中创建一个通用类,但我似乎无法让它通过编译器: protocol Struct1Protocol { } struct Str
我的测试用例是这样的: class FooTest extends PHPUnit_Framework_TestCase { /** @covers MyClass::bar */ f
我正在尝试将brew install wine作为使electron-builder工作的一步。但是我所能得到的只是以下响应: ==> Installing dependencies for wine
我这样做: string[,] string1 = {{"one", "0"},{"Two", "5"},{"Three","1"}}; int b = 0; for(int i = 0; i <=
我正在尝试使用 SetWindowsHookEx 键盘 Hook Notepad.exe。 如您所见,工作线程正在将其 ASCII 代码(即 wParam)发送到指定的服务器。 UINT WINAPI
我正在尝试将 ListView 实现到我的 Fragment 中,但无论我尝试什么,我都会得到一个 NullPointerException。我检查对象是否为 null 并记录是否为 null,看起来
我尝试在一行中对齐两个 div。使用 float left 属性,一切顺利。但是当我在 div 中使用图像时,它开始产生问题。 所以这是我的示例代码:- Some headi
我目前正在使用此代码来获取图像的灰度图像表示并以 (512, 370, 1) 的格式表示它大批。 img_instance = cv2.imread(df.iloc[i][x_col]) / 255.
总结 我正在创建一个简单的应用程序,它允许用户选择一个包含顶级窗口的进程。用户首先键入 native DLL(而非托管 DLL)的路径。然后用户键入将在 Hook 过程中调用的方法的名称。该方法不得返
我是一名优秀的程序员,十分优秀!