I am working on a project. When I set a String I'd like the setting method to test for a null value. If there is a null value I'd like set the global variable to "purple hotdog". I get an error that says Type mismatch: cannot convert from String to boolean and I'm not sure why. Eventually I'd like to call a method that returns a value that encryptedBlock is set to instead of setting the value to "purple hotdog", but baby steps for now. Here is my code, and thanks for the help.
private String encryptedBlock = null;
public void setEncryptedBlock(String encryptedBlock) {
if (this.encryptedBlock.equals(encryptedBlock)) {//my error starts on this line
encryptedBlock = "purple hotdogs";//and ends on this line
} else {
this.encryptedBlock = encryptedBlock;
}
}
here is proper code:
private String encryptedBlock = null;
public void setEncryptedBlock(String encryptedBlock) {
if (encryptedBlock == null)
encryptedBlock = "purple hotdogs";
this.encryptedBlock = encryptedBlock;
}
Related
#PostMapping("/order")
public Bill addOrder(#RequestBody Bill theBill) {
theBill.setDate(timestamp = new Timestamp(System.currentTimeMillis()));
billService.save(theBill);
List<BillDetails> billDetails1 = theBill.getBillDetails();
for(BillDetails billDetails2:billDetails1)
{
Product product = billDetails2.getProduct();
Location location = billDetails2.getLocation();
Quality quality = billDetails2.getQuality();
double quantity1 = billDetails2.getQuantity();
LocationDetails locationDetails1 = locationDetailsService.findById(new LocationDetailsId(product,quality,location));
if(locationDetails1 == null){
LocationDetails locationDetails2 = new LocationDetails();
LocationDetailsId locationDetailsId = new LocationDetailsId();
locationDetailsId.setProduct(product);
locationDetailsId.setLocation(location);
locationDetailsId.setQuality(quality);
locationDetails2.setQuantity(quantity1);
locationDetails2.setLocationDetailsId(locationDetailsId);
locationDetailsService.save(locationDetails2);
}
else {
double quantity2 = locationDetails1.getQuantity();
double quantity3 = quantity2 + quantity1;
LocationDetails locationDetails3 = new LocationDetails();
locationDetails3.setLocationDetailsId(locationDetails1.getLocationDetailsId());
locationDetails3.setQuantity(quantity3);
locationDetailsService.save(locationDetails3);
}
}
return theBill;
}
Hi I'm new to springboot, here my app runs fine when I have values for locationDetails1 . but it stops running when locationDetails1 returns null value . The Error I'm getting is (did not found). I want to handle that in if clause which I did .But still I receive the error. how to get rid of this?
Try to use ! operator like this...
if(!locationDetails1) ....
Because you are setting locationDetails1 in locationDetailsService.
locationDetailsService.save(locationDetails1);
You should be setting locationDetails2.
You are trying to save a null object - locationDetails1 - to the database in the end.
locationDetailsService.save(locationDetails1);
This question already has answers here:
If statement gives condition is always true
(4 answers)
Closed 2 years ago.
I'm totally new to Java programming and I'm trying to create a Java FX project. I've followed tutorials about the date validation method but it seems to fail.In this certain part I have to make a list with objects inserted by a user in text fields. That includes a date but it needs to be valid.
Below in this piece of code, the object I need to get validated is datep . I've created a method in which if the string is valid, it should set my flag to true and return it. Before the list is created I inserted an if statement to check whether that my flag is set to true which means that the date is verified according to the format.When I run it,it creates the list whatsoever even if the date is invalid.Am I putting the if statement in the wrong part? Cause I think the method is fine.
#Override
public void handle(MouseEvent event) {
if (event.getSource() == NewrentBtn) {
String vehiclen =OximaTxT.getText();
String clientn = ClientTxT.getText();
String store = StoreTxT.getText();
String storer = StorerTxT.getText();
String timerp = TimeTxT.getText();
String timer = TimerTxT.getText();
String datep = DateTxT.getText(); // <-------------
String dater = DaterTxT.getText();
Integer sum = Integer.parseInt(SumTxT.getText());
if(flag = true) { // <------------
createRental(id, vehiclen, store, datep, timerp, clientn, storer, dater, timer, sum);
clearTextFields();
}
}
public boolean Checkdate(String datep) { // <-------------
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date BOD = null;
df.setLenient(false);
try
{
BOD = df.parse(datep); // <----------------
flag = true;
}
catch(Exception e)
{
flag = false;
}
return flag;
}
public void createRental(int id,String vehiclen,String store,String datep,String timerp,String clientn,String storer,String dater,String timer,int sum ) {
Rental m = new Rental(id,vehiclen,store,datep,timerp,clientn,storer,dater,timer,sum);
RentalList.add(m);
rentalTableView.getItems().add(m);
}
From the looks of what you are trying to achieve here is my suggestion to modify the code.
First of all let me explain to you two issues i found: the first one is that you are missing the call to the validation method of the Date, that is the call to the CheckDate(datep) when you receive the text input and store the flag variable, or so it seems as we dont have the full code (which is ok ); and second you are missing a =in the if(flag = true), it should be if(flag == true)
So here is the full code:
#Override
public void handle(MouseEvent event) {
if (event.getSource() == NewrentBtn) {
String vehiclen =OximaTxT.getText();
String clientn = ClientTxT.getText();
String store = StoreTxT.getText();
String storer = StorerTxT.getText();
String timerp = TimeTxT.getText();
String timer = TimerTxT.getText();
String dater = DaterTxT.getText();
Integer sum = Integer.parseInt(SumTxT.getText());
String datep = DateTxT.getText();
boolean flag = Checkdate(datep);
if(flag == true) {
createRental(id,vehiclen,store,datep,timerp,clientn,storer,dater,timer,sum);
clearTextFields();
}
}
}
This way you are verifying if the date is correctly formatted and continue the process if it is according to your scheme.
Finally i have three recommendations as you are new to java programming:
For all methods the first letter should always be in lowercase like public boolean checkDate() this way you can differentiate a method from a Class, which will always start in Uppercase like public class Product. The only exception for this is the constructor of a class.
You should never mix the graphical interface logic, with the logical processing logic. This is: you should keep the processing part in one package and the graphic component in another and relate both of them by creating an instance of the processing logic in the graphical interface.
The user input validation should be directly made in the handler method with try-catch clauses like the following.
Here:
public void handle(MouseEvent event) {
if (event.getSource() == NewrentBtn) {
String vehiclen =OximaTxT.getText();
String clientn = ClientTxT.getText();
String store = StoreTxT.getText();
String storer = StorerTxT.getText();
String timerp = TimeTxT.getText();
String timer = TimerTxT.getText();
String dater = DaterTxT.getText();
Integer sum = Integer.parseInt(SumTxT.getText());
try {
String datep = DateTxT.getText();
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
df.parse(date);
createRental(id,vehiclen,store,datep,timerp,clientn,storer,dater,timer,sum);
clearTextFields();
} catch (ParseException e) {
/* Here you handle what happens when if fails, you can create a JDialog to show
the error or create an alert, whatever you need */
e.printStackTrace();
}
}
}
And voila a cleaner version
This is my function which return null always.. But i want the output as RECY0 for the first time and then second time RECY1 an so on...
public String generateRecycleCode() {
Integer recycleNoIncremnt=0;
String code= null;
for(MaterialRecycleIssueBean materialRecycleIssueBean : materialRecycleIssueBeanlist){
materialRecycleIssueBean.recyleCode = "RECY"+recycleNoIncremnt++;
code = materialRecycleIssueBean.recyleCode;
}
return code;
}
I am getting the above error when I am trying to set a flag and returning the value of the flag.I am writing the code below:
public static long insertEpaymentResDtls(PortalEpaymentResVO portalEpaymentResVO) {
// TODO Auto-generated method stub
String flag = null;
SppPaymentRequestKiosk sppPaymentRequestKiosk = new SppPaymentRequestKiosk();
SppUserEformData sppUserEformData = new SppUserEformData();
sppUserEformData = genericDao.findById(SppUserEformData.class, SppUserEformDataId.class,
new SppUserEformDataId(portalEpaymentResVO.getMtrxId(), portalEpaymentResVO.getOxitrxId(), Integer.parseInt(portalEpaymentResVO.getTrxStatus())));
try
{
if (sppPaymentRequestKiosk != null)
{
sppPaymentRequestKiosk.setStatus(portalEpaymentResVO.getTrxStatus());
}
sppPaymentRequestKiosk = genericDao.save(sppPaymentRequestKiosk);
if (sppPaymentRequestKiosk != null)
{
flag = "Success";
}
}
catch (Exception e)
{
GLOGGER.error("Exceptin occured at the time of updateStatus in EformServiceImpl." + e.getMessage());
}
return flag;//ERROR IN THIS LINE
}
This is the code fragment.Please help.
Your method signature states it returns primitive long.
Since you are returning String flag, you must change your return type to String.
As in:
public static String insertEpaymentResDtls(etc...
Change you method return type to String, how can you decalre long return and return String flag
public static String insertEpaymentResDtls(PortalEpaymentResVO portalEpaymentResVO) {
.
.
.
}
Your function is supposed to return a long, but you're returning a String instead.
Either change the return type to String, or define numeric values for whatever you want to return (i.e. success) and declare "flag" as a long instead of String.
You can also type, say: flag = "1" instead of flag = "success"
and return Long.parseLong(flag);
I already saw a lot of similar posts, but none seems to be the same as this. I am testing if a string is null in my Java Android (2.2) app and whatever the string is, it is always true. Here is the code :
public static String getLocalBluetoothName(){
String name;
if(mBluetoothAdapter == null){
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
}
try{
name = mBluetoothAdapter.getName();
if(name == null){
System.out.println("Name is null!");
name = mBluetoothAdapter.getAddress();
}
return name;
}
catch (Exception e){
return "";
}
}
The if(name == null) is always true even if my string has a value. By the way, I also tried mBluetoothAdapter.getName() == null and it is always true too. I saw somewhere that you can do something like that:
if(name.equals("null")){
}
But if the string is null, wouldn't that create an exception because I should not be able to use a method if the object is null? Also, testing "null" is somewhat strange to me...
Try this simplified version :
public static String getLocalBluetoothName(){
String name = null;
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter == null) {
//System.out.println("Can't get adapter");
Log.d(TAG, "Can't get adapter");
return name;
}
if ((name = adapter.getName()) == null) {
//System.out.println("Name is null!");
Log.d(TAG, "Name is null!");
name = adapter.getAddress();
}
return name;
}
and don't forget to include android.permission.BLUETOOTH permission in your app's manifest.
Also, note that sometimes your debugger may trick you by showing executing specific branches that are not in fact run (happened to me debugging in Eclipse before). So, make sure that you ACTUALLY have Name is null output in logcat, otherwise your name may be not null.
name = mBluetoothAdapter.getName();
Since name is null, your Bluetooth adapter probably doesn't have a name.
According to me, mBluetoothAdapter.getName() is always returning null, that is why the if condition is always returning true. Your method of comparing if(name == null) is absolutely correct, no doubt in this.