0

我有4节课。假设类:图形、管理器、数据输入输出和文件。在 Graphics 中,我有一个方法调用 Manager 中的另一个方法,该方法将调用 DataInputOutput 中的一个方法,该方法将调用 Files 中的一个方法。

但是我java.lang.NullPointerException总是在方法 getCars() 上遇到异常,我得到了 DataInputOutPut、Manager 和 Graphics 类的异常(异常)我知道有些东西没有初始化或指向 null,但我就是不知道它在哪里。有人可以帮我吗?

调用流程是这样的:Graphics ---> Manager ---> DataOutputInput ---> Files

以下是与错误相关的代码:

类图形:

public class Graphics extends JFrame {

private Manager manager;

public Graphics() {
manager = new Manager();


(..)

tablePanel.setData((manager.getCars()));

}

班级经理;

public class Manager {

DataInputOutput io = new DataInputOutput();

    public List<Car> getCars()
    {
        return io.getCars();
    }
}

类数据输入输出:

public class DataInputOutput{

private Database database;
private Files files;

public DataInputOutput()
{
    database = Database.getInstance();
    Files files = new Files();
}

     public List<Car> getCars()
     {
         return files.getCars();
     }
}

类文件:

public class Files {

private List<Car> carList;

public Files(){
    carList = new ArrayList<Car>();
}


public void addCar(Car sta) {
        carList.add(sta);
}

public List<Car> getCars() {
        return Collections.unmodifiableList(carList);
}

这是堆栈打印:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at controller.DataInputOutput.getCars(DataInputOutput.java:59)
at controller.Manager.getCars(Manager.java:41)
at view.Graphics.<init>(Graphics.java:125)
at view.App$1.run(App.java:27)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)
4

1 回答 1

4

public DataInputOutput()
{
    database = Database.getInstance();
    Files files = new Files();
}

线

Files files = new Files();

不设置this.files

它定义了一个局部变量,留下this.filesvalue null

于 2013-09-26T00:55:03.333 回答