0

I'm learning Java 6 EE and I have a simple web app.

I have UserBean class that uses CurrencyManager class. CurrencyManager is application scoped and is a managed bean. UserBean is managed bean and session scoped.

Here is my UserBean:

@ManagedBean
@SessionScoped
public class UserBean implements Serializable{
    private String username;
    private ArrayList<Money> ownedMoney;

    private CurrencyManager currencyManager;
    private BigDecimal credits;

    public UserBean() {
        currencyManager = new CurrencyManager();
        username = "User";
        ownedMoney = new ArrayList<>();
        ownedMoney.add(new Money(new BigDecimal(15000), currencyManager.getCurrency("CZK")));
        ownedMoney.add(new Money(new BigDecimal(100), currencyManager.getCurrency("USD")));
        credits = new BigDecimal(150);
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public BigDecimal getCredits() {
        return credits;
    }

    public void setCredits(BigDecimal credits) {
        this.credits = credits;
    }

    public ArrayList<Money> getOwnedMoney() {
        return ownedMoney;
    }

    public void setOwnedMoney(ArrayList<Money> ownedMoney) {
        this.ownedMoney = ownedMoney;
    }

    public CurrencyManager getCurrencyManager() {
        return currencyManager;
    }

    public void setCurrencyManager(CurrencyManager currencyManager) {
        this.currencyManager = currencyManager;
    }
}

And here my CurrencyManager:

@ManagedBean(name = "currencyManager")
@ApplicationScoped
public class CurrencyManager {
    private HashMap<String, Currency> currencies;

    public CurrencyManager() {
        this.currencies = new HashMap<>();
        currencies.put("CZK", new Currency("CZK", new BigDecimal("0.0503")));
        currencies.put("GBP", new Currency("GBP", new BigDecimal("0.59")));
        currencies.put("EUR", new Currency("EUR", new BigDecimal("1.38")));
        currencies.put("USD", new Currency("USD", new BigDecimal("1.0")));
    }

    public Currency getCurrency(String name){
        return currencies.get(name);
    }

    public java.util.Collection<Currency> getCurrencies() {
        return currencies.values();
    }

    public void setCurrencies(HashMap<String, Currency> currencies) {
        this.currencies = currencies;
    }
}

The code I posted works fine as is. However I don't want to instantiate CurrencyManager in my UserBean class - that's is why I made it ApplicationScoped, since it should be available at all times.

If I remove the instantiation (first line in UserBean constructor) and change declaration to:

@ManagedProperty(value = "#{currencyManager}")
private CurrencyManager currencyManager;

then the first page that queries ownedMoney property in UserBean throws javax.servlet.ServletException: Cant instantiate class: model.UserBean. with root cause of NullPointerException. GlassFish log showed that the NullPtr occurs in UserBean constructor, when I call getCurrency on currencyManager, here:

ownedMoney.add(new Money(new BigDecimal(15000), currencyManager.getCurrency("CZK")));

Can you tell me what I'm doing wrong?

isklenar
  • 974
  • 2
  • 14
  • 34

1 Answers1

0

I just came across the same problem, and found out by chance, that it is not working, if I try with firefox (actually icedove under linux), but well working, if I try with the eclipse build-in browser.

Even so this does not make sense to me, have you tried with different browsers already?

Thomas Schütt
  • 832
  • 10
  • 14