Reputation: 25
***Iam trying to show a list populated with image and text,when iam rendering res file from UI builder iam getting null pointer exception. Following is the code :
@Override
protected void beforeMain(Form f) {
super.beforeMain(f);
final List list = findList(f);
Component selected = createContainer(fetchResourceFile(), "Renderer");
Component unselected = createContainer(fetchResourceFile(), "Renderer");
System.out.println(selected.getName());
list.setRenderer(new GenericListCellRenderer(selected, unselected) {
public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected) {
if ((index + 1) >= list.size()) {
fetchMore(list);
}
return super.getListCellRendererComponent(list, value, index, isSelected);
}
private void fetchMore(final List list) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
NetworkManager networkManager = NetworkManager.getInstance();
networkManager.start();
networkManager.addErrorListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
NetworkEvent n = (NetworkEvent) evt;
n.getError().printStackTrace();
}});
ConnectionRequest request = new ConnectionRequest() {
String array[] ;
String images[];
//StringBuffer sb = new StringBuffer();
Hashtable h;
Vector tweets;
// @Override
protected void postResponse() {
//cmp.setModel(new com.codename1.ui.list.DefaultListModel(array));
}
protected void readResponse(InputStream input) throws IOException {
Result result = Result.fromContent(input, Result.XML);
array = result.getAsStringArray("/music[1]//thumb_url");
int count = list.size();
for (int i = 0; i < array.length; i++) {
// Hashtable tweet = (Hashtable)tweets.elementAt(i);
list.addItem(array[i]);
addAvatar(list, array[i], count + i);
}
System.out.println(""+array.length);
}
private void addAvatar(List list, String array, int i) {
// TODO Auto-generated method stub
//String url = (String)array.get("profile_image_url");
//String user = (String)array.get("from_user");
// if (array == null || array.startsWith("http:") == false) {
// ImageDownloadService doesn't support HTTPS at moment
// return;
//}
ImageDownloadService ids = new ImageDownloadService(array, list, i, "avatar");
ids.setDuplicateSupported(true);
NetworkManager.getInstance().addToQueue(ids);
ImageDownloadService.createImageToStorage(array, list, i, "avatar", "-avatar",
new Dimension(48, 48));
}
protected void handleException(Exception err) {
//An error occured - show a message:
Dialog.show("Alert", "Are you connected to the internet? Check your connection", "Ok", null);
}
};
request.setUrl("http://api.androidhive.info/music/music.xml"); //servlet calling
request.setPost(false);
//infiniteProgress prog=
InfiniteProgress prog=new InfiniteProgress();
Dialog dlg=prog.showInifiniteBlocking();
request.setDisposeOnCompletion(dlg);
networkManager.addToQueueAndWait(request);
}
}); }
Following is the Exception throughed
java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at com.codename1.impl.javase.Executor$1.run(Executor.java:95) at com.codename1.ui.Display.processSerialCalls(Display.java:1075) at com.codename1.ui.Display.mainEDTLoop(Display.java:897) at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120) at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176) Caused by: java.lang.NullPointerException at userclasses.StateMachine.beforeMain(StateMachine.java:117) at generated.StateMachineBase.beforeShow(StateMachineBase.java:218) at com.codename1.ui.util.UIBuilder.showForm(UIBuilder.java:2436) at com.codename1.ui.util.UIBuilder.showForm(UIBuilder.java:2483) at generated.StateMachineBase.startApp(StateMachineBase.java:57) at generated.StateMachineBase.(StateMachineBase.java:31) at generated.StateMachineBase.(StateMachineBase.java:100) at userclasses.StateMachine.(StateMachine.java:38) at com.prime.cnna.MyApplication.start(MyApplication.java:62)
Null pointer Exception on following line
list.setRenderer(new GenericListCellRenderer(selected, unselected) {
});*
Upvotes: 0
Views: 536
Reputation: 52770
Specifying an exception without highlighting the actual line 117 isn't quite helpful.
You are doing a network request from the renderer which is probably not a good approach and would be invoked thousands of times per minute. You should use the model to implement lazy data fetching e.g. https://code.google.com/p/codenameone/source/browse/trunk/CodenameOne/src/com/codename1/cloud/CloudListModel.java
Upvotes: 0