variable value in class is not getting loaded properly - java

Below is my code snippet,this piece of code will be called from a workitem with the needed input parameters.The problem which Iam facing is the value of ExcpStatus variable is not getting loaded properly,my workitems will fail sometime and the value of finalOpStatus will be fail making the value of ExcpStatus to YES.Now second time if i call this code (after ExcpStatus is changed to YES) the value of ExcpStatus should be reloaded as NO but it takes the previous changed value YES...Can someone help me in this....
public abstract class SampleWorkItemHandler implements WorkItemHandler {
public SampleWorkItemHandler()
{
this.sb_serviceTaskName="default";
}
public SampleWorkItemHandler(String taskName)
{
this.sb_serviceTaskName=taskName;
}
protected String ExcpStatus="NO";
protected String loggerCorrelationId = "";
private String startLoggerInsertStr="";
public void executeWorkItem(WorkItem workitem, WorkItemManager manager) {
try{
logger.info("the exception staus is::"+ExcpStatus);
logger.info("the loggerCorrelationId is::"+loggerCorrelationId);
String finalOpStatus=SyntBotsBPMOutputValidatorUtil.validateResponse(handlerResponseParams);
if(finalOpStatus.equalsIgnoreCase("Fail"))
{
ExcpStatus="YES";
}
}catch(Exception e){
System.out.println("Exception Stack Trace:::"+e.getMessage());
}
}

Related

How to wait for a JavaFX Service to finish before returning data in caller?

Working on my first Java project I can't seem to get around this probably basic problem: In a JavaFX application I have a DAO class, which starts a service to get values from a mysql db, builds an object from it and returns the object to the caller. But the object never gets build, because the return happens before the service has succeeded.
public IQA getQA(int id) throws SQLException {
try {
GetQuizService getQuizService = new GetQuizService();
getQuizService.restart();
getQuizService.setId(id);
getQuizService.setOnSucceeded(e -> {
this.quiz = getQuizService.getValue();
});
} catch (Exception e) {
System.err.println(e);
}
return quiz;
}
The service works fine, inside the onSucceeded action the object is present, but how can I make the return wait until the service has finished?
As requested here's a minimal version of the GetQuizService
public class GetQuizService extends Service<Quiz> {
private int id;
private Quiz quiz;
public void setId(int id) {
this.id = id;
}
#Override
protected Task<Quiz> createTask() {
return new Task<Quiz>() {
#Override
protected Quiz call() throws Exception {
// Severall calls to db here, Quiz object gets constructed
return quiz;
}
};
}
}
The problem in your code is, that you service methods are executed asynchronously.
You should return the Task<Quiz> instead of quiz and use that to update your frontend if the result is received (I have to few information to create an appropriate example for you).
Another option is to pass a callback to your service, which is invoked when the result is received instead of returning the quiz.
public void getQA(int id, QuizReceiver callback) throws SQLException {
try {
GetQuizService getQuizService = new GetQuizService();
getQuizService.restart();
getQuizService.setId(id);
getQuizService.setOnSucceeded(e -> {
callback.quizReceived(getQuizService.getValue());
});
} catch (Exception e) {
System.err.println(e);
}
return quiz;
}
public interface OuizReceiver {
void quizReceived(IQA quiz);
}

how to check value of variable has changed or not inside while loop without comparing inside java

i am fetching value from data base which is overriding the old value , how could in know when the variable value has changed.
while(result.next())
{
String rteCd = result.getString("Rte_Cd")
}
every time the rteCd will get get overridden with the database value, i want to check at which point it has changed becoz it can be same also, i need to perform some action when it changes
Npte:- i cant change the database and the query can return same value multiple times
Java.util.Observable might be of help.
The java.util.Observable.hasChanged() method returns if this object has changed.
Here's a sample code you can check out
import java.util.Observable;
import java.util.Observer;
class ObservedObject extends Observable {
private String watchedValue;
public ObservedObject(String value) {
watchedValue = value;
}
public void setValue(String value) {
// if value has changed notify observers
if(!watchedValue.equals(value)) {
watchedValue = value;
// mark as value changed
setChanged();
}
}
}
public class ObservableDemo implements Observer {
public String name;
public ObservableDemo(String name) {
this.name = name;
}
public static void main(String[] args) {
// create watched and watcher objects
ObservedObject watched = new ObservedObject("Original Value");
// watcher object listens to object change
ObservableDemo watcher = new ObservableDemo("Watcher");
// add observer to the watched object
watched.addObserver(watcher);
// trigger value change
System.out.println("setValue method called...");
watched.setValue("New Value");
// check if value has changed
if(watched.hasChanged())
System.out.println("Value changed");
else
System.out.println("Value not changed");
}
public void update(Observable obj, Object arg) {
System.out.println("Update called");
}
}
Output
setValue method called...
Value changed
Declare two more variable outside the while loop and then see the old and new value like this
String oldvalue="";
String newvalue="";
while(result.next())
{
oldvalue=rteCd;
String rteCd = result.getString("Rte_Cd")
newvalue=rteCd;
}
//Now Display them
println("old value"+oldvalue);
println("new value"+newvalue);
String prevRteCd = null;
String rteCd = null;
while(result.next())
{
prevRteCd = rteCd;
rteCd = result.getString("Rte_Cd");
if(prevRteCd! =null && !prevRteCd.equals(rteCd))
{
itChanged(prevRteCd, rteCd);
}
}

Getting java.lang.nullpointerexception when passing argument to private class of other class [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
I have a Main class:
public class Retrigger {
public static void main(String[] args){
Long i= 97944605L;
com.armus.flow.Implement rdf = new com.armus.flow.Implement();
try {
rdf.retrfail(i);
}
catch(Throwable e){
System.out.println("In exception a = "+e+" "+i);
e.printStackTrace();
return;
}
}
}
I am calling method retrfail of the Implement class and passing a long value:
import com.armus.common.Dsessionservice;
public class Implement
extends Remote
implements DMSer, Ajaxser {
private Dsessionservice flowservice;
private Dsession getDsession(long sessionId)
throws ServiceException {
try {
dss = this.flowservice.getprocessname(Long.valueOf(sessionId));
}
catch (ServerException e) {
//some code
}
//some code
}
public void retrfail(long sessionId) {
Dsession dss = getDsession(sessionId);
// some code
}
}
The implementing class passes the id to other Dsessionservice interface to get the process name.
public abstract interface Dsessionservice
{
public abstract Dsessionservice getprocessname(Long paramLong)
throws ServerException;
}
The program compiles fine. But I am getting java.lang.nullpointerexception when running the program at the below line
dss = this.flowservice.getprocessname(Long.valueOf(sessionId));
What am I doing wrong here.
Can someone please help?
You forgot to initialize your flowserive variable.
In Java, when you declare a variable like private Dsession flowserive; it is not initialized to anything, and therefore has no member method getprocessname(...) Trying to access this method when it does not exist throws a java.lang.nullpointerexception.
Try something like this :
import com.armus.common.Dsession;
public class Implement
extends Remote
implements DMSer, Ajaxser
{
private Dsession flowserive;
// ADDING CONSTRUCTOR HERE ////
public Implement() {
this.flowservice = new Dsession(); // Or initialize with any parameters you need
}
///////////////////////////////
private Dsession getDsession(long sessionId)
throws ServiceException
{
try
{
dss = this.flowserive.getprocessname(Long.valueOf(sessionId));
}
catch (ServerException e)
{
//some code
}
//some code
public void retrfail(long sessionId)
{
Dsession dss = getDsession(sessionId);
// some code
}
}
I'm sorry I do not know your Dsession class, so maybe you need to change this to initialize the Dsession object correctly...

Error in deserializing body of reply message for a web service operation

Error is obviously after the remote procedure/method has been executed. It's most likely that the remote service is sending back the incorrect data or so I think. I have tried increasing the readQuote in Web.Config, that didn't help.
Response Class
When this property (MultiSMSPostedList) is set, the value is NULL. As soon as the code comes out of get, the exception is thrown.
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.5485")]
[System.SerializableAttribute()]
//[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.SoapTypeAttribute(Namespace="urn")]
public partial class MessagingServices_SendMultiSMSResult : object, System.ComponentModel.INotifyPropertyChanged {
private int sMSCountField;
private MessagingServices_SendMultiSMS_SendSuccess[] multiSMSPostedListField;
private MessagingServices_SendMultiSMS_SendFailed[] multiSMSRejectedListField;
/// <remarks/>
public int SMSCount {
get {
return this.sMSCountField;
}
set {
this.sMSCountField = value;
this.RaisePropertyChanged("SMSCount");
}
}
/// <remarks/>
[System.Xml.Serialization.SoapElementAttribute(IsNullable=true)]
public MessagingServices_SendMultiSMS_SendSuccess[] MultiSMSPostedList {
get {
return this.multiSMSPostedListField;
}
set {
this.multiSMSPostedListField = value;
this.RaisePropertyChanged("MultiSMSPostedList");
}
}
/// <remarks/>
[System.Xml.Serialization.SoapElementAttribute(IsNullable=true)]
public MessagingServices_SendMultiSMS_SendFailed[] MultiSMSRejectedList {
get {
return this.multiSMSRejectedListField;
}
set {
this.multiSMSRejectedListField = value;
this.RaisePropertyChanged("MultiSMSRejectedList");
}
}
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertyName) {
System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
if ((propertyChanged != null)) {
propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
}
Exception
The exception text is below, it clear that exception is caused when system tries to convert variable to array.
http://justpaste.it/sms-exception
More information
Further digging shows that the remote service is returning the correct number of values, if not the format.
This property MultiSMSPostedList makes call to below class based on the number of values passed to the original procedure/method. (i.e. if I pass two phone numbers and two text messages, the properties inside MessagingServices_SendMultiSMS_SendSuccess are initialized twice, indicating remote server returned an array)
Property class
public partial class MessagingServices_SendMultiSMS_SendSuccess : object, System.ComponentModel.INotifyPropertyChanged {
private string gRecipientNameField;
private string gRecipientMSISDNwithCCField;
private int gOutMsgIDField;
/// <remarks/>
[System.Xml.Serialization.SoapElementAttribute(IsNullable=true)]
public string gRecipientName {
get {
return this.gRecipientNameField;
}
set {
this.gRecipientNameField = value;
this.RaisePropertyChanged("gRecipientName");
}
}
/// <remarks/>
[System.Xml.Serialization.SoapElementAttribute(IsNullable=true)]
public string gRecipientMSISDNwithCC {
get {
return this.gRecipientMSISDNwithCCField;
}
set {
this.gRecipientMSISDNwithCCField = value;
this.RaisePropertyChanged("gRecipientMSISDNwithCC");
}
}
/// <remarks/>
public int gOutMsgID {
get {
return this.gOutMsgIDField;
}
set {
this.gOutMsgIDField = value;
this.RaisePropertyChanged("gOutMsgID");
}
}
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertyName) {
System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
if ((propertyChanged != null)) {
propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
}
I am sure, visual studio is not able to generate the class correctly based on the WSDL data, I may need to make some changes to reference.cs file. I am not an expert on the subject, so not really sure what and where should I make the change?
Could be useful
Remote service is java based, axis web service.
MORE INFO: Changes made to reference.cs
I made below changes to Response Class and it's working without any exception. Since it's a variable instead of array, I get only first response and all the other response values are lost.
private MessagingServices_SendMultiSMS_SendSuccess[] multiSMSPostedListField;
private MessagingServices_SendMultiSMS_SendFailed[] multiSMSRejectedListField;
I am not sure where exactly in the reference.cs I can find the location, where remote call is made/ends, may be making some changes there could help. Any suggestions?

MySQL error store boolean value into tinyint

Class Code
this is some of code from Class Code that may help.
private ResultSet rs;
private Connection cn;
private Statement st;
public void insertData(String data)
{
try
{
st.executeUpdate(data);
{
JOptionPane.showMessageDialog(null, "Data berhasil Disimpan");
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, "Gagal Insert Data");
}
}
InsertDaftar Class
public class InsertDaftar implements DaftarInterface {
public String nama;
public boolean kuasa;
Code cd = new Code();
public void setNama(String nama){
this.nama=nama;
}
public void setKuasa(Boolean kuasa){
this.kuasa=kuasa;
}
public void Akun(){
String data = "INSERT INTO akun (Nama,Kuasa)"+"values('"+this.nama+"','"+this.kuasa+"')";
cd.insertData(data);
}
I have created some code boolean for radio button.
boolean akun_kuasa;
if (admin.isSelected()){
akun_kuasa=true;
}
if (teller.isSelected()){
akun_kuasa=false;
}
//todo
InsertDaftar id = new InsertDaftar()
id.setNama(akun_nama.getText());
id.setKuasa(akun_kuasa);
there are warning message on
id.setKuasa(akun_kuasa);
Warning in Netbeans.
Initialize variable of akun_kuasa
I have tried to change type of "akun_kuasa" into int,
and changed akun_kuasa into 0 and 1, but there still error.
I have searched this problem. but there are to many about BOOLEAN or TinyInt.
NOTE: id is an object that have a method to store into database.
akun_kuasa can be undefined after the if's
Set it to false first? Only u would know
It is a good compiler warning to u
akun_kuasa is declared but potentially undefined
when u call the setter

Categories

Resources