I'm getting this error: The method flatMap() in the type Mono is not applicable for the arguments (( prev)->{})
public Mono<PortCall> updateByFindById(String gsisKey, PortCall portCall) {
PortCall next = portCallRepository.findById(portCall.getNextPortCall().getNextScheduleEntryKey()).toProcessor().block();
return portCallRepository.findById(gsisKey)
.switchIfEmpty(Mono
.error(new DataNotFoundException(HttpStatus.NOT_FOUND, PortCallConstants.PORT_CALL_NOT_FOUND)))
.flatMap(retrivedPortCall -> {
PortCall prev1 = portCallRepository.findById(retrivedPortCall.getPreviousPortCall()
.getPreviousScheduleEntryKey()).flatMap(prev->{
prev.setNextSiteCode("");
prev.getNextPortCall().setNextScheduleEntryKey(next.getGsisKey());
prev.getNextPortCall().setTerminalCode(next.getSiteRkstCode());
prev.getNextPortCall().setCityCode(next.getCityCode());
prev.getNextPortCall().setCityName(next.getCity());
prev.getNextPortCall().setTerminalName(next.getSiteName());
prev.getNextPortCall().setArrivalVoyage(next.getArrivalVoyageCode());
prev.getNextPortCall().setDepartureVoyage(next.getDepartureVoyageCode());
portCallRepository.save(prev);
});
The expression (( prev)->{}) signifies that you are not returning anything with your lambda implementation inside the flatmap call whereas the flatmap contract expects that ? super Optional<U> should be returned.
As Toerktumlare suggests, portCallRepository.save(prev); should return an Optional, if a concrete value is returned try using map instead or if nothing is returned, add a return statement.
public int calculateWholeAmount()
{
int wholeAmount = 0;
Dice[] amountOfDice;
amountOfDice = new Dice[getDiceAmount()];
for(int i = 0; i < amountOfDice.length ; i++)
{
int x = amountOfDice[i].wuerfeln();
wholeAmount = wholeAmount + x;
}
return wholeAmount;
}
//This method is supposed to be there to create a flexible amount of dice that i can throw. Problem is When starting the method by itself i get a nullpoint exception not sure why.
You're using amountOfDice (line 5) before it is initialized.
amountOfDice = new Dice[amountOfDice];
Initialized it before using to fix your problem. For example:
amountOfDice = new Dice[5];
Edit:
In your updated code you're calling .lenght on an array. If that array is null (when your method returns 0), it will throw a nullpointer exception. So you must make sure that .length is getting called on an array that's not null.
if(amountOfDice != null) { /*your code*/ }
In the code above I've made an example of a check how to verify if your array is not null. Inside that if-statement you can call the for-loop. Of course you can also try/catch your exception, but this is not best practice as you know where the problem appears. So just use the above code arround your forloop.
Or make sure that your method (getDiceAmount()) will never return 0 or null. But once again, if someone changes your method, you'll hit the same problem in the future again as others don't know your implementation.
I'm looking for the best way to implement a method originally from a PHP class in Ruby. The method uses PHP's "static" keyword to create a counter which can remember it's value from the last time the function was called.
function fetchRule()
{
static $index = 0;
$ret = null;
if(isset($this->rules[$index]))
{
$ret = $this->rules[$index];
}
$index = $ret == null ? 0 : $index++;
return $ret;
}
As you can see rules is an array member of the object. Every time the function is called it gets the next rule and then returns null at the end. I need to port this to ruby. There are probably a dozen ways to do it but I'm hoping to keep it simple.
Here the method in pure Ruby.
def fetch_rule
#rule_index ||= 0 # An instance variable of the object which will be set only if #rule_index is nil
ret = #rules[#rule_index] # A local variable
# Increases the index or resets it to 0 if `ret` is nil.
#rule_index = (ret.nil? ? 0 : #rule_index++)
ret # Returns the value implicitly. WARNING! Returns nil if the method reaches the end of the Array!
end
I think you have a small bug in your code above because you reset $index on every call to 0.
You should also check out TryRuby. It's a cool tutorial to start with Ruby.
I'm new to Dart and just learning the basics.
The Dart-Homepage shows following:
It turns out that Dart does indeed have a way to ask if an optional
parameter was provided when the method was called. Just use the
question mark parameter syntax.
Here is an example:
void alignDingleArm(num axis, [num rotations]) {
if (?rotations) {
// the parameter was really used
}
}
So I've wrote a simple testing script for learning:
import 'dart:html';
void main() {
String showLine(String string, {String printBefore : "Line: ", String printAfter}){
// check, if parameter was set manually:
if(?printBefore){
// check, if parameter was set to null
if(printBefore == null){
printBefore = "";
}
}
String line = printBefore + string + printAfter;
output.appendText(line);
output.appendHtml("<br />\n");
return line;
}
showLine("Hallo Welt!",printBefore: null);
}
The Dart-Editor already marks the questionmark as Error:
Multiple markers at this line
- Unexpected token '?'
- Conditions must have a static type of
'bool'
When running the script in Dartium, the JS-Console shows folloing Error:
Internal error: 'http://localhost:8081/main.dart': error: line 7 pos 8: unexpected token '?'
if(?printBefore){
^
I know, that it would be enough to check if printBefore is null, but I want to learn the language.
Does anyone know the reason for this problem?
How to check, if the parameter is set manually?
The feature existed at some point in Dart's development, but it was removed again because it caused more complication than it removed, without solving the problem that actually needed solving - forwarding of default parameters.
If you have a function foo([x = 42]) and you want a function to forward to it, bar([x]) => f(x);, then, since foo could actually tell if x is passed or not, you actually ended up writing bar([x]) => ?x ? foo(x) : foo();. That was worse than what you had to do without the ?: operator.
Ideas came up about having a bar([x]) => foo(?:x) or something which pased on x if it was present and not if it was absent (I no longer remember the actual proposed syntax), but that got complicated fast, fx converting named arguments to positional - bar({x,y}) => foo(?:x, ?:y); - what if y was provided and x was not. It was really just a bad solution for a self-inflicted problem.
So, the ?x feature was rolled back. All optional parameters have a default value which is passed if there is no matching argument in a call. If you want to forward an optional parameter, you need to know the default value of the function you are forwarding to.
For most function arguments, the declared default value is null, with an internal if (arg == null) arg = defaultValue; statement to fix it. That means that the null value can be forwarded directly without any confusion.
Some arguments have a non-null default value. It's mostly boolean arguments, but there are other cases too. I recommend using null for everything except named boolean parameters (because they are really meant to be named more than they are meant to be optional). At least unless there is a good reason not to - like ensuring that all subclasses will have the same default value for a method parameter (which may be a good reason, or not, and should be used judiciosuly).
If you have an optional parameter that can also accept null as a value ... consider whether it should really be optional, or if you just need a different function with one more argument. Or maybe you can introduce a different "missing argument" default value. Example:
abstract class C { foo([D something]); }
class _DMarker implements D { const _DMarker(); }
class _ActualC {
foo([D something = const _DMarker()]) {
if (something == const _DMarker()) {
// No argument passed, because user cannot create a _DMarker.
} else {
// Argument passed, may be null.
}
}
}
This is a big workaround, and hardly ever worth it. In general, just use null as default value, it's simpler.
I was trying something similar:
This does not work
widget.optionalStringParameter ? widget.optionalStringParameter : 'default string'
This works
widget.optionalStringParameter != null ? widget.optionalStringParameter : 'default string'
This also works
widget.optionalStringParameter ?? 'default string'
There was support for checking if an optional parameter was actually provider in early Dart days (pre 1.0) but was removed because it causes some troubles.
I have something like the following:
var lst = db.usp_GetLst(ID,Name, Type);
if (lst.Count == 0)
{
}
I get a swigly lie under lst.Count == 0 and it says:
Operator '==' cannot be applied to operands of type 'method group' and 'int'
Enumerable.Count is an extension method, not a property. This means usp_GetLst probably returns IEnumerable<T> (or some equivalent) rather than a derivative of IList<T> or ICollection<T> which you expected.
// Notice we use lst.Count() instead of lst.Count
if (lst.Count() == 0)
{
}
// However lst.Count() may walk the entire enumeration, depending on its
// implementation. Instead favor Any() when testing for the presence
// or absence of members in some enumeration.
if (!lst.Any())
{
}
sometimes addition of model on top of view is missing that time you will get same error
eg. - #model List<Test.Admin.Models>