简体   繁体   English

Java中的空指针异常

[英]Null Pointer Exception in Java

Hi i'm trying to use "ten dashes" between lines and i want to write it to an output file.i used writeFile method and the object oDel: oDel.writeFile("----------"); 嗨,我正在尝试在行之间使用“十破折号”,我想将其写入输出文件。我使用了writeFile方法和对象oDel:oDel.writeFile(“ ----------”) ;

when i write this line eclipse gives me a null pointer execption.what can i do ? 当我写这行日食给我一个空指针执行。我该怎么办? thanks for help. 感谢帮助。

public BankSystem(String dataFileName) { 
    this.iDel = new InputDelegate(dataFileName); 
} 

public BankSystem(String inputFileName, String outputFileName, ArrayList personList, ArrayList bankList, ArrayList branchList) { 
    this.iDel = new InputDelegate(inputFileName); 
    this.oDel = new OutputDelegate(outputFileName); 
    this.personList = personList; 
    this.bankList = bankList; 
    this.branchList = branchList;

have you instantiated oDel?? 您实例化了ODel吗? (for example, oDel = new FileWriter(..) )?? (例如, oDel = new FileWriter(..) )?

So it seems like you're instantiating oDel in one of your constructors, but not the other (the one that only instantiates iDel). 因此,似乎您要在一个构造函数中实例化oDel,而不在另一个构造函数中实例化(仅实例化iDel的一个)。 So, what if you were to instantiate a BankSystem object with the constructor that only instantiates iDel, but not oDel, and then try to use a method that writes the ten dashes with oDel? 因此,如果要用仅实例化iDel而不是oDel的构造函数实例化BankSystem对象,然后尝试使用用oDel写入十个破折号的方法呢? This would obviously create a NullPointerException to be thrown. 显然,这将创建一个NullPointerException被抛出。 So you will need oDel to be instantiated in both constructors if you are to implement a method that uses iDel. 因此,如果要实现使用iDel的方法,则需要在两个构造函数中实例化oDel。 This is the best advice I can give based off your code. 这是我可以根据您的代码给出的最佳建议。 It might be more useful if you provide the code in full. 如果您提供完整的代码,则可能会更有用。 However, I'm pretty confident this is why you're getting the error. 但是,我非常有信心,这就是为什么您会收到错误消息。 What you could do is make a child class that "extends" the BankSystem class so that it incorporates the oDel object and its respective methods. 您可以做的是创建一个子类,该子类“扩展” BankSystem类,以便它包含oDel对象及其各自的方法。 If you're confused on this, let me know. 如果您对此感到困惑,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM