Reputation: 2087
This is an extension to ( Managed-Bean best practice ). I have written a class AppProperties that defines the various items in the Class:
public class AppProperties implements Serializable {
private static final long serialVersionUID = 1L;
//contents of AppProperties Object
private String appRepID;
private String helpRepID;
private String ruleRepID;
private String filePath;
private Vector formNames;
//end content
private Session s;
private String serverName;
public String getAppRepID() {
return appRepID;
}
public void setAppRepID(String appRepID) {
this.appRepID = appRepID;
}
//rest if getters and setters
}
In my bean I have the following:
import ca.wfsystems.core.AppProperties;
private final Map<String, AppProperties> internalMap = new HashMap<String, AppProperties>();
public ApplicationMap() {
this.buildMap(internalMap);
}
private void buildMap(Map<String, AppProperties> theMap) {
try{
AppProperties ap = null;
Session s = ExtLibUtil.getCurrentSession();
vwApps = s.getCurrentDatabase().getView("vwWFSApplications");
veCol = vwApps.getAllEntries();
ve = veCol.getFirstEntry();
tVE = null;
while (ve != null){
Vector colVal = ve.getColumnValues();
String tAppRepID = colVal.get(2).toString();
ap.setAppRepID(colVal.get(2).toString());
ap.setHelpRepID(colVal.get(3).toString());
ap.setRuleRepID(colVal.get(4).toString());
theMap.put(colVal.get(0).toString(), ap);
}
}catch(Exception e){
System.out.println(e.toString());
}finally{
Utils.recycleObjects(s,vwApps);
}
}
everything seems to be ok except at ap.setAppRepID(colVal(2).toString()) there is a compiler error that "Null pointer access The variable ap can only be null at this point" the code for the setAppRepID and setHelpRepID are identical and there is no compiler error of either setHelpRepID or setRuleRepID. I'm not sure if the problem is in the setting AppProperties ap = null tried to create AppProperties ap = new AppProperties but it doesn't like that. I think I'm really close to making this work but ....
Thanks to all who have been very patient with me as I climb up the JAVA slope.
Upvotes: 0
Views: 247
Reputation: 1460
the compiler error is correct, your variable ap can only be null at that point. Follow each statement from
AppProperties ap = null;
to
ap.setAppRepID(colVal.get(2).toString());
and at no point is ap initialised to an object, it will still be null.
You would also see the compiler error on setHelpRepID or setRuleRepID as well but it doesn't bother showing you because it is already a problem with the first statement. You can try this by commenting out the setAppRepID line and you should see the same error on the next line.
make a public constructor in your AppProperties class
public AppProperties() {};
and then try to change
AppProperties ap = null;
to
AppProperties ap = new AppProperties();
Upvotes: 1
Reputation: 4471
It's definitely the "AppProperties ap = null" line. When you say that you tried "AppProperties ap = new AppProprties", did you include "()" at the end (i.e. "AppProperties ap = new AppProperties()
")? It looks like your AppProperties bean has the default argumentless constructor, so that should work. Specifically, guessing from your code, I expect you'll want to move that line to just after the opening of the while loop.
You also have an infinite loop lying in wait: you never set the entry to the next one in the while loop. If you're not using the OpenNTF Domino API, I suggest an idiom like this:
ViewEntry entry = veCol.getFirstEntry();
while(entry != null) {
Vector<?> colVal = ve.getColumnValues();
...
entry.recycle(colVal);
ViewEntry tempEntry = entry;
entry = veCol.getNextEntry();
tempEntry.recycle();
}
Upvotes: 0