這是我遇到的問題的簡化版本。我試圖在類數據庫中創建一個ProduceItem數組。我概述了我在嘗試中遇到的警告和問題。提前謝謝你的幫助。在另一個類中創建一個類的數組
import javax.swing.*;
import java.awt.*;
public class Test{
public static void main(String[] args) {
//attempt 1:
//database a;
// Warning: local variable a is not used.
// Warning: Null pointer access: The variable a can only be null at this location
//attempt 2:
//database a;
//a.test[0].setCode(2);
//local variable has not been initialize
//results in attempt 2 part 2
//database a = null;
//a.test[0].setCode(2);
//Null pointer access: The variable a can only be null at this location
//When I run it, Exception in thread "main" java.lang.NullPointerException
//at Test.main(Test.java:8)
//\which is a.test[0].setCode(1);
}
public class ProduceItem{
private int code;
public ProduceItem(){
code = 0;
}
public int getCode(){
return code;}
public void setCode(int a){
code = a;}
public class database{
ProduceItem[] test;
}
a = new database()? – MadProgrammer