我有4节课。我们来说类:Graphics,Manager,DataInputOutput和Files。 在Graphics中我有一个方法调用Manager中的另一个方法,该方法将调用DataInputOutput中的一个方法,该方法将调用Files中的方法。
但是我总是在方法getCars()上得到java.lang.NullPointerException
异常,我得到它(异常)类DataInputOutPut,Manager和Graphics我知道某些东西没有初始化或指向null但我只是不要到达目的地。有人可以帮助我吗?
呼叫流程如下:Graphics --->经理---> DataOutputInput --->文件
以下是与错误相关的代码:
Classe Graphics:
public class Graphics extends JFrame {
private Manager manager;
public Graphics() {
manager = new Manager();
(..)
tablePanel.setData((manager.getCars()));
}
Classe Manager;
public class Manager {
DataInputOutput io = new DataInputOutput();
public List<Car> getCars()
{
return io.getCars();
}
}
Classe DataInputOutput:
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)
答案 0 :(得分:4)
在
public DataInputOutput() { database = Database.getInstance(); Files files = new Files(); }
行
Files files = new Files();
未设置this.files
。
它定义了一个局部变量,this.files
的值为null
。