Setting Attribute in Servlet results in null - servlets

I try setting an attribute for a session and want to assign a String to it, that is displayed later in a jsp.
I have the following code:
The System.out results in null even thought the String was set to the error message? What am I doing wrong?
error = "Something";
session.setAttribute("error", error);
System.out.println("This is get Attr: " +session.getAttribute(error));
I added the following lines of code now:
RequestDispatcher disp = req.getRequestDispatcher("error.jsp");
disp.forward(req, resp);
and in my jsp:
Hello ${error}
and it is displaying ${error} instead of the value!

You need to access the object by key:
System.out.println("This is get Attr: " +session.getAttribute("error"));
In your example, you are using the value (i.e.the object reference error)

Related

ConditionalCheckFailed in DynamoDb

While inserting the entry into the dynamodb table, I am getting the follwing error:
com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException:
The conditional request failed (Service: AmazonDynamoDBv2; Status
Code: 400; Error Code: ConditionalCheckFailedException;
This is my Java code for inserting the element:
DynamoDBSaveExpression expression = (new DynamoDBSaveExpression()).withExpectedEntry("itemKey", new ExpectedAttributeValue(false));
try {
this.mapper.save(itemKeyMapping, expression);
} catch (Exception var3) {
this.logger.error("", var3);
throw var3;
}
itemKeyMapping is an object which has a member variable as itemKey.
So, it should work. The condition is just saying that itemKey should exist, right?
Please let me know if I am doing some mistake.
My understanding is here was wrong. Actually, condition above means that insert only when itemKey attribute value in the database does not exist.

How to setvalue on onload event in Dynamics crm

I'm getting a record using fetch to client side, and I need to fill the data from the fetch into a field in a new record I create (fill field on client side), but when I use set value I get an error:
Cannot read property 'setValue' of null
I don't understand why, the field is Null because the new record is empty, why can't I fill it?
function OnLoad() {
formType = Xrm.Page.ui.getFormType();
if(formType == formTypes.Create){
copyCatNewRecord();
}
}
function copyCatNewRecord(){
var xml;
xml ="<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false' count='1'>"
xml += "<entity name='needs'>"
xml += "<attribute name='needsid' />"
xml += "<attribute name='needsname' />"
xml += "<order attribute='modifiedon' descending='true' />"
xml += "</entity></fetch>"
var ret = frameworkGlobal.ExecuteFetchRequest(xml);
var currValue = ret[0].needsid.Value;
// This line returns an error
Xrm.Page.getAttribute("needs").setValue(currValue);
}
Cannot read property 'setValue' of null
This error message is usually received when Xrm.Page.getAttribute() has been called on a field that doesn't exist. So, are you sure that "needs" is the schema name of an actual field on your form? The field should probably have a prefix such as new_ i.e. "new_needs"
The error that you are receiving indicates that the field you are trying to write data was not found. Make sure the field is present on the form and that you are using the logical name when trying to access it (see Dave Clark's answer).
In case you are trying to set the value from a Html webresource present on your form use window.parent to access the field > window.parent.Xrm.Page.getAttribute("prefix_fieldname").setValue(currValue);

cannot find symbol addActionListener()

I am a beginner in javafx.I have a function button_loop() that is supposed to dynamically generate a specified number of buttons and add an action listener for each button as it is generated.The buttons are then stored in an ArrayList and the ArrayList is then added to a HBox. The buttons are generating fine. However, when I attempt to add an action listener like so:
button.addActionListener(this)
And despite implementing the actionListener interface,I get the following error:
error: cannot find symbol
button.addActionListener(this);
symbol: method addActionListener(dummy_td)
location: variable button of type Button
The code for the function is:
void button_loop() throws SQLException, ClassNotFoundException {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection c = DriverManager.getConnection(connection, user_name, password);
PreparedStatement st = c.prepareStatement("select service_name from services");
ResultSet rs = st.executeQuery();
while (rs.next()) {
String service = rs.getString("service_name");
Button button = new Button(service);
button.addActionListener(this);
buttonlist.add(i,button);
}
hboxx.getChildren().addAll(buttonlist);
}
I've been trying to figure this out for 2 days..please help.
It's either
button.setOnAction(this);
(allows only for a single event handler added this way)
or
button.addEventHandler(ActionEvent.ACTION, this);
// event type ^^^^^^ | ^^^ handler
Note: if you get this error it may be worth checking the JavaDoc, if the member even exists.

Value passed with request.setAttribute() is not available by request.getParameter()

I give a string variable a value in the normal execution of the code ,but if an exception happen I will give it another value , the problem is that in catch block the value is still the same as i assign first .
Here is my code ,first I assign page value "addUser" inside try block and in catch I give it "ErrorPage" value , I send the value of page within http request to forword method and inside it i print the value of page.
I cause an error in the excution of the code an i want it to go through catch block , and it does , but when it send the page value to the forword function the value of page is "addUser" not "ErrorPage" although i assign it to "ErrorPage" !!
String page = "addUser";
try {
// ...
request.setAttribute("page", page);
forward(request, response);
} catch (SQLException e) {
page = "ErrorPage";
request.setAttribute("page", page);
forward(request, response);
}
and here is the forword function
String page = request.getParameter("page");
System.out.println("page is " + page); // each time it prints addUSer
Can someone help? and thanx in advance.
You're calling request.getParameter() instead of request.getAttribute() to obtain the value. Since you've set it as request attribute, you should also get it as request attribute.
So:
request.setAttribute("foo", foo);
is only available by
Object foo = request.getAttribute("foo"); // NOT getParameter().
The getParameter() is only for HTTP request parameters as you can specify in request URL or in input fields of HTML forms.
In addition to BalusC's point"
In your code you have declared two "String page" variable. This will not compile. I think the parameter that you are passing to the request in the catch must be the "other" page variable. But it is hard to tell since this is not a true example.

Avoiding a NullReferenceException

I have used this code for extracting urls from web page.But in the line of 'foreach' it is showing
Object reference not set to an instance of an object
exception. What is the problem? how can i correct that?
WebClient client = new WebClient();
string url = "http://www.google.co.in/search?hl=en&q=java&start=10&sa=N";
string source = client.DownloadString(url);
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(source);
foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[#href and #rel='nofollow']"))
{
Console.WriteLine(link.Attributes["href"].Value);
}
First, you should look up the NullReferenceException in the documentation. It says
The exception that is thrown when there is an attempt to dereference a null object reference.
This means you did the equivalent of
SomeClass reference = null;
reference.Method(); // or reference.Property;
Next, look at the line of code that has the error and figure out what you are derefencing:
foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[#href and #rel='nofollow']"))
doc.DocumentNode
doc.DocumentNode.SelectNodes
So either doc is null, or doc.DocumentNodes is null. Since you just assigned a new instance of HtmlDocument to doc, doc can't be the problem. That implies that you loaded an empty document, such that there is no doc.DocumentNode.
Check before the loop to see if doc.DocumentNode is null.
Which line is the exception being thrown from; the actual foreach line, or the contents of the loop?
Probably the easiest method to deal with this one is to use the debugger - pop a breakpoint in front of the foreach, and when the runtime pauses, inspect the contents of the various variables, such as link, doc.DocumentNode, etc. If link is non-null, then check whether link.Attributes["href"] is, and so on.

Categories

Resources