1

Need help i want to set url image from String parameter in my code below wich is i got from table path in my database here is my .zul code:

   <div data-u="slides" apply="id.my.berkah.web.controller.ListTopController" style="cursor: default; position: relative; top: 0px; left: 0px; width: 900px; height: 250px; overflow: hidden;">
            <div data-p="112.50" style="display: none;">
            <img data-u="image" src="ImageProgram/Tsel-Banner.jpg" />
            </div>
            <div data-p="112.50" style="display: none;">
            <img data-u="image" src="ImageProgram/Xl Baner.jpg" />
            </div>
            <div data-p="112.50" style="display: none;">
            <img data-u="image" src="ImageProgram/IndosatBnr.jpg" />
            </div>
            <div id="jssor_1" apply="id.my.berkah.web.controller.ListTopController" data-p="112.50" style="display: none;">
            <image id="bg4" data-u="image" src="" />
            </div>
            <a data-u="ad" href="http://www.jssor.com" style="display:none">Responsive Slider</a>

        </div>

and here is my Controller i am using import org.zkoss.zul.Image :

@Wire 
Image bg4;

 @Listen("onCreate=#listTop")
    public void initWindow() throws IOException {
    AImage tempImage=null;    
    prmTrxTop par=new prmTrxTop(); 
    par.setPrmOutlet("%%");
    ReportImpl list = new ReportImpl();
    List<TopModel> result = list.selectTop(par);
    String image1=result.get(0).getImageModel1();
    bg4.setSrc(image1);
    }

but it just give me null pointer exception, what should i do to set src Image from my table path in my database.

syam houng
  • 307
  • 4
  • 14
  • it's look like my variable bg4 is not identified, but i have @Wire it – syam houng Mar 31 '16 at 08:19
  • what composer are you using and why are you applying twice the same controller while the first has complete acces to the div? – chillworld Mar 31 '16 at 16:25
  • hello @chillworld i am using SelectorComposer, i am still not good to use zk, in here i am try to applying controller in my first div and i am geting null pointer exception so i am applying in a specified div wich is contain image that i want to change the image src but it still null pointer exception, please need help – syam houng Mar 31 '16 at 18:27
  • 1
    Try adding a button and listen to that for testing purpose. I think the onCreate in triggered when the image is not het created. – chillworld Apr 01 '16 at 04:27
  • i have create onClick event in a button like this : – syam houng Apr 02 '16 at 02:20
  • @Listen("onClick=#btnLg") public void click(){ Path parent = new Path("//pg_dashboard/winDashboard"); Image change = (Image)new Path(parent, "bg4").getComponent(); Messagebox.show("SHOWW"); change.setSrc("ImageProgram/IndosatBnr.jpg"); } and the null pointer exception apear on the first then my messagebox showed up on the second. – syam houng Apr 02 '16 at 02:30
  • In the button click is your bg4 wired or not? If yes move code to aftercompose – chillworld Apr 02 '16 at 07:07
  • yes i have @Wire Image bg4, and create doAfterCompose event like this : public void doAfterCompose(Component comp) throws Exception{ super.doAfterCompose(comp); bg4.setSrc("ImageProgram/IndosatBnr.jpg"); } but still Null pointer exception – syam houng Apr 02 '16 at 17:20
  • @Tunaki it's not really duplicated. The var is null because the wiring of zk isn't working for some reason. He is trying to find out why the wiring doesn't succeed or how to solve it. As you see in mine answer, that is no normaal nullpointer solution – chillworld Apr 03 '16 at 16:47
  • @chillworld Yes, I agree that there is more to it than a simple NullPointerException but I still decided to close it because it appears that the OP doesn't know what this exception is and what it means. When faced with this situation, I find it best to redirect them to the best Q/A on SO for that. It may not solve their issue directly but it'll help them phrase the problem better (and, thus, find potential other questions out there or write a question more orientied towards "Why is ... not injecting my variable" than "Why am I getting a NPE?") – Tunaki Apr 03 '16 at 16:52
  • @tunaki your correct about the phrasing, it could have been a beter question. Also I don't think I see the whole picture here because I don't see any listTop id in the zul. – chillworld Apr 03 '16 at 18:20
  • @tunaki should i change my question title ? – syam houng Apr 04 '16 at 08:24

1 Answers1

0

I first make some comments to see the direction what it was going.
The most easy fix is just changing to MVVM.

MVVM is easier to understand, no need to wire and reusable zul or ViewModel.

How do we start, first we change the zul :

Zul :

<div data-u="slides" apply="org.zkoss.bind.BindComposer" viewModel="@id('vm') @init('id.my.berkah.web.controller.ListTopController')" style="cursor: default; position: relative; top: 0px; left: 0px; width: 900px; height: 250px; overflow: hidden;">
    <div data-p="112.50" style="display: none;">
        <img data-u="image" src="ImageProgram/Tsel-Banner.jpg" />
    </div>
    <div data-p="112.50" style="display: none;">
        <img data-u="image" src="ImageProgram/Xl Baner.jpg" />
    </div>
    <div data-p="112.50" style="display: none;">
        <img data-u="image" src="ImageProgram/IndosatBnr.jpg" />
    </div>
    <div id="jssor_1" data-p="112.50" style="display: none;">
         <image id="bg4" data-u="image" src="@load(vm.myImage)" />
         <button label="change img source" onClick="@command('changeSrc') onCreate="@command('changeSrc')"/>
    </div>
    <a data-u="ad" href="http://www.jssor.com" style="display:none">Responsive Slider</a>
</div>

Java:

public class ListTopController {
    private String myImage;

    @Init
    public void callMeWhateverYouWant() {
        // do what you need to fetch your src.
        myImage = "";
    }

    @Command
    @NotifyChange("myImage")
    public void changeSrc() {
        //This can be added to any event.
        //you can change your image or something else, but you need to notify what's changed.
        myImage="new img src";
    }

    public String getMyImage() {
        return myImage;
    }

    //setter not needed as we use only @load.
}   

First of all, if there any faults in the code just tell me and I'll correct them.
This reason for this is that I write the code life on SO, so there is always a typo possible. Now I added a button to show how you can call the command.
You see I can call it on any event I like as long they exist.
Just take attention, the command has to be called in the scope of the component where you declare the viewmodel. (but this is also for the Controller)

If there are any more questions, just make a comment and I'll clarify more.

Edit:

In order to complete the answer with the questions of the comment :

  • @bind will not help you, this is a shortcut for @load + @save.
  • You should check if your getter is called :
  • Yes => the src will be incorrect.
  • No => the NotifyChange is not there or set on something else then a command or setter. You could use BindUtils.postNotifyChange(null,null,myImage,"."); in order to tell the binder to reload the property. (getter should be called with this)
  • About the html, are you working in a zul file or some other file?( It should be zul) Native html can be added in zul with native namespace.
chillworld
  • 4,207
  • 3
  • 23
  • 50
  • Thank you so much all for your help,...the event in above is working but the value of variable myImage still not loaded into bg4 in the zul file,i've try to change src="@load(vm.myImage)" to src="@bind(vm.myImage)" but still no change, maybe the component inside tag can not to given event in java code – syam houng Apr 04 '16 at 08:18
  • thank you so much @ chillworld,all of my component image is inside tag, i see this [example](http://books.zkoss.org/wiki/ZK_Developer's_Reference/UI_Patterns/HTML_Tags/The_XHTML_Component_Set/) it seem a nice way, i will try – syam houng Apr 07 '16 at 10:59