【发布时间】:2015-10-18 20:36:52
【问题描述】:
这是我遇到的问题的简化版本。我正在尝试在类数据库中创建一个 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 = 新数据库()?