Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Detecting a Null Array Element in JSTL  RSS feed

 
Steve Dyke
Ranch Hand
Posts: 1739
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I avoid errors when a value for mArray[2](line 6) does not exist in the following jstl code? In other words the value that is evaluated for mArray is only two values in some instances instead of three. I have tried mArray[2] != null && but I still get an error.

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65825
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you have hard-coded array indices in your code? If [0], [1] and [2] represent values that represent something, why use an array rather than a bean with properties?

One of the biggest problems that can plague a JSP is a poor choice made in data modeling.

In any case, the operator you seek is empty.
 
Steve Dyke
Ranch Hand
Posts: 1739
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Why do you have hard-coded array indices in your code? If [0], [1] and [2] represent values that represent something, why use an array rather than a bean with properties?

One of the biggest problems that can plague a JSP is a poor choice made in data modeling.

In any case, the operator you seek is empty.


Nothing I try seems to stop or redirect the progression of the code from running the following:



I have tried this but it seems to ignore it:

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65825
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's in mArray[2], and again, why is it an array in the first place?

I really think that the 1st step to solving this should be to straighten out the data model.

Also, in your post, the indentation is really wonky. It's much easier to see the structure of the JSTL when the indentation is conventional.
 
Steve Dyke
Ranch Hand
Posts: 1739
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:What's in mArray[2], and again, why is it an array in the first place?

I really think that the 1st step to solving this should be to straighten out the data model.

Also, in your post, the indentation is really wonky. It's much easier to see the structure of the JSTL when the indentation is conventional.


Here is the entire code for this page. It might explain the need for the array. As far as the indentions go when I copy from my page it is indented correctly but the copy process jumbles it up.

Line 44 is what I need to escape or bypass.

I do not understand why this will not work:



 
Stefan Evans
Bartender
Posts: 1834
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From looking at this code, you seem to have a two dimensional array of Strings.
Each string value is a '~' separated list.
The value might also have a comma in it. If it does, processing should not be performed.

I will echo Bear's sentiments about this data structure.
Also your variable names "arrayItem" and "arrayElement" can potentially lead to confusion. Is there a better name for these that might indicate what the value represents?

An alternative to ${empty mArray[2]} might be better ${fn:length(mArray) > 2}

 
Steve Dyke
Ranch Hand
Posts: 1739
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:From looking at this code, you seem to have a two dimensional array of Strings.
Each string value is a '~' separated list.
The value might also have a comma in it. If it does, processing should not be performed.

I will echo Bear's sentiments about this data structure.
Also your variable names "arrayItem" and "arrayElement" can potentially lead to confusion. Is there a better name for these that might indicate what the value represents?

An alternative to ${empty mArray[2]} might be better ${fn:length(mArray) > 2}



You discovered what I found earlier today. If string did not have a comma in it, which the string I was evaluating did not, is when it crashed. I added a condition for no comma and it works as expected.

Thanks. I will work on the nameing of the variables and learn how to do the data structure per suggestions. Just got to have time to redo it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!