0

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("----------");

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;
2
  • 6
    Post your code and where you are getting error. I can say from here oDel is null and that's why you are getting NullPointer exception. Commented Apr 26, 2011 at 18:26
  • 2
    oDel is probably null. where did you initialize it? Commented Apr 26, 2011 at 18:27

2 Answers 2

2

have you instantiated oDel?? (for example, oDel = new FileWriter(..))??

Sign up to request clarification or add additional context in comments.

5 Comments

this.oDel = new OutputDelegate(outputFileName);
i defined it also private OutputDelegate oDel;
You created a reference, but the reference does not point to an object. To see this, write this in your program just before the call to writeFile: System.out.println("oDel: " + oDel); You should see: oDel: null
yes i see it now but this.oDel = new OutputDelegate(outputFileName); does it not provides that point to an object?
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; i initialize objects like this.iDel is not but oDel is null.i couldn't find a solution
1

So it seems like you're instantiating oDel in one of your constructors, but not the other (the one that only instantiates 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? This would obviously create a NullPointerException to be thrown. So you will need oDel to be instantiated in both constructors if you are to implement a method that uses iDel. 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. If you're confused on this, let me know.

1 Comment

Also, if you're referring to fields that are within the same class, you need not use "this" everywhere. It's sufficient to say: "iDel = new InputDelegate(inputFileName);". Unless you're passing in a parameter with the same name. Then the "this" is necessary. Just a random tip.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.