rowRenderer and NullPointerException

asked 2009-03-16 17:11:45 +0800

james gravatar image james
255 2

I am trying to get a listmodel to work and I am using a custom rowRenderer file.
I want to change the background color of a row depending on a few things. But when I check the value of the variables i get an error (java.lang.NullPointerException)


import org.zkoss.zul.RowRenderer;

import org.zkoss.zul.Label;
import org.zkoss.zul.Row;

//define the RowRenderer class 
public class rowRendererTT implements RowRenderer{ 
	public void render(Row row, java.lang.Object data) throws Exception {
		String[] _data = (String[])data;
		if (_data[0].equals("1")){
		} else if (_data[1].equals("0")){
		new Label(_data[2]).setParent(row);
		new Label(_data[3]).setParent(row); 
		new Label(_data[4]).setParent(row);
		new Label(_data[5]).setParent(row); 
		new Label(_data[6]).setParent(row);
		new Label(_data[7]).setParent(row); 
		new Label(_data[8]).setParent(row);
		new Label(_data[9]).setParent(row); 
		new Label(_data[10]).setParent(row);
		new Label(_data[11]).setParent(row); 

	sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	java.lang.reflect.Method.invoke(Unknown Source)

I currently have the "row.setStyle" commented out for testing purposes.
But if I comment out the whole if statement it works just fine. I can even have "new Label(_data[0]).setParent(row);" and it works just fine. _data[0] and _data[1] are filled with data. I can set them as labels and there is no problem. But for some reason, when i access them in the if statement, it errors out on me.
Please help me to figure this out, as I am at a loss. Thank you for any help that can be given.

delete flag offensive retag edit

7 Replies

Sort by ยป oldest newest

answered 2009-03-16 18:24:10 +0800

bob007 gravatar image bob007
384 3 4

updated 2009-03-16 18:24:29 +0800

Hi james,

Maybe i'm wrong but I don't think you should cast from an single object to an array of strings. It may cause problems.

java.lang.Object data
String[] _data = (String[])data;


link publish delete flag offensive edit

answered 2009-03-16 19:06:40 +0800

james gravatar image james
255 2

Hey Steve,
I'm actually quite new zk and java, but that was the way i found to make a row renderer for a listmodel.
my example was from here -> http://www.zkoss.org/smalltalks/livedata/livedataforgrid.dsp
It works well.. other than when trying to access it in the if statement.
However, if you know a better way, i'm willing to try it.

link publish delete flag offensive edit

answered 2009-03-16 19:28:10 +0800

bob007 gravatar image bob007
384 3 4

Personally I prefer to use a List of Object (ex: List of Person).

public class personRowRenderer implements RowRenderer{ 
  public void render(Row row, Object data) throws Exception {
     Person person = (Person) data;
     new Label(person.getFirstName()).setParent(row);
     new Label(person.getLastName()).setParent(row); 

But your way is suppose to work too.

Please can you post your code of where you prepare your data.
//prepare the data model 
     String[][] model = new String[100][2]; 
     for(int j=0; j < model.length; ++j) { 
     	model[0] = "key"+j;
     	model[1] = "value"+j; 
     ListModel strset = new SimpleListModel(model);


link publish delete flag offensive edit

answered 2009-03-16 19:38:18 +0800

james gravatar image james
255 2

updated 2009-03-16 21:59:44 +0800


<grid id="viewGrid" mold="paging" pageSize="20" pagingPosition="both">
		<column label="Product" width="90" />
		<column label="Procedure" width="90" />
		<column label="DT" width="50px" />
		<column label="Cal Date" width="90px" />
		<column label="CT" width="50px" />
		<column label="Amount" width="70px" />
		<column label="Unit" width="70px" />
		<column label="Qty" width="30px" />
		<column label="Patient" width="90" />
		<column label="Note" width="90" />
    String [][] products = mainV.viewOrdersCombine(mainV.getDelDate());
    ListModel productModel = new SimpleListModel(products);
    viewGrid.setRowRenderer(new rowRendererTT());

code in func.java
public String[][] viewOrdersCombine(String date) throws ClassNotFoundException, SQLException{
	String[][] Orders = viewOrdersQuery(date);
	String[][] Rxorders = viewRxordersQuery(date);
	String[][] fullOrders = new String[Orders.length + Rxorders.length][13];
	Integer oNum=0;
	for (oNum=0; oNum<Orders.length; oNum++){
		fullOrders[ oNum][0] = "x";
		fullOrders[ oNum][1] = "x";
		fullOrders[ oNum][2] = Orders[ oNum][2];
		fullOrders[ oNum][3] = Orders[ oNum][3];
		fullOrders[ oNum][4] = Orders[ oNum][5];
		try {
			fullOrders[ oNum][5] = showDFormat(dashYFormat(Orders[ oNum][6]));
		} catch (Exception e) {
			// TODO Auto-generated catch block
		fullOrders[ oNum][6] = Orders[ oNum][7];
		fullOrders[ oNum][7] = Orders[ oNum][8];
		fullOrders[ oNum][8] = Orders[ oNum][9];
		fullOrders[ oNum][9] = Orders[ oNum][10];
		fullOrders[ oNum][10] = Orders[ oNum][12];
		fullOrders[ oNum][11] = Orders[ oNum][14];
	for (Integer rNum=1; rNum<Rxorders.length; rNum++){
		fullOrders[rNum+oNum][0] = Rxorders[ rNum][14];
		fullOrders[rNum+oNum][1] = Rxorders[ rNum][13];
		fullOrders[rNum+oNum][2] = Rxorders[ rNum][1];
		fullOrders[rNum+oNum][3] = Rxorders[ rNum][2];
		fullOrders[rNum+oNum][4] = Rxorders[ rNum][3];
		try {
			fullOrders[rNum+oNum][5] = showDFormat(dashYFormat(Rxorders[ rNum][5]));
		} catch (Exception e) {
			// TODO Auto-generated catch block
		fullOrders[rNum+oNum][6] = Rxorders[ rNum][6];
		fullOrders[rNum+oNum][7] = Rxorders[ rNum][7];
		fullOrders[rNum+oNum][8] = Rxorders[ rNum][8];
		fullOrders[rNum+oNum][9] = Rxorders[ rNum][9];
		fullOrders[rNum+oNum][10] = Rxorders[ rNum][10];
		fullOrders[rNum+oNum][11] = Rxorders[ rNum][12];
	return fullOrders;

// i had to put a space with "rNum" and "oNum" otherwise this forum doesn't display it at all.

the viewOrdersQuery() and viewRxordersQuery() functions just return a string[][] of the data queried from a database.

link publish delete flag offensive edit

answered 2009-03-16 19:58:13 +0800

bob007 gravatar image bob007
384 3 4

Hi james,

I'm not sure at all but I think the problem is

fullOrders[0] = "x";
fullOrders[1] = "x";

Since you trying to put a String where it should be an Array of String ( String[] )

Please try it this way :
fullOrders[0] = {"x"};
fullOrders[1] = {"x"};


link publish delete flag offensive edit

answered 2009-03-16 20:13:25 +0800

james gravatar image james
255 2

I tried "" before but with the NullPointerException, i tried to just fill it. wasn't too sure what to do really.
and its...

fullOrders[ oNum][0] = "x";
fullOrders[ oNum][1] = "x";

because the [ and ] have just "oNum" in the middle with no space... it doesn't show it... I'll do something about that though.
Also, I just tried your suggestion... long error, but I think this is the basis of it.
root cause

java.lang.Error: Unresolved compilation problems: 
	Array constants can only be used in initializers
	Array constants can only be used in initializers

link publish delete flag offensive edit

answered 2009-03-17 20:42:52 +0800

james gravatar image james
255 2

For those who may have a similar problem...
The error was on the "equals()" function.

if (_data[0].equals("1")){

if (_data[0] == "1"){

It works as desired.
Thank you everyone for your input.

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools




Asked: 2009-03-16 17:11:45 +0800

Seen: 387 times

Last updated: Mar 17 '09

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More