这是我遇到的问题的简化版本。我试图在类数据库中创建一个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