Question:
Null exception error?
Jacob
2012-11-17 23:13:29 UTC
I try to run this code.and it gives me a null exception error.And the error is caused by Patron patron = patronManager.retrievePatron(username);
which means patronManager is a null.But how do i initialize it?


import java.util.*;

public class RentalManager {

private ArrayList rentalList;
private PatronManager patronManager;
private DVDManager dvdManager;

private final double RATE = 6.5;

public RentalManager() {
rentalList = new ArrayList();

}

public ArrayList retrieveAll() {
return rentalList;
}

public int addRental(String username, int dvdId) {
Patron patron = patronManager.retrievePatron(username);
DVD dvd = dvdManager.retrieveById(dvdId);

if (patron.getBalance() < RATE) {
return 1;
} else {
Rental rental = new Rental(dvdId,dvd, patron);
rentalList.add(rental);
return 0;
}
}

}
Three answers:
?
2012-11-17 23:20:19 UTC
That's pretty cut and dry:



patronMangager is obviously Null. Show me in the code where you initialized it (i.e. called patronManager = new PatronManager(...)).



btw, you would have gotten a compile time error if you have declared it as final (which I'll bet you anything it SHOULD be final).



...likewise your ArrayList should ALSO be declared as final (ArrayList should ALWAYS be declared as final). In fact, ALL of your members should be final.



The objects should be private final, but your field RATE should be public final (anytime you declare a primitive as final you should also declare it as public--since it cannot be modified anyway).
aQudei
2012-11-17 23:23:00 UTC
Hello there.



You can initialize it by using 'new' keyword.



patronManager = new PatronManager();

dvdManager = new DVDManager();



After these you can use those objects like,



Patron patron = patronManager.retrievePatron(username);

DVD dvd = dvdManager.retrieveById(dvdId);



Good Luck.
2016-11-26 07:19:07 UTC
i don't be attentive to java yet this in elementary terms creates 5 factors interior the array, I beleive int[] result = new int[5]; yet right here you're dealing with 6 factors. I anticipate result[5] is inflicting a difficulty. for (i=0;i<5;i++){ if (result[i] >=ninety && result[i] <= one hundred)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...