Unknown NullPointerException when creating simple player - nullpointerexception

I am trying to write a simple player but on running this code, it throws a NullPointerException.
try {
player = Manager.createPlayer("C:\\Users\\Admin\\Desktop\\Movie_0001.3gp");
player.realize();
VideoControl vc;
vc=(VideoControl) player.getControl("VideoControl");
if(vc!=null){
Item video=(Item) vc.initDisplayMode(vc.USE_GUI_PRIMITIVE, null);
playerFrm=new Form("playing video");
playerFrm.append(video);
display=display.getDisplay(this);
display.setCurrent(playerFrm);
}
player.prefetch();
player.start();
}
catch(IOException ex) {
ex.printStackTrace();
} catch (MediaException ex) {
ex.printStackTrace();
}

display=display.getDisplay seems odd! Was display initialized before and are you overwriting it with a function from its own?
getDisplay probably should be some sort of static function

you need to change in code Where you get cureent display,
display=Display.getDisplay(this);

Related

Getting a warning when use objectmapper in flux inappropriate blocking method call in java reactor

i am new to reactor, i tried to create a flux from Iterable. then i want to convert my object into string by using object mapper. then the ide warns a message like this in this part of the code new ObjectMapper().writeValueAsString(event). the message Inappropriate blocking method call. there is no compile error. could you suggest a solution.
Flux.fromIterable(Arrays.asList(new Event(), new Event()))
.flatMap(event -> {
try {
return Mono.just(new ObjectMapper().writeValueAsString(event));
} catch (JsonProcessingException e) {
return Mono.error(e);
}
})
.subscribe(jsonStrin -> {
System.out.println("jsonStrin = " + jsonStrin);
});
I will give you an answer, but I don't pretty sure this is what you want. it seems like block the thread. so then you can't get the exact benefits of reactive if you block the thread. that's why the IDE warns you. you can create the mono with monoSink. like below.
AtomicReference<ObjectMapper> objectMapper = new AtomicReference<>(new ObjectMapper());
Flux.fromIterable(Arrays.asList(new Event(), new Event()))
.flatMap(event -> {
return Mono.create(monoSink -> {
try {
monoSink.success(objectMapper .writeValueAsString(event));
} catch (JsonProcessingException e) {
monoSink.error(e);
}
});
})
.cast(String.class) // this cast will help you to axact data type that you want to continue the pipeline
.subscribe(jsonString -> {
System.out.println("jsonString = " + jsonString);
});
please try out this method and check that error will be gone.
it doesn't matter if objectMapper is be a normal java object as you did. (if you don't change). it is not necessary for your case.
You need to do it like this:
Flux.fromIterable(Arrays.asList(new Event(), new Event()))
.flatMap(event -> {
try {
return Mono.just(new ObjectMapper().writeValueAsString(event));
} catch (JsonProcessingException e) {
return Mono.error(e);
}
})
.subscribe(jsonStrin -> {
System.out.println("jsonStrin = " + jsonStrin);
});

I have two methods with ActionResult return types yet one of them is giving back a single Employee object and still works. Why is that?

I've just wrote a controller class in a Blazor application and I don't undersand something.
I have a method GetEmployee:
[HttpGet("{employeeId:int}")]
public async Task<ActionResult<Employee>> GetEmployee(int employeeId)
{
try
{
var result = await employeeRepsitory.GetEmployee(employeeId);
if (result == null)
{
NotFound();
}
return result;
}
catch (Exception)
{
return StatusCode(StatusCodes.Status500InternalServerError,
"Error retrieving data from the database");
}
}
The return type is an ActionResult<Employee> but clearly in my method I'm returning an Employee. Because the employeeRepsitory.GetEmployee(employeeId) method gives back a single Employee which is stored in a variable "result".
I have an other method GetEmployees :
[HttpGet]
public async Task<ActionResult<IEnumerable<Employee>>> GetEmployees()
{
try
{
return Ok(await employeeRepsitory.GetEmployees());
}
catch (Exception)
{
return StatusCode(StatusCodes.Status500InternalServerError,
"Error retrieving data from the database");
}
}
Where I put my object in an Ok() ActionResult. If I didn't wrap await employeeRepsitory.GetEmployees() in an Ok() then I have compillation error because I'm not giving back ActionResult. But how does that GetEmployee(int employeeId) method is not giving me compilation error yet I'm clearly giving back an Employee object and not an ActionResult. Thank you for your answers.

How to use If-else condition in Selenium (Java) to make the condition in else block work

I am trying to find an element in the else block, if the condition in the if block doesn't work.
try
{
if(Driver.driver.findElement(By.xpath("//div[#id='Tpay_success']")).isDisplayed())
{
System.out.println("Payment is successful");
Reporter.log("Payment is successful");
}
else
{
Thread.sleep(2000);
{
if(Driver.driver.findElement(By.id("pay_decline")).isEnabled())
{
System.out.println("pay declined");
action.moveToElement(By.id("pay_decline")).isEnabled()).click().perform();
Reporter.log("PAYMENT DECLINED!!");
}
}
catch(ExceptionInInitializerError ex)
{
System.out.println(ex);
}
}
I am getting an error saying:
Unable to locate element: {"method":"xpath","selector":"//div[#id='Tpay_succes']"}
I want the else block to be executed if the if block doesn't get executed.
Any suggestions are welcome. Thanks.
if(Driver.driver.findElement(By.xpath("//div[#id='Tpay_success']")).isDisplayed())
This doesn't work the way you think it does. findElement() will throw an error if the element is not present in the DOM of the currently loaded page. This means that isDisplayed() will not be called in such a case.
You can do what you want with a try...catch:
try
{
Driver.driver.findElement(By.xpath("//div[#id='Tpay_success']"))
System.out.println("Payment is successful");
Reporter.log("Payment is successful");
} catch(ExceptionInInitializerError ex) {
Thread.sleep(2000);
if(Driver.driver.findElement(By.id("pay_decline")).isEnabled()) {
System.out.println("pay declined");
action.moveToElement(By.id("pay_decline")).isEnabled()).click().perform();
Reporter.log("PAYMENT DECLINED!!");
}
}
Note that you should learn about how to make the selenium driver wait for a specific element rather than using Thread.sleep().

IntelliJ compile warning for Groovy code is wrong

Optional<BigDecimal> maybeConvertStringToBigDecimal(Optional<String> string) {
string.flatMap {
try {
return Optional.of(new BigDecimal(it))
} catch (Exception e) {
log.error "Exception: ${e.message}\n\n${e.stackTrace}"
return Optional.<BigDecimal> empty()
}
}
}
This code compiles just fine but IntelliJ shows a red warning that
Cannot return 'Optional<U>' from method returning
'Optional<BigDecimal>'
I think it is clear that there is always Optional<BigDecimal> returned. What can be done to get rid of the warning? Is this an IntelliJ issue or is there an issue with the code?

How do I factor out my use of `try {...} catch (Error e) {log_error(e);}`

I need errors to be logged in the same way across a large number of function calls. Here I want errors from foo.create(...) and File.new_tmp(...) to be logged by handle_error(...).
// compile with `valac --pkg gio-2.0 main.vala`
void log_error(Error e) {
// error logging here
}
void main() {
var foo = File.new_for_path("foo");
try {
foo.create(FileCreateFlags.NONE);
} catch (Error e) {
log_error(e);
}
FileIOStream tmp_stream;
try {
File.new_tmp(null, out tmp_stream);
} catch (Error e) {
log_error(e);
}
}
(Yes, main should continue with the FileIOStream stuff if foo.create fails, which is why they're in separate try/catch blocks.)
I want to factor out the use of try {...} catch (Error e) {log_error(e);} into a function like so:
delegate void Action();
void log_error(global::Action action) {
try {
action();
} catch (Error e) {
// error logging here
}
}
void main() {
var foo = File.new_for_path("foo");
log_error(() => foo.create(FileCreateFlags.NONE));
FileIOStream tmp_stream;
log_error(() => File.new_tmp(null, out tmp_stream));
}
But valac gives the warning unhandled error 'GLib.IOError' because you can't seem to catch errors thrown within a closure, nor can I just rewrite log_error(...) as a #define macro as vala doesn't support them. So what can I do?
You can catch exceptions thrown in closures, you just need to have the delegate throw the exception. What you want is probably something like this:
public delegate T? Action<T> () throws GLib.Error;
T? log_error<T> (global::Action<T> func) {
try {
return func ();
} catch (GLib.Error e) {
// error logging here
return null;
}
}
void main () {
var foo = File.new_for_path("foo");
log_error<GLib.FileOutputStream> (() => foo.create (FileCreateFlags.NONE));
FileIOStream? tmp_stream = null;
GLib.File? f = log_error<GLib.File> (() => File.new_tmp (null, out tmp_stream));
}
Note that I've made it a generic so you can actually use a return value. If you want it should be trivial to remove the generic type argument and just return void, though you'll lose some flexivility.