When using setString it doesn't store the variable - java

I'm trying to set a string to receive a value from other elements. I'm currently using selenium java. I don't understand why I get NullPointerException.
The method used for setString is that,
public void setString(String name, String value) {
this.configuration.setString(name, value);
}
Basically, this method set using HashMaps.
But when I use debug mode on IntelliJ
element(EXAMPLE_1).getText()
I get that:
That's the string that I want to save.
But when I do :
setString(VAR_EXAMPLE, Selenium.getInstance().getCurrentDriver()
.findElements(By.xpath(EXAMPLE_1)).getText())
I get this error
Can anyone help me out? Really appreciate :)

Can you try with findElement instead of findElements, get Text is only applicable on particular element not on all elements
.findElement(By.xpath(EXAMPLE_1)).getText())

Related

Pass String with Xpand method call as parameter

In XPAND template, I have a call where, the call should be done for the method which takes the String and the Object.
i.e. ,
«setData("String",Object)-»
For the above method, the String value I want to pass is the simplified value of below string:
where ,«getAddress(object)» , return dynamic value.
"/begin DATA XETK DEFAULT_RASTERS «getAddress(object)» /end DATA"
If I try to do this in the below way, i get compilation error.
«setData("/begin DATA XETK DEFAULT_RASTERS «getAddress(object)» /end DATA",Object)-»
Can anyone help me, how can I pass this string to the method?
Thanks ,
Regards,
Shwetha
Ok , I got the answer for the above question.
In XPAND , the expressions are not allowed to pass as parameter. But this is done in XTEND 2

GWT - CellTable/DataGrid Using ImageCell

I want to add an ImageCell in a CellTable. my code is the following:
Column<Message, String> myColumn = new Column<Message, String>(new ImageCell()) {
#Override
public String getValue(Message details) {
Image image = new Image (ClientResources.of().image1());
return image.getUrl();
}
};
It shows the following warnning when I run it:
[WARN] [adminportal] - Template with variable in URL attribute context: The template code generator cannot guarantee HTML-safety of the template -- please inspect manually or use SafeUri to specify arguments in a URL attribute context
My question is that how I can return the url as string value safely. I saw an example about using imagecell before and the return value is string. I cannot find it now. Could anyone tell me how to fix this.
Note: I just want to know how to fix this if I want to use imagecell in celltable and return value is string. I know how to use imageresourcecell to achieve the same goal. Also, I know I can change the type of getValue() from string to safeHtml to achieve the goal. But i am really wondering how to achieve this by using imagecell and string type of getValue() because I saw an example about this before and I tried it successfully. Just cannot remember what I did wrong here.
There's no way to fix this (remove the warning): using a String as part of a template is inherently unsafe, and GWT warns you about it. If you're absolutely certain of the safety of your values, then you can safely ignore the warnings, but they'll still be emitted.
The only way to not have those warnings is to use a SafeUri, i.e. use the SafeImagecell. It's then up to you to guarantee the safety of your URL, depending on the method you use to construct the SafeUri (have a look at UriUtils)

Passing Jackjson JSON object from JSP to JavaScript function

