Having problem with TimerTask and Timer - java

brightonStores = BrightonUtil.getStoreInfo(file.getName(), dir.getAbsolutePath(),mailInfoST);
String status = "604";
if (!brightonStores.isEmpty()) {
status = brightonStoreService.saveDeleteBrightonStore(brightonStores,mailInfoST);
storeLog.info("Adding Store records Status:: "+status);
if (status.equals("600")) {
storeLog.info("File: " + file.getName() + " No of records: " + brightonStores.size() + " :: Added");
mailInfoST.setToSend(false);
//mailTimerTaskST.cancel();
} else {
storeLog.info("File: " + file.getName() + " :: Error in adding records");
writer.append("File: " + file.getName() + " :: Error in adding records"+"\n" + mailInfoST.getResult());
writer.close();
BrightonUtil.setMailInfo(mailInfoST, true, false, "File: " + file.getName() + " :: Error in adding records");
mailTimerST.schedule(mailTimerTaskST, new java.util.Date(), 1000*configurationManager.getFileUploadErrorMailTimer());
}
} else {
storeLog.info("File: " + file.getName() + " :: Error in adding records");
storeLog.info("File: " + file.getName() + " :: No record found ");
writer.append("File: " + file.getName() + " :: Error in adding records");
writer.append("\nFile: " + file.getName() + " :: No record found ");
if(null != mailInfoST.getResult()){
storeLog.info(mailInfoST.getResult());
writer.append("\n"+mailInfoST.getResult());
}
writer.close();
BrightonUtil.setMailInfo(mailInfoST, true, false, "File: " + file.getName() + " :: Error in adding records");
mailTimerST.schedule(mailTimerTaskST, new java.util.Date(), 1000*configurationManager.getFileUploadErrorMailTimer());
}
} else if(validationStatus.equals("604")){
storeLog.error("Error in Brighton Store data file :: Invalid structure or value :: "+ file.getName());
writer.append("Error in Brighton Store data file :: Invalid structure or value :: "+ file.getName() + "\n" + mailInfoST.getResult());
writer.close();
BrightonUtil.setMailInfo(mailInfoST, true, false, "Error in Brighton Store data file :: Invalid structure or value :: "+ file.getName());
mailTimerST.schedule(mailTimerTaskST, new java.util.Date(), 1000*configurationManager.getFileUploadErrorMailTimer());
} else {
storeLog.error("Error in reading data from Brighton Store file :: " + file.getName());
writer.append("Error in reading data from Brighton Store file :: " + file.getName()+ "\n" + mailInfoST.getResult());
writer.close();
BrightonUtil.setMailInfo(mailInfoST, true, false, "Error in reading data from Brighton Store file :: " + file.getName());
mailTimerST.schedule(mailTimerTaskST, new java.util.Date(), 1000*configurationManager.getFileUploadErrorMailTimer());
}
brightonStores = null;
java.lang.Runtime.getRuntime().gc();
Is it possible reshedule TimerTask in a Timer..

You can't reschedule a TimerTask within a Timer. If you want to change the schedule that the task is run on, you need to cancel the old timer and create a new one.

Related

