0

What's wrong with this Null Pointer ?

asked 2011-07-25 23:09:27 +0800

gellaps gravatar image gellaps
157 1

I have many cases that causing a Null Pointer,

how can this happen?

this is some examples,

1. I have an overlapped window, and when the window's closed, i want to clear the text field, but have an null pointer in the Textbox.

   public void onClose$winLoginAdmin(Event event){
        if (event instanceof ForwardEvent){ 
            event = ((ForwardEvent)event).getOrigin(); 
        }
         event.stopPropagation(); //stop event to prevent window be detached

         txtUsernameAdmin.setText("");
         txtPasswordAdmin.setText("");
         
         String jqCommand = "jq('$winLoginAdmin').fadeOut(1000)";
         Clients.evalJavaScript(jqCommand);
   }

when i change like this

   public void onClose$winLoginAdmin(Event event){
        if (event instanceof ForwardEvent){ 
            event = ((ForwardEvent)event).getOrigin(); 
        }
         event.stopPropagation(); //stop event to prevent window be detached
        
         try {
        	 txtUsernameAdmin.setText("");
        	 txtPasswordAdmin.setText("");
         }catch (Exception e) {
			e.printStackTrace();
	}
        String jqCommand = "jq('$winLoginAdmin').fadeOut(1000)";
        Clients.evalJavaScript(jqCommand);
   }

when i close the window, the error is not appear, but still have an Null Pointer warning in Console..

2. i have a popup window, and i set the window visible false, and when i click some button, i want the window appear with some label that have a value filled by user.

	public void onClick$tbbStart(Event event){
		winStart.setVisible(true);
		winStart.doPopup();
		winStart.setSizable(false);
                com.gellaps.entity.User users = (com.gellaps.entity.User) session.getAttribute("User");
	        lblUser.setValue(users.getFname());
	}

and got a Null Pointer Error too, from the label (lblUser)


did my code is wrong?

Thanks for your times..

delete flag offensive retag edit

9 Replies

Sort by ยป oldest newest

answered 2011-07-25 23:25:01 +0800

woodyki gravatar image woodyki
168

Please check the The closable Property from Developer Guide

Window closable Property

link publish delete flag offensive edit

answered 2011-07-26 23:48:08 +0800

gellaps gravatar image gellaps
157 1

thanks woodyki for your advise,

but nothing in that link refers to my Problem,

do you have any suggestion?

link publish delete flag offensive edit

answered 2011-07-27 00:48:05 +0800

woodyki gravatar image woodyki
168

May be the ID space

ID Space

link publish delete flag offensive edit

answered 2011-07-27 23:21:43 +0800

gellaps gravatar image gellaps
157 1

sorry woodyki. i still dont understand, :(

can u give me an example. cause i think i have put the unique ID for every component.

link publish delete flag offensive edit

answered 2011-07-28 01:01:11 +0800

woodyki gravatar image woodyki
168

How to get the Label object?
It's parent should be the Window ID "winStart".

It should be wired as below:
Label lblUser = (Label)winStart.getFellow("lblUser");

Above is just my assumption for your case.

link publish delete flag offensive edit

answered 2011-07-28 02:08:28 +0800

gellaps gravatar image gellaps
157 1

Hi woodyki

i have tried to change my code as you said before

my previous

         txtUsernameAdmin.setText("");
         txtPasswordAdmin.setText("");


my new code

        Textbox txtUsernameAdmin = (Textbox) winLoginAdmin.getFellow("txtUsernameAdmin");
     	txtUsernameAdmin.setText("");
     	
     	Textbox txtPasswordAdmin = (Textbox) winLoginAdmin.getFellow("txtPasswordAdmin");
     	txtPasswordAdmin.setText("");


and voila, it's pretty works :)
many thanks woodyki, this so brilliant .. :)

link publish delete flag offensive edit

answered 2011-07-28 04:19:20 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

updated 2011-07-28 04:20:43 +0800

gellaps,

the guys from zk have spent a lot of time for creating the GenericForwardComposer.
If you extend your controller from this GenericForwardComposer the autowiring of the zul components are done for you automatically.

So you need only to declare the components with the same id as you declared in the zul and you have full access to it.

public class MyLoginComposer extends GenericForwardComposer {

  private Textbox txtUsernameAdmin;  // autowired
  private Textbox txtPasswordAdmin;  // autowired

  . . .


   public void myMethod() {
        txtUsernameAdmin.setText("");
        txtPasswordAdmin.setText("");
       }

}

best
Stephan

link publish delete flag offensive edit

answered 2011-07-28 21:40:43 +0800

gellaps gravatar image gellaps
157 1

updated 2011-07-28 22:13:38 +0800

@Stephan,
yes, in fact, i always extends my Composers to GenericForwardComposer.

like this


import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.GenericForwardComposer;

public class LoginComposer extends GenericForwardComposer {

	protected static final long serialVersionUID = 1L;
	
	private Textbox txtUsernameAdmin;
	private Textbox txtPasswordAdmin;
	
	private Window winLoginAdmin;
	
	public void onClose$winLoginAdmin(Event event){
        if (event instanceof ForwardEvent){ 
            event = ((ForwardEvent)event).getOrigin(); 
        }
        event.stopPropagation(); //stop event to prevent window be detached

     	txtUsernameAdmin.setText("");
     	txtPasswordAdmin.setText("");
      
     	String jqCommand = "jq('$winLoginAdmin').fadeOut(1000)";
     	Clients.evalJavaScript(jqCommand);
		 
	}
}

and i have a Null pointer error, until i change the code like what woodyki said.

thanks Stephan for your times

link publish delete flag offensive edit

answered 2011-07-29 01:01:01 +0800

woodyki gravatar image woodyki
168

updated 2011-07-29 01:01:20 +0800

This is not a problem about auto-wired feature of the GenericForwardComposer.

The Composer applied by the main Window can get all components ID on the first level only.
Each nested/child Window component is a ID space owner.
All components inside a child/nested ID space are invisible in the parent ID space (Window).

So, it is need to get the component object by the childWindow.getFellow("component ID").

Woody

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

Follow

RSS

Stats

Asked: 2011-07-25 23:09:27 +0800

Seen: 368 times

Last updated: Jul 29 '11

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