I need to clear the warning or say error message,which will be the best one to choose
setMessage(null)
or
by using StringUtils.EMPTY - i just want to know the exact way of using StringUtils.EMPTY,it would be helpful...
Thanks & Regards.
Do not use null.
Since it's a message you might showing it some where,If you set it to null,it displays as "null" to end user.
Try like ,
private static final String EMPTY_STRING = "";
setMessage(EMPTY_STRING );
on there other hand you can check with isEmpty method,instead of checking null.
Related
lets say i have written "doSomthing()" in a text file. Does anybody know if it is possible to have that text doSomthing() without having to wirite:
if(txt.equals("doSomthing()"){
doSomthing();
}
the answer was indeed in the reflection chapter and it's straight forward. thanx for that StephaneM :
Method method = MyClass.class.getMethod("doSometing", String.Object);
Object returnValue = method.invoke(null, "parameter-value1");
I'm working in Java and trying to determine something like so:
I have a SqlParameterSource with an array named "ids" in it. I need to determine what type these ids are in, for example numeric or varchar. I specify that earlier in the code with for example:
return con.createArrayOf("varchar")
or
return con.createArrayOf("numeric")
I have tried this:
if (parametersource.getSqlType("ids") == something) {
// do something
}
else {
//do something else
}
I can't figure out how to do this. getSqlType seems to return an int but I don't know what to compare it to to get the correct comparison.
There is another method named getTypeName but I don't get how this works.
I solved it with:
parameterSource.getValue("objtype").toString();
Fetches the value from "objtype" which I set in my ParameterSource alongside my array to make my end goal easier.
Thanks friends, I love you all. :)
If you're talking about Spring SqlParameterSource,try parametersource.getTypeName("ids")which is inherited from AbstractSqlParameterSource
The call of the hashCode() method on File object , triggers this SonarQube issue :
Use the original value instead.
How to resolve? Any help please?
File file = ......
.... = Math.abs(file.hashCode());
Update: the SonarQube description of the issue is here
Can I just write : .... = Math.abs(file.hashCode()+1);
The cause of your problem is the fact that Integer.MIN returns itself when taking the absolute value. This issue is described here.
The solution depends on what you need the value for. Your suggested solution (Math.abs(file.hashCode() + 1)) won't work, because if the hashCode() function returns Integer.MAX then that plus 1 is still Integer.MIN, so the final result could still be negative.
If you really need a positive hash of the file, you could do something like this:
final int hash = file.hashCode() == Integer.MIN ? Integer.MAX : Math.abs(file.hashCode());
I am currently creating a plugin for Minecraft using the SpigotAPI. Reason I'm posting this here is because this I believe is a Java error. I am creating a duels plugin where inside my code it'll loop through an enum, and see if it's a specific type. The first time using it around it properly works, no problems. But when I try it for a second time without restarting my plugin/program/code it'll return the enum as null. Here is the code, is there a fix to this?
public DuelArenas[] getArenasWithType(DuelTypes type) {
String suffix = "_NORMAL";
List<DuelArenas> arenasAsList = new ArrayList<>();
switch (type) {
case NORMAL:
suffix = "_NORMAL";
break;
}
for (DuelArenas arena : duelArenaStatus.keySet()) {
if (arena.toString().endsWith(suffix)) {
arenasAsList.add(arena);
}
}
DuelArenas[] arenas = new DuelArenas[arenasAsList.size()];
return arenasAsList.toArray(arenas);
}
Stacktrace:
Caused by: java.lang.NullPointerException
at me.nick.acore.duels.DuelsAPI.getArenasWithType(DuelsAPI.java:97) ~[?:?]
And yes I've checked to see if the enum was null, and it was in fact null. Also line 97 is
if (arena.toString().endsWith(suffix)) {
And finally here is the DuelArena class
public enum DuelArenas {
ARENA_1_NORMAL, ARENA_2_NORMAL, ARENA_3_NORMAL, ARENA_4_NORMAL, ARENA_5_NORMAL,
ARENA_6_NORMAL, ARENA_7_NORMAL, ARENA_8_NORMAL, ARENA_9_NORMAL, ARENA_10_NORMAL,
ARENA_11_NORMAL, ARENA_12_NORMAL }
Thanks!
Your problem is that you cannot directly convert your custom DuelArenas class to a string. However when you are comparing to see if a .toString() ends with suffix, I feel that something is also going wrong. You would only ever use .toString to convert things like numbers to strings, and if your are converting a number to a string there is no way it will end in _NORMAL.
So if you want me to troubleshoot further please post your DuelArenas class, but until then my best guess is that when you do arena.toString you are looking to pull some sort of value from that class that is stored in it, and to do this you would do arena.variableInsideArenaName and work with that.
EDIT:
After seeing the class scratch that, the error in going to be somewhere in this line DuelArenas arena : duelArenaStatus.keySet()
I am running in to bit of a trouble trying to use parametric replacement.
In my properties file I have the following entry
tpi.message=This is a test message. Generated for ? on ? .
The text above is displayed on the web and in the report therefore I need to replace ? with parameters.
However, I can't use replace* method because ? is special character for regex. I also don't want to use String.format method.
I know it is possible to replace ? but I don't remember how.
Your help is appreciated.
You can do it like this:
String message = "This is a test message. Generated for ? on ?";
message = message.replaceFirst("\\?", "Bob").replaceFirst("\\?", "Tuesday");
System.out.println(message); // This is a test message. Generated for Bob on Tuesday