0

In my managedBean, fileUpload : Am calling other beans using @ManagedProperty as shown in code, now later in my class i have something like rtParser.getQuote() but it throws NullPointerException, my question is:

How can I initialize rtParser in this class?

@ManagedProperty(value = "#{rtParser}")
private PositionParserRT rtParser;

public PositionParserRT getRtParser()
{
    return rtParser;
}

public void setrtParser(PositionParserRT rtParser)
{
    this.rtParser = rtParser;
}

Updated

I am having similar kind of issue here and would highly appreciate any suggestions.

Community
  • 1
  • 1
Rachel
  • 100,387
  • 116
  • 269
  • 365

1 Answers1

1

The way as you use @ManagedProperty expects the PositionParserRT to be a @ManagedBean too. So put that annotation on the class.

@ManagedBean
@SomeScoped // TODO: Choose the suitable scope.
public class PositionParserRT {
    // ...
}

But if that class is already not a JSF managed bean in the first place (i.e. it has nothing do to with JSF views/models), then you're probably looking for the solution in the wrong direction. If it's a business service, rather make it a @Stateless EJB and inject it by @EJB instead.

@Stateless
public class PositionParserRT {
    // ...
}

with

@EJB
private PositionParserRT rtParser;
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • my `PositionParserRT` is not an EJB, it is just an normal parser which parses the file and then calls other class to do some more processing, am not sure how to register them as i need to get session that i have from facesContext in managedBean as well as in parser and contractMapping class – Rachel Apr 23 '12 at 17:51
  • Well, if it does not represent a business service, then just construct it yourself. You can just pass the session along as constructor argument. I only wonder what's the functional requirement for that as that's smelly. You should rather let the parser return some data and let the backing bean action method handle the necessary session actions itself based on the returned data. – BalusC Apr 23 '12 at 17:58
  • When we say backing bean, we are referring to managedBean, right? – Rachel Apr 23 '12 at 18:00
  • Uh yes, the class which has the `@ManagedBean` annotation for which JSF will manage the instances. – BalusC Apr 23 '12 at 18:01
  • I am had similar kind of issues that I have mentioned in that question and so wanted to see if you share some thoughts. – Rachel Apr 23 '12 at 19:33
  • What are you talking about? Your initial question of how to initialize a bean that's going to be injected by `@ManagedProperty` has already been answered: just make that class a `@ManagedBean` too. Whether that's the right approach for the particular functional requirement or not is indeed a different question, but I don't see any updates in your question which require further explanation. – BalusC Apr 23 '12 at 19:35
  • I have updated question with link to one of my previous question and wanted to get your inputs on that issue also. – Rachel Apr 23 '12 at 19:39
  • Also I have tried putting `ManagedBean` on that class but still am getting `NullPointerException`. – Rachel Apr 23 '12 at 19:40
  • That's an entirely different question and actually too broad. I voted it for close. I've even downvoted Vyoma's answer on that because it makes no sense, not sure why it's counter-upvoted. But again, this is offtopic and not specific enough. As to the `NullPointerException`, perhaps you're trying to access it at the wrong moment? In the constructor instead of the postconstructor, for example. – BalusC Apr 23 '12 at 19:48
  • Why I should not be calling that stuff from constructor but postConstructor, i have never used postConstructor and so am not sure about it. – Rachel Apr 23 '12 at 19:55
  • Becuase it's not possible to set a managed property before the managed bean is constructed. So the managed properties (and all other injected dependencies) are always `null` inside the constructor. Create a method and annotate it with `@PostConstruct`. – BalusC Apr 23 '12 at 19:56
  • Ok, also when function has postConstruct annotation, we are 100 sure that function is always called and then we do not have to call that function explicitly? – Rachel Apr 23 '12 at 19:57
  • 1
    The specification says that, yes. By the way, it's a "method", not a "function". – BalusC Apr 23 '12 at 19:58