I have a JSON String stored in a database. In one of my JSP pages, I retrieve this string, and I want to be able to pass the String or the JSON object into Javascript function. The function is simply this for test purposes
function test(h){
alert(h);
}
Now I can retrieve the JSON string from the database fine, I have printed it out to the screen to ensure that it is getting it, however when I pass it in like this
<input type="button"
name="setFontButton"
value="Set"
class="form_btn_primary"
onclick="test('<%=theJSON%>'); return false;"/>
Nothing happens. I used firebug to check what was wrong, and it says there is invalid character.
So I then tried passing in the JSON object like so
Widget widg = mapper.readValue(testing.get(0), Widget.class);
Then pass in it
onclick="test('<%=widg%>'); return false;"/>
Now this will pass in without an error, and it alerts the object name, however I am unable to parse it. Object comes in like with the package name of where the widget class is stored like so
com.package.mode.Widget#ba8af9
I tried using Stringify, but that doesn't seem to work on this Jackson JSON object.
After all that failed, I tried a last resort of taking the String from the database, and encoding it in base64. However, this too fails if I do this
String test = Base64.encode(theString);
and pass that in. However if I do that, print it out to the screen, then copy what is printed out, and send that through it works, so don't quite understand why that is.
So could someone please tell me what I am doing wrong. I have tried soo many different solutions and nothing is working.
The JSON String is stored in database like this
{
"id":1,
"splits":[
{
"texts":[
{
"value":"Test",
"locationX":3,
"locationY":-153,
"font":{
"type":"Normal",
"size":"Medium",
"bold":false,
"colour":"5a5a5a",
"italics":false
}
}
]
}
]
}
Would be very grateful if someone could point me in the direct direction!!
Edit:
Incase anyone else has same problem do this to pass the JSON from JSP to the JS function
<%=theJSON.replaceAll("\"", "\\\'")%>
That allows you to pass the JSON in,
then to get it back in JavaScript to normal JSON format
theJSON = theJSON.replace(/'/g,'"');
Should work fine
I think the combination of double quotes wrapping the onclick and the ones in your JSON may be messing you up. Think of it as if you entered the JSON manually -- it would look like this:
onclick="test('{ "id":1, "splits":[ { "texts":[ { "value":"Test", "locationX":3, "locationY":-153, "font":{ "type":"Normal", "size":"Medium", "bold":false, "colour":"5a5a5a", "italics":false } } ] } ] }'); return false;"
and the opening double quote before id would actually be closing the double quote following onclick= (You should be able to verify this by looking at the page source). Try specifying the onclick as:
onclick='test(\'<%=theJSON%>\'); return false;'
You can follow the following steps
Fetch the jon string
Using the jackson or any other JSON jar file , convert the json string to json array and print the string using out.println.
Call this jsp which prints the json string
check in the firebug , you will be able to see your json .
If the Json string does not print , there can be some problems in your json format.
this is a good website for json beautification , http://jsbeautifier.org/ , really makes the string simple to read .
Thanks
Abhi

How to get value from HSSFComments in apache POI

I want to read/extract the value from HSSFComment.
I can access the HSSFComment by the following code:
HSSFComment comment = workSheet.getCellComment(1, 0);
But, how can I get the text/value from that "comment" instance?
there are tow methods in HSSFComment:
getTextObjectRecord()
getNoteRecord()
But both are protected methods...that's why I can't access those from my class. in other word, these methods are not visible from my class. Following line of code doesn't compile.
TextObjectRecord txo = comment.getTextObjectRecord();
Any comments?
Use getString() inherited from HSSFTextBox. This returns an HSSFRichTextString, which itself has a getString() method to get the plain text. In otherwords
String comment = cell.getComment().getString().getString();
Which you can't do like that due to the possibility of null returns, but that's the idea.

Can't get localized strings from xml files correctly

I've been racking my head with this...
I've got a localized strings.xml file in a values-en folder with this example string:
#string/my_string
The string has the following text stored in English: "My String"
When accessing the localized string via a layout, it works fine.
When I try to change it in code, that's where I get problems.
I store the string into an array of strings for later use. The 'context' is passed from my activity to a data class and used with this line of code:
dataStrings = new String[] { (String) context.getResources().getString(R.string.my_string) };
Later, I try to display this string, like so:
buttons[0].setText(dataStrings[0]);
It displays:
#string/my_string
How do I get it to display the string without '#string/', the proper localized string?
You can run getString() directly on the Context object; you don't need to run getResources(). However, this should do the same thing as you're currently doing so I don't think that's the source of your problem.
The first thing to confirm is that what you think is happening is happening. Either use the debugger to check that buttons[0] contains "#string/my_string" or try calling setText() with a hard-coded value to make sure the text is actually being updated on the correct button - e.g. buttons[0].setText("StackOverflow!");

Categories

Resources