how to remove this error (android.content.res.Resources$NotFoundException: String resource ID #0xffffffff)

I'm building an Android app that contain SQLite DB and I was having a problem with translating SQLite saved data then I find a solution which is a method to get resource id of strings, and it worked just fine, the app was working fine then suddenly this error showed I don't know why, please help
The method, I used to get resource for string:
public static int getResId(String resName, Class<?> c) {
try {
Field idField = c.getDeclaredField(resName);
return idField.getInt(idField);
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
The code in the adapter
textViewname.setText(context.getString(R.string.product_Name)+" " + repo.getName());
textViewquantity.setText(context.getString(R.string.last_added_qty)+" " + repo.getQuantity() + " " + context.getResources().getString(mdatabase.getResId(repo.getItemsType(),R.string.class)));
textViewdate.setText(context.getString(R.string.entry_date)+" " + repo.getDate()+ " /"+context.getResources().getString(mdatabase.getResId(repo.getInOrOut(),R.string.class)));
textViewTotal.setText(context.getString(R.string.total_qty)+" " + repo.getTotal() + " " + context.getResources().getString(mdatabase.getResId(repo.getItemsType(),R.string.class)));
textviewlastUpdated.setText(context.getString(R.string.last_updated_date)+" " + repo.getUpdateDate() +" /"+context.getResources().getString(mdatabase.getResId(repo.getInOrOut(),R.string.class)));
textViewid.setText(context.getString(R.string.id)+" " + repo.getId());
textViewOutputqty.setText(context.getString(R.string.last_out_qty)+" " + repo.getOutputqty() + " " + context.getResources().getString(mdatabase.getResId(repo.getItemsType(),R.string.class)));
textViewpriceofsingleitem.setText(context.getString(R.string.retail_price)+" " + repo.getPrice() + " " + context.getResources().getString(mdatabase.getResId(repo.getCurrency(),R.string.class)));
textViewpriceofMultipleitem.setText(context.getString(R.string.wholesale_price)+" " + repo.getPriceofmultipleInput() + " " + context.getResources().getString(mdatabase.getResId(repo.getCurrency(),R.string.class)));
textViewProfitSIngle.setText(context.getString(R.string.retail_profit)+" " + repo.getProfitSingle() + " " + context.getResources().getString(mdatabase.getResId(repo.getCurrency(),R.string.class)));
textViewProfitMultiple.setText(context.getString(R.string.wholesale_profit)+" " + repo.getProfitMultiple() + " " + context.getResources().getString(mdatabase.getResId(repo.getCurrency(),R.string.class)));
textViewPurchasingPrice.setText(context.getString(R.string.purchasing_price)+" " + repo.getPurchasingPricePeranItem() + " " + context.getResources().getString(mdatabase.getResId(repo.getCurrency(),R.string.class)));
textViewTotalPurchasingPrice.setText(context.getString(R.string.total_purchase_price)+" " + repo.getTotalPurchasingPrice() + " " + context.getResources().getString(mdatabase.getResId(repo.getCurrency(),R.string.class)));
textViewTotalProfitSingle.setText(context.getString(R.string.total_retail_profit)+" " + repo.getTotalProfitperSingle() + " " + context.getResources().getString(mdatabase.getResId(repo.getCurrency(),R.string.class)));
textViewTotalProfitMultiple.setText(context.getString(R.string.total_wholesale_profit)+" "+ repo.getTotalProfitPerMultiple() + " " + context.getResources().getString(mdatabase.getResId(repo.getCurrency(),R.string.class)));
textViewitemsIBox.setText(context.getString(R.string.num_unitsInBox)+" "+ repo.getItemsInBox());
textViewSinglePriceOfiteminBox.setText(context.getString(R.string.retail_price_perUnit)+ " " + repo.getSinglePriceofItemInBox()+" " +context.getResources().getString(mdatabase.getResId(repo.getCurrency(),R.string.class)));
textViewMultipriceOfItemInBox.setText(context.getString(R.string.wholesale_price_perUnit)+" "+repo.getMultiplePriceofitemInBox()+" "+context.getResources().getString(mdatabase.getResId(repo.getCurrency(),R.string.class)));
textViewtotalItemsInBoexs.setText(context.getString(R.string.total_units)+" "+ repo.getTotalitemsInboxes() +" "+ context.getResources().getString(R.string.Pieces));
This error happens because you are trying to get resource that does not exist. It is because you store memory address which allocate to your string resource and possible it will change next time. I do not know what is your scenario but why you store it already in your strings it will never lost. I can help you to build another logic only when you tell what are you trying to achieve.

How to loop writer .csv?

I want every data to be retrieved once looping will occur in the CSV writer, how do I make the loop occur?
thanks!
Data from Firebase
Output writer
*sorry, i can't display images 'cause it must have 10 reputations :(
try {
File root = new File(Environment.getExternalStorageDirectory() + "/StockCount/");
if (!root.exists()) {
root.mkdirs();
}
File myCSV = new File(root, currentDate + " DataStockCount.csv");
myCSV.createNewFile();
FileWriter writer = new FileWriter(myCSV);
writer.append("Date : " + getDate + "\n");
writer.append("Inspector : " + getInspector + "\n");
writer.append("Location : " + getLocation + "\n");
writer.append("Product Name : " + getProductName + "\n");
writer.append("Price : " + getPrice + "\n");
writer.append("Quantity : " + getAmount + "\n");
writer.append("Barcode : " + getCode + "\n");
writer.flush();
writer.close();
Toast.makeText(this, "File Saved Successfully!", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "Error : " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
Why not using an existing library like FastCSV?

Losing focus on SwingX JCOMBO BOX

I want to achieve AutoSuggest ComboBox 2 problems I am facing
1st getting an array of id and name both want the name only.
Array i am receiving
2nd losing focus on typing the text.
Please help
database Query
public SentenceList getAutoProductList() {
return new StaticSentence(s, "SELECT "
+ "P.ID, "
+ "P.REFERENCE, "
+ "P.CODE, "
+ "P.CODETYPE, "
+ "P.NAME, "
+ "P.PRICEBUY, "
+ "P.PRICESELL, "
+ "P.CATEGORY, "
+ "P.TAXCAT, "
+ "P.ATTRIBUTESET_ID, "
+ "P.STOCKCOST, "
+ "P.STOCKVOLUME, "
+ "P.IMAGE, "
+ "P.ISCOM, "
+ "P.ISSCALE, "
+ "P.ISKITCHEN, "
+ "P.PRINTKB, "
+ "P.SENDSTATUS, "
+ "P.ISSERVICE, "
+ "P.ATTRIBUTES, "
+ "P.DISPLAY, "
+ "P.ISVPRICE, "
+ "P.ISVERPATRIB, "
+ "P.TEXTTIP, "
+ "P.WARRANTY, "
+ "P.STOCKUNITS "
+ "FROM PRODUCTS P "
+ "ORDER BY NAME", null, ProductInfoExt.getSerializerRead());
}
Auto Complete JcomboBox Code
SentenceList sentProductNames = dlSales.getAutoProductList();
m_JComboProductName.setEditable(true);
ComboBoxValModel m_jProductNameModel = new ComboBoxValModel();
editor = (JTextComponent) m_JComboProductName.getEditor().getEditorComponent();
List productList = null;
try {
productList = sentProductNames.list();
} catch (Exception e) {
}
productList.add(0, null);
m_jProductNameModel = new ComboBoxValModel(productList);
m_JComboProductName.setModel(m_jProductNameModel);
AutoCompleteDecorator.decorate(m_JComboProductName);
System.out.println("product List "+ productList);
if(m_JComboProductName.getItemCount()>0){
m_JComboProductName.setSelectedIndex(0);
}

Cycle for error

I have one problem with my cycle for.
I parse data with JsonParser and after i use one cycle for print all data but i recive only the last result.
This is the code:
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Log.d("id_Persons", "id: " + json_data.getString("id_Persons")
+ ", nome: " + json_data.getString("name")
+ json_data.getString("address")
);
stringaFinale = json_data.getString("id_Persons") + " "
+ json_data.getString("name") + " "
+ json_data.getString("address");
}
return stringaFinale;
}
catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
but, with
Log.d("id_Persons", "id: " + json_data.getString("id_Persons")
+ ", nome: " + json_data.getString("name")
+ json_data.getString("address")
);
I do not understand what is the error.
This is the screen of app
You are overwriting its value in each iteration of for loop.
Change
stringaFinale = json_data.getString("id_Persons") + " " + json_data.getString("name") + " " + json_data.getString("address");
to
stringaFinale += json_data.getString("id_Persons") + " " + json_data.getString("name") + " " + json_data.getString("address");
Note: You should initialize stringaFinale with empty string like this
String stringaFinale = "";
Update: Another nicer way is to use StringBuilder like this
// declare it before the loop
StringBuilder stringaFinale = new StringBuilder(200); // use appropriate size
//Inside the loop
stringaFinale.append(json_data.getString("id_Persons") + " " + json_data.getString("name") + " " + json_data.getString("address"));
//Get the value
stringFinale.toString();

sqLite error when using update statement

I am busy using a SQLite database with a java application and after updating the database successfully I get the following error
org.sqlite.jdbc4.JDBC4PreparedStatement#4e1c6f
Below is my update code
PreparedStatement update = con.prepareStatement("UPDATE highLeaker SET " + cyc + " = "
+ dataArray[3] + " , "
+ "gasSurveyOperator" + " = "
+ dataArray[1] + " , "
+ "gasSurveySerial" + " = "
+ dataArray[2] + " , "
+ "loss" + " = "
+ dataArray[4] + " , "
+ "comment" + " = "
+ dataArray[5] + " , "
+ "cycle" + " = '"
+ cycleT + "' , "
+ "date" + " = "
+ dataArray[6]
+ " WHERE leakerID = " + dataArray[0]+";");
System.out.println(update);
update.executeUpdate();
dataRow = CSVFile.readLine(); // Read next line of data.
You printed out the object. Also, I would highly recommend using place holders instead when executing SQL queries.

Categories