This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 16 - Pages: 2 [ 1 2 | Next ] - Last Post: 15-Feb-2017 14:19 Last Post By: davidekholm
drmikey

Posts: 862
Registered: 12/22/06
Code help with a null value and out.print
Posted: 11-Feb-2017 23:52
 
  Click to reply to this thread Reply
I am having an issue with "null" being written to a data attribute in my code. Unfortunately, it breaks some javascript due to the presence of the actual word "null" rather than just being blank. (the value would normally be a hex color). Wondering if any scripting gurus can help me make the null go away!

I am getting the string for the color value like this:

String colorText = vars.get("jgalleryText");
String colorBG = vars.get("jgalleryBG");


And writing out the code like this:

  
out.print("<a href=\"" + slideLink2 + "\"><img  data-jgallery-text-color=\"" + colorText + "\" data-jgallery-bg-color=\"" + colorBG + "\"  src=\"" + thumbSRC2 + "\" alt=\"" + name + " <em>" + comments + "</em>\" title=\"" + slideLink + "\" ></a>\r\n");     


data-jgallery-text-color and data-jgallery-bg-color need to be blank unless colorText an colrBG have a value.
Thanks for any info.

Mike
jGromit

Posts: 32,924
Registered: 01/31/06
Re: Code help with a null value and out.print
Posted: 12-Feb-2017 00:06   in response to: drmikey in response to: drmikey
 
  Click to reply to this thread Reply
Rather than try to get fancy, I think I'd just beat the problem to death:
String colorText = vars.get("jgalleryText");
if(colorText == null) colorText="";
drmikey

Posts: 862
Registered: 12/22/06
Re: Code help with a null value and out.print
Posted: 12-Feb-2017 00:09   in response to: jGromit in response to: jGromit
 
  Click to reply to this thread Reply
I tried
 if(colorText == null) colorText=" ";
and got an error and then went off to try and beat it to death!!
Thanks for the easy fix.
jGromit

Posts: 32,924
Registered: 01/31/06
Re: Code help with a null value and out.print
Posted: 12-Feb-2017 00:16   in response to: drmikey in response to: drmikey
 
  Click to reply to this thread Reply
One of the joys of skin development is trying to figure out whether something is void, null, or an empty string. Fun for the whole family.
davidekholm

Posts: 21,292
Registered: 10/18/02
Re: Code help with a null value and out.print
Posted: 12-Feb-2017 09:31   in response to: jGromit in response to: jGromit
 
  Click to reply to this thread Reply
Let me remind you to use SkinModel files in your skins. With it, you're guaranteed against void and null variables. Any variable declared there and having a default value is guaranteed to never be void or null. As a bonus, you get properly typed variables and a skin that works in console mode too.

A SkinModel is a SkinModel.java class file filled with variable references and then compiled to a class file. Put it inside your skin's "plugins" folder or within a jar file within your skin's "lib" folder.

Example:
public class SkinModel {
		
	// Site tab
	public int blogStyleDepth = 1;
	public boolean useBreadcrumbPath = false;
	public boolean showTopNavigation = true;
	public boolean topNavigationPagesOnly = true;
	public String logoName;
	public boolean showBottomNavigation = false;
	
	public boolean useSearch = true;
	public String searchFields = "title,comment,name,creator,keywords";
}
If you want to name the SkinModel file something else or put it inside a package, then put the fully qualified name (package.ClassName format) as the value of a SkinModel attribute of your skin.properties file.

PS, you don't even need to compile these SkinModel.java files as jAlbum uses BeanShell to dynamically do so, but I recommend compiling them anyway.
jGromit

Posts: 32,924
Registered: 01/31/06
Re: Code help with a null value and out.print
Posted: 12-Feb-2017 13:46   in response to: davidekholm in response to: davidekholm
 
  Click to reply to this thread Reply
davidekholm wrote:
Let me remind you to use SkinModel files in your skins. With it, you're guaranteed against void and null variables.

Well yeah, except for the variables that come from the jAlbum core, for which there are no guarantees. ;)
Dschuwi

Posts: 1,521
Registered: 11/12/03
Re: Code help with a null value and out.print
Posted: 12-Feb-2017 14:28   in response to: davidekholm in response to: davidekholm
 
  Click to reply to this thread Reply
davidekholm wrote:

Example:
public class SkinModel {
		
	// Site tab
	public int blogStyleDepth = 1;
	public boolean useBreadcrumbPath = false;
	public boolean showTopNavigation = true;
	public boolean topNavigationPagesOnly = true;
	public String logoName;
	public boolean showBottomNavigation = false;
	
	public boolean useSearch = true;
	public String searchFields = "title,comment,name,creator,keywords";
}

Wouldn't logoName be null in your example, as there is not even an empty string ("") assigned to it?
davidekholm

Posts: 21,292
Registered: 10/18/02
Re: Code help with a null value and out.print
Posted: 13-Feb-2017 12:34   in response to: Dschuwi in response to: Dschuwi
 
  Click to reply to this thread Reply
Dschuwi wrote:
davidekholm wrote:

Example:
public class SkinModel {
		
	// Site tab
	public int blogStyleDepth = 1;
	public boolean useBreadcrumbPath = false;
	public boolean showTopNavigation = true;
	public boolean topNavigationPagesOnly = true;
	public String logoName;
	public boolean showBottomNavigation = false;
	
	public boolean useSearch = true;
	public String searchFields = "title,comment,name,creator,keywords";
}

Wouldn't logoName be null in your example, as there is not even an empty string ("") assigned to it?


Yes, logoName could potentially be null, but not void. It's better to assign decent defaults to all variables (I copied this sample from PhotoBlogger as it included various data types)
Dschuwi

Posts: 1,521
Registered: 11/12/03
Re: Code help with a null value and out.print
Posted: 13-Feb-2017 21:33   in response to: davidekholm in response to: davidekholm
 
  Click to reply to this thread Reply
davidekholm wrote:

PS, you don't even need to compile these SkinModel.java files as jAlbum uses BeanShell to dynamically do so, but I recommend compiling them anyway.

This did not work for me during a quick test. I named it SkinModel.java, put it in a freshly created "plugins" folder of my skin, but the test variable was not recognized during album creation.

I declare my variables at the top of init.bsh - what is the advantage of using a SkinModel?
jGromit

Posts: 32,924
Registered: 01/31/06
Re: Code help with a null value and out.print
Posted: 13-Feb-2017 22:06   in response to: Dschuwi in response to: Dschuwi
 
  Click to reply to this thread Reply
I just tried it, and got an immediate null pointer exception when trying to load the skin.

I think the advantage of having this material in a separate file (and it works just fine with a compiled UI), rather than in init.bsh, is that the core doesn't have to interpret and process all those variable sets every time you hit Make Album. From the moment you load the skin, those variables are all present and accounted for.

Edit: It also means that you can use those values in the UI. For example, let's say you have a JComboBox with four values: "North", "East", "South", and "West." You want to use "South" as the default, but you want to keep the list in the original order - NESW. You can set the value of that String to "South", and that will be selected by default when the skin is loaded, rather than the first value in the list. No need for setSelectedItem() in your UI code.
davidekholm

Posts: 21,292
Registered: 10/18/02
Re: Code help with a null value and out.print
Posted: 14-Feb-2017 10:43   in response to: Dschuwi in response to: Dschuwi
 
  Click to reply to this thread Reply
Dschuwi wrote:

This did not work for me during a quick test. I named it SkinModel.java, put it in a freshly created "plugins" folder of my skin, but the test variable was not recognized during album creation.

Please attach it so I can look at it

I declare my variables at the top of init.bsh - what is the advantage of using a SkinModel?

It's the most solid way of setting defaults for UI variables. You also get typing (ints will be ints and booleans booleans etc) and your skin will work in console mode too.
jGromit

Posts: 32,924
Registered: 01/31/06
Re: Code help with a null value and out.print
Posted: 14-Feb-2017 13:26   in response to: davidekholm in response to: davidekholm
 
  Click to reply to this thread Reply
Ignore my comment about the null pointer exception - I was doing something dopey.

But the UI doesn't seem to be picking up the entries from the SkinModel.java file. In the attached skin, several of the checkboxes should be set to true, but it appears that only one of them is.

It works if I set their default values to true in the onload.bsh as well as in SkinModel.java, but that sort of defeats the purpose.
jGromit

Posts: 32,924
Registered: 01/31/06
Re: Code help with a null value and out.print
Posted: 14-Feb-2017 14:05   in response to: jGromit in response to: jGromit
 
  Click to reply to this thread Reply
BTW, somewhat off-topic, but I still had a tool that monkeyboy created back in 2010. It creates a SkinModel.java file automatically. However, no matter what skin you select, it creates the SkinModel.java file from your default skin. And since it plants the result in the plugins folder of the currently-selected skin, that can produce some nasty surprises!
Dschuwi

Posts: 1,521
Registered: 11/12/03
Re: Code help with a null value and out.print
Posted: 14-Feb-2017 16:04   in response to: davidekholm in response to: davidekholm
 
  Click to reply to this thread Reply
davidekholm wrote:

This did not work for me during a quick test. I named it SkinModel.java, put it in a freshly created "plugins" folder of my skin, but the test variable was not recognized during album creation.

Please attach it so I can look at it


I took your code example from above, saved it as "SkinModel.java" in my skin's "plugins" folder ... Undefined Argument error:

bsh.EvalError: Sourced file: inline evaluation of: ``  out.println(logoName);    //  +++ Classic Aligned 2 debug info: 3.1.4 index.ht . . . '' : Undefined argument: logoName  : at Line: 2 : in file: inline evaluation of: ``  out.println(logoName);    //  +++ Classic Aligned 2 debug info: 3.1.4 index.ht . . . '' : ( logoName ) 
 in index.htt at line number 4
monkeyboy

Posts: 1,213
Registered: 06/21/08
Re: Code help with a null value and out.print
Posted: 14-Feb-2017 22:32   in response to: jGromit in response to: jGromit
 
  Click to reply to this thread Reply
jGromit wrote:
BTW, somewhat off-topic, but I still had a tool that monkeyboy created back in 2010. It creates a SkinModel.java file automatically. However, no matter what skin you select, it creates the SkinModel.java file from your default skin. And since it plants the result in the plugins folder of the currently-selected skin, that can produce some nasty surprises!
I'm not sure what version of the 'Create skin model file.bsh' file you have. Try the latest one attached to see if it works for you. It seems to work OK for me, creating a 'SkinModel.java' file from the currently-selected skin and placing it in the 'plugins' folder of the currently-selected skin. Fingers crossed...
For anyone wishing to try it, just pop the attached file into the 'jalbum/tools/' directory, open jAlbum, select your skin of choice and go to 'Tools -> External Tools -> Create skin model file' from jAlbum's drop-down menu at the top.
Legend
Forum admins
Helpful Answer
Correct Answer

Point your RSS reader here for a feed of the latest messages in all forums