NullPointerException with findButton() Codename One - nullpointerexception

I'm getting a NullPointerException when I run a very simple piece of code in Codename One, not sure what's the problem. My code is:
#Override
protected void beforeMain(Form f) {
if(zona1.giveNumberofBulbs() != 0){
int estadoGlobal = zona1.giveGlobalState();
if(estadoGlobal == 0){
findIllu2().setText("OFF");
}
else if(estadoGlobal == 1){
findIllu2().setText("ON");
}
else{
findIllu2().setText("...");
}
}
else{
findIllu1().setVisible(false);
}
}
where Illu1 and Illu2 are Buttons. The exception says java.lang.reflect.InvocationTargetException Caused by: java.lang.NullPointerException
at com.codename1.ui.util.UIBuilder.findByName(UIBuilder.java:578)
at generated.StateMachineBase.findIllu2(StateMachineBase.java:530),
appreciate your help!

Super stupid and noob question, I now see. I found the answer few minutes after I posted it, even though it had bugged me for several hours now. I had to give each find function the Form f as argument. Works now!

Related

Bukkit nullpointerexception

Does anyone knows how to fix this nullpointerexception?
https://haste.nycode.de/gipicaxune.avrasm
Silvan
The world you are trying to load with your code (Bukkit.getWorld("myWorld");) in class InventoryListener in line 55.
Before you handle optional information you should check if it exist.
Example:
if(Bukkit.getWorld("myWorld") == null) {
return;
}
Bukkit.getWorld("myWorld").//do something

C#-like case when in kotlin?

I come from a C# background and one really nice thing they offer is a way to add conditionals to a switch statement
switch(type)
{
case "edit":
// do something...
break;
case "delete" when user.isAdmin:
// do something...
break;
}
I'm curious if Kotlin has such an ability with their equivalent when? I'm not seeing that it's possible but I'm also still getting the hang of Kotlin so maybe there is a way.
I figured it out, guess I should have just asked Android studio XD
for those wondering, heres how:
when {
type == "edit" -> {
// do sometihng...
}
type == "delete" && user.isAdmin -> {
// do sometihng...
}
}
hope this helped someone else.

Kotlin: lambda run alternative scenario

I have userDto, contains programs, which contains actual field. Actual program can be only one. I need to get it. Than, I run this:
userDto.programs.sortedBy { it.created }.findLast { it.actual }?
Okay, but I want to foresee case, when findLast returns null, & throw exception. Please, advice, how to do it?
UPD:
ProgramType.valueOf(userDto.programs
.sortedBy { it.created }
.findLast { it.actual }
//check here
!!.programType!!).percentage
You are pretty close actually :)! What you could do is:
userDto.programs.sortedBy { it.created }.findLast { it.actual } ?: throw RuntimeException()
Or if you're trying to actually avoid throwing an error(couldn't really tell with the way question is asked), you could just do an error check like this:
userDto.programs.sortedBy { it.created }.findLast { it.actual }?.let{
//rest of your code goes here
}
Hope this helps, cheers!

How do I properly handle these exceptions?

In the following method, I need to handle if a null gets passed in but I am unsure how. How do I handle this so this method will pass a JUnit test?
public Album(String name) {
if(name==null){
// what do I do here?
}
this.name = name;
this.images = new ArrayList<Image>();
}
And in this method, how do I handle if the index passed in is either negative or greater than the size of the array list it is getting from?
public Image getImage(int index) {
if (index < 0 || index > images.size()) {
// how to handle here?
}
return images.get(index);
}
I've tried various things for both, but these methods keep failing their JUnit tests. Thank you for any input you can give me.
if(name==null){
throw new IllegalArgumentException("name");
}
although personally I don't see anything wrong with throwing NullPointerException directly.
if (index < 0 || index > images.size()) {
throw new ArrayIndexOutOfBounds(index);
}
But you know what? images.get(index) will throw it as well, so don't bother checking it first. Less code to read and test. In fact you already made a mistake. Your precondition will let index equal to images.size(). There should have been index >= images.size().

Erroring with PetrelLogger.NewAsyncProgress

I am using the PetrelLogger.NewAsyncProgress which seems to work well. However I can't figure out how to report an error with my task. Once I Dispose of the NewAsyncProgress, it reports 'Success' for my task.
I have tried setting the ProgressStatus = -1, but that didn't make a difference.
Example:
using (_asyncProgress = PetrelLogger.NewAsyncProgress("Doing Job", ProgressType.Default, (AsyncProgressCanceledCallback)AsyncProgressCanceled, this))
{
try
{
//Do Something
_asyncProgress.ProgressStatus = 100;
}
catch (Exception e)
{
//Error happened
_asyncProgress.ProgressStatus = -1;
}
}
So if an exception is thrown, the task manager result is Success 100%. Any ideas?
It's not possible in Ocean at the moment. However, we have such requirement recorded, so it can be implemented in one of future